Switch-case语句未按预期工作

时间:2012-06-21 07:03:20

标签: c# .net

我编写了一个基本适用于SWITCH语句的小程序。它在switch语句中读取每个case并调用并执行适当的函数。但是,我遇到的问题是,当调用switch语句的第一种情况时,所有函数都会被执行。不应该是这种情况,因为只应执行由特定switch-case语句调用的函数。有人可以告诉我我的代码有什么问题吗?发布我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Drawing;
using System.ComponentModel;
using System.ServiceModel.Security;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
using Excel1.MWMClient.MWMServiceProxy;
namespace Excel1
{
    class WebServiceFunctions
    {
        private string siteID = "INDIA";
        static TemplateClient templateClient;
        static TaskClient taskClient;
        public WebServiceFunctions()
        {
            templateClient = new Excel1.MWMClient.MWMServiceProxy.TemplateClient();
            templateClient.ClientCredentials.UserName.UserName = "admin";
            templateClient.ClientCredentials.UserName.Password = "admin";
            templateClient.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;

            taskClient = new Excel1.MWMClient.MWMServiceProxy.TaskClient();
            taskClient.ClientCredentials.UserName.UserName = "admin";
            taskClient.ClientCredentials.UserName.Password = "admin";
            taskClient.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
        }
        public void GetAllTemplateNames(String strSite, String strRetVal)
        {
            //GetAllTemplateNames
            Console.WriteLine("Invoking GetAllTemplate method");
            Console.WriteLine("List of all templates with details");
            Console.WriteLine("-----------------------------------");
            List<Excel1.MWMClient.MWMServiceProxy.TemplateData> tData = templateClient.GetAllTemplate(strSite).ToList();
            foreach (Excel1.MWMClient.MWMServiceProxy.TemplateData data in tData)
            {
                Console.WriteLine("TemplateName:" + data.name);
                Console.WriteLine("TemplateDescription:" + data.description);
                Console.WriteLine("SiteName:" + data.site_name);
                Console.WriteLine("-----------------------------------");
            }
            Console.WriteLine("-----------------------------------");
        }
        public List<TemplateData> ReturnAllTemplateNames()
        {
            // Console.WriteLine("Invoking GetAllTemplate method");
            // Console.WriteLine("List of all templates with details");
            // Console.WriteLine("-----------------------------------");
            List<Excel1.MWMClient.MWMServiceProxy.TemplateData> tData = templateClient.GetAllTemplate(siteID).ToList();
            foreach (Excel1.MWMClient.MWMServiceProxy.TemplateData data in tData)
            {
                Console.WriteLine("TemplateName:" + data.name);
                Console.WriteLine("TemplateDescription:" + data.description);
                Console.WriteLine("SiteName:" + data.site_name);
                Console.WriteLine("-----------------------------------");
            }
            Console.WriteLine("-----------------------------------");
            return tData;
        }
        public void GetTaskInstanceFromTemplate(Excel1.MWMClient.MWMServiceProxy.TemplateData data)
        {
            Console.WriteLine("Invoking GetTaskInstanceFromTemplate method");
            Console.WriteLine("-----------------------------------");
            Excel1.MWMClient.MWMServiceProxy.TaskInstance tInstance = taskClient.GetTaskInstanceFromTemplate(siteID, data);
            if (tInstance != null)
            {
                Console.WriteLine("TaskName: " + tInstance.name);
                Console.WriteLine("TaskDescription :" + tInstance.description);
            }
            Console.WriteLine("-----------------------------------");
        }
        public TaskInstance ReturnTaskInstanceFromTemplate(Excel1.MWMClient.MWMServiceProxy.TemplateData data)
        {
            Console.WriteLine("Invoking GetTaskInstanceFromTemplate method");
            Console.WriteLine("-----------------------------------");
            Excel1.MWMClient.MWMServiceProxy.TaskInstance tInstance = taskClient.GetTaskInstanceFromTemplate(siteID, data);
            if (tInstance != null)
            {
                Console.WriteLine("TaskName: " + tInstance.name);
                Console.WriteLine("TaskDescription :" + tInstance.description);
            }
            Console.WriteLine("----------------------" + tInstance.name + "-------------");
            return tInstance;
        }
        public int CreateTask(Excel1.MWMClient.MWMServiceProxy.TaskInstance instance)
        {
            Console.WriteLine("Invoking CreateTask method");
            Console.WriteLine("-----------------------------------");
            int retVal = taskClient.CreateTask(instance);
            Console.WriteLine("Task create successfully:ID=" + retVal.ToString());
            Console.WriteLine("-----------------------------------");
            return retVal;
        }
        public void GetTaskStatus(int taskId)
        {
            Console.WriteLine("Invoking GetTaskStatus method");
            Console.WriteLine("------------------****-----------------");
            Excel1.MWMClient.MWMServiceProxy.TaskStatus taskStatus = new Excel1.MWMClient.MWMServiceProxy.TaskStatus();
            taskStatus = taskClient.GetTaskStatus(taskId);
            Console.WriteLine("Task status : " + taskStatus.taskStatus.ToString());
            Console.WriteLine("-----------------------------------");
        }
        public void PerformTaskOperation(int taskId, string operation, String reason)
        {
            Console.WriteLine("PerformTaskOperation method");
            Console.WriteLine("-----------------------------------");
            int operationStatusCode = 0;
            operationStatusCode = taskClient.PerformTaskOperation(taskId, operation, reason);
            String updateSuccess = operationStatusCode == 1 ? "update success" : "update failed";
            Console.WriteLine("Returned Operation Status Code = " + operationStatusCode + "|| Updation status message:" + updateSuccess);
        }
        //public void GetTaskHistory(int taskId, int detailLevel)
        //{
        //    Console.WriteLine("Invoking GetTaskHistory method");
        //    Console.WriteLine("------------------*****-----------------");
        //    Excel1.MWMClient.MWMServiceProxy.TaskHistory taskHistory = new Excel1.MWMClient.MWMServiceProxy.TaskHistory();
        //    taskHistory = taskClient.GetTaskHistory(taskId, detailLevel);
        //    if (taskHistory.listOfAllowedActions != null)
        //        foreach (Excel1.MWMClient.MWMServiceProxy.TaskAllowedAction allowedAction in taskHistory.listOfAllowedActions)
        //        {
        //            Console.WriteLine("Task History - allowedAction : " + allowedAction.actionName);
        //            Console.WriteLine("-----------------------------------");
        //        }
        //    if (taskHistory.listOfCustomAttributes != null)
        //        foreach (Excel1.MWMClient.MWMServiceProxy.TaskCustomAttributes customAttribute in taskHistory.listOfCustomAttributes)
        //        {
        //            Console.WriteLine("Custom Attribute : " + customAttribute.key + " | Value:" + customAttribute.value);
        //            Console.WriteLine("-----------------------------------");
        //        }
        //    if (taskHistory.taskinstanceEscalations != null)
        //        foreach (Excel1.MWMClient.MWMServiceProxy.TaskEscaltion taskEscaltion in taskHistory.taskinstanceEscalations)
        //        {
        //            Console.WriteLine("Task Escaltion : " + taskEscaltion.escalationLevel + " | escalationType : " + taskEscaltion.escalationType);
        //            Console.WriteLine("-----------------------------------");
        //        }
        //    if (taskHistory.taskInstanceUsers != null)
        //        foreach (Excel1.MWMClient.MWMServiceProxy.TaskUser taskUser in taskHistory.taskInstanceUsers)
        //        {
        //            Console.WriteLine("Task User : " + taskUser.firstName + " ,  " + taskUser.lastName);
        //            Console.WriteLine("-----------------------------------");
        //        }
        //    if (taskHistory.taskInstanceGroups != null)
        //        foreach (Excel1.MWMClient.MWMServiceProxy.TaskGroup group in taskHistory.taskInstanceGroups)
        //        {
        //            Console.WriteLine("Group : " + group.groupName + "| escalationLevel : " + group.escalationLevel);
        //            Console.WriteLine("-----------------------------------");
        //        }
        //    if (taskHistory.taskInstanceSkills != null)
        //        foreach (Excel1.MWMClient.MWMServiceProxy.TaskSkill skill in taskHistory.taskInstanceSkills)
        //        {
        //            Console.WriteLine("Skill : " + skill.skillName + "| description : " + skill.description);
        //            Console.WriteLine("-----------------------------------");
        //        }
        //    if (taskHistory.taskStatusList != null)
        //        foreach (Excel1.MWMClient.MWMServiceProxy.TaskStatus taskStatus in taskHistory.taskStatusList)
        //        {
        //            Console.WriteLine("TaskStatus : " + taskStatus.taskStatus);
        //            Console.WriteLine("-----------------------------------");
        //        }
        //    if (taskHistory.userTaskInstanceList != null)
        //        foreach (Excel1.MWMClient.MWMServiceProxy.UserTaskInstance userTaskInstance in taskHistory.userTaskInstanceList)
        //        {
        //            Console.WriteLine("UserTaskInstance status: " + userTaskInstance.status);
        //            Console.WriteLine("-----------------------------------");
        //        }
        //    //taskHistory.listOfAllowedActions
        //}
        public void CreateTaskFromTemplate(Excel1.MWMClient.MWMServiceProxy.TemplateData data)
        {
            //taskClient.ClientCredentials.UserName.UserName = "USR";
            //taskClient.ClientCredentials.UserName.Password = "PWD";
            //taskClient.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;

            Console.WriteLine("Invoking CreateTaskFromTemplate method");
            Console.WriteLine("------------------***-----------------");
            int taskId = taskClient.CreateTaskFromTemplate(siteID, data);
            Console.WriteLine("Created task from template with TaskId: " + taskId);
            Console.WriteLine("-----------------------------------");

        }
    };
    class Program
    {
        static WebServiceFunctions wsf = new WebServiceFunctions();
        public static void Main(string[] args)
        {
            //
            //Initializing the excel sheet
            //
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"D:/WebServiceTemplate.xlsx");
            Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Excel.Range xlRange = xlWorksheet.UsedRange;
            //
            //Getting row count, column count and the number of sheets 
            //
            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;
            int numSheets = xlWorkbook.Sheets.Count;
            //
            // Iterate through the sheets. They are indexed starting at 1.
            //            
            for (int row = 2; row <= rowCount; row++)
            {
                //
                // Do something with the data in the array with a custom method.
                //
                String strRetVal = xlRange.Cells[row, 3].Value;
                String strFunctionName = xlRange.Cells[row, 4].Value;
                String strParam1 = xlRange.Cells[row, 6].Value;
                String strParam2 = xlRange.Cells[row, 7].Value;
                String strParam3 = xlRange.Cells[row, 8].Value;
                String strParam4 = xlRange.Cells[row, 9].Value;
                TemplateData tData = new TemplateData();
                TaskInstance tInstance = new TaskInstance();
                int tID = 0;
                ProcessInput(strRetVal, strFunctionName, strParam1, strParam2, strParam3, strParam4, tData, tInstance, tID);
                //creating a task from templates in one flow
                //1)Get all templates.
                List<Excel1.MWMClient.MWMServiceProxy.TemplateData> templateDataList = new List<TemplateData>();
                //bool createTaskFromTemplate_Flag = false;
                templateDataList = wsf.ReturnAllTemplateNames();
                //if (createTaskFromTemplate_Flag)
                //{
                //    foreach (TemplateData templateDataInst in templateDataList)
                //    {
                //        //We are not using the method CreateTaskFromTemplate for this application
                //        //WebServiceFunctions.CreateTaskFromTemplate(templateDataInst);
                //    }
                //}
                bool createTaskForAll = false;
                if (createTaskForAll)
                {
                    foreach (TemplateData templateDataInst in templateDataList)
                    {
                        //2)Get task instance
                        TaskInstance taskInstance = new TaskInstance();
                        taskInstance = wsf.ReturnTaskInstanceFromTemplate(templateDataInst);
                        taskInstance.scheduleActivity = new ScheduleActivity();
                        taskInstance.scheduleActivity.nextRun = new DateTime();
                        taskInstance.scheduleActivity.isRecurring = 0;
                        //over riding taskType in task
                        taskInstance.taskType = "pull";

                        //3)Create task.
                        wsf.CreateTask(taskInstance);
                    }
                }
                else 
                {
                    int numberOfTemplatesReturned = templateDataList.Count();
                    Random random = new Random();
                    int randomTemplateInList = random.Next(0, numberOfTemplatesReturned);
                    Console.WriteLine("Random number from template list:" + randomTemplateInList);
                    TemplateData templateDataInstance = new TemplateData();
                    templateDataInstance = templateDataList.ElementAt(randomTemplateInList);

                    //2)Get task instance
                    TaskInstance taskInstance = new TaskInstance();
                    taskInstance = wsf.ReturnTaskInstanceFromTemplate(templateDataInstance);

                   // 3)Create task.
                    tID = wsf.CreateTask(taskInstance);
                    wsf.GetTaskStatus(tID);
                    //int detailLevel = 2;
                   // Console.WriteLine("Task History for a newly created task would contain minimal details.");
                   // Console.WriteLine("Run method with taskId of a task which has gone through various phases to see detailed history.");
                   // wsf.GetTaskHistory(tID, detailLevel);
                }
               // Console.WriteLine("Updating status via update task status");
               //wsf.PerformTaskOperation(tID, "DELETE", "trying pauseeee");

               // Console.WriteLine("Press Enter To Exit Client Application.");
                Console.ReadLine();
            }
        }
        public static void ProcessInput(String strRetVal, String strFunctionName, /*String strParamCount,*/ String strParam1, String strParam2, String strParam3, String strParam4, TemplateData tData, TaskInstance tInstance, int tID)
        {
            switch (strFunctionName)
            {
                case "ITemplate.GetAllTemplate":
                    {
                        //ITemplate.GetAllTemplate code
                        MessageBox.Show("ITemplate.GetAllTemplate");
                        wsf.GetAllTemplateNames(strParam1, strRetVal);
                        break;
                    }
                case "ITask.GetTaskInstanceFromTemplate":
                    {
                        //ITask.GetTaskInstanceFromTemplate code
                        MessageBox.Show("ITask.GetTaskInstanceFromTemplate");
                        wsf.GetTaskInstanceFromTemplate(tData);
                        break;
                    }
                case "CreateTask":
                    {
                        //CreateTask code
                        MessageBox.Show("CreateTask");
                       wsf.CreateTask(tInstance);
                        break;
                    }
                case "UpdateDatabase":
                    {
                        //UpdateDatabase code
                        MessageBox.Show("UpdateDatabase");
                        break;
                    }
                case "GetTaskStatus":
                    {
                        //GetTaskStatus code
                        MessageBox.Show("GetTaskStatus");
                        wsf.GetTaskStatus(tID);
                        break;
                    }
                case "VerifyValue":
                    {
                        //VerifyValue code
                        MessageBox.Show("VerifyValue");
                    }
                    break;
            }
        }
    };
}

1 个答案:

答案 0 :(得分:0)

对ProcessInput()的调用处于循环中。你有没有在调试中运行它?那个开关应该有效。你确定对ProcessInput的SINGLE调用会运行所有的情况吗?