2011-02-28 40 views
2

我期待創建C#控制檯應用程序,該應用程序將使用我期望的web服務(http://localhost/MyService/MyService.asmx)。如何創建c#控制檯應用程序以彌補.net webservice

我的控制檯應用程序將消耗上述webservice並將調用其中的web方法,我寧願從控制檯窗口作爲參數傳遞值,說如果有名稱爲「MyDetails」的web方法,所以從控制檯應用程序,如果我通過「管理員」和它的「密碼」,然後它會在我的控制檯窗口上顯示結果。

例如,如果我嘗試從如下控制檯窗口中運行:

運行>> myconsoleservice.exe MyDetails管理員密碼

編輯:我要創建控制檯應用程序,這將消耗我的互聯網服務和Web方法的所有參數都將從參數中傳遞。

謝謝。

最好的問候,

+6

問題是什麼? – 2011-02-28 18:45:23

+0

夥計們我想創建控制檯應用程序,消耗我的web服務,然後從控制檯窗口,我會傳遞參數到web方法,它會返回相應的結果 – 2011-02-28 18:50:40

+1

澄清...是「管理員」和「密碼」Windows憑據?還是他們的「MyDetails」方法的參數? – dana 2011-02-28 19:00:26

回答

2

我嘗試......

using System; 
using System.Net; 
using System.Reflection; 
using System.ComponentModel; 

public class WSTest 
{ 
    static void Main(string[] args) 
    { 
     if(args.Length < 1) 
     { 
      Console.WriteLine("Usage: [method_name] ([arg0], ...)"); 
      return; 
     } 

     MyService s = new MyService(); 

     String methodName = args[0]; 
     MethodInfo mi = s.GetType().GetMethod(methodName); 
     if(mi == null) 
     { 
      Console.WriteLine("No such method: " + methodName); 
      return; 
     } 

     ParameterInfo[] parameters = mi.GetParameters(); 
     if(parameters.Length != (args.Length - 1)) 
     { 
      Console.WriteLine("Invalid argument count"); 
      return; 
     } 

     Object[] methodArgs = new Object[parameters.Length]; 
     for(int ix = 0; ix < parameters.Length; ix++) 
     { 
      Type parameterType = parameters[ix].ParameterType; 
      String arg = args[ix + 1]; 
      try 
      { 
       methodArgs[ix] = TypeDescriptor.GetConverter(parameterType).ConvertFrom(arg); 
      } 
      catch 
      { 
       Console.WriteLine("Unable to convert from '" + arg + "' to " + parameterType); 
       return; 
      } 
     } 

     // print results 
     try 
     { 
      Object result = mi.Invoke(s, methodArgs); 

      // ObjectDumper code at http://stackoverflow.com/questions/1347375/c-object-dumper 
      // Alternatively, Console.WriteLine() could be used for simple value types. 
      ObjectDumper.Write(result); 

      // print any out parameters 
      for(int ix = 0; ix < parameters.Length; ix++) 
      { 
       if(parameters[ix].IsOut) 
       { 
        ObjectDumper.Write(methodArgs[ix]); 
       } 
      } 
     } 
     catch(Exception e) 
     { 
      Console.WriteLine("Error invoking method '" + methodName + "'"); 
      Console.WriteLine(e); 
     } 
     Console.WriteLine("Press enter to continue..."); 
     Console.ReadLine(); 
    } 
} 
+0

@thanks dana,所以打印由s.MyMethod()生成的結果;在控制檯窗口中,我們只需要獲取所有值並使用console.print,以便我們可以在控制檯窗口本身看到結果 – 2011-02-28 18:55:07

+0

@Dana,我們爲什麼使用s.Credentials = new NetworkCredentials(userName,password);因爲這些用戶名和密碼是我的web方法的參數,而且這些參數也可以是動態的,所以需要更通用的代碼 – 2011-02-28 19:10:58

+0

@MKS - 對不起。我對這個問題感到困惑,並且更新了我的代碼,使用動態參數來調用方法。 – dana 2011-02-28 19:21:09

2

在您的項目「參考」上點右鍵,選擇「添加Web引用」。

要使用參數,

public static void Main(string[] args) 
{ 
    string method = args[0]; 
    string user = args[1]; 
    string password = args[2]; 

    MyService svc = new MyService();   

    switch (method) 
    { 
     case "MyDetails": 
      svc.MyDetails(user, password); 
      break; 
     case "AnoterFunction": 
      svc.AnotherFunction(); 
      break; 
    } 
} 
+0

感謝@brak,我知道這些步驟非常好,我正在研究如何編寫代碼以從參數中獲取值,然後將這些值傳遞給web方法 – 2011-02-28 18:52:05

+0

好吧,添加了有關如何處理參數的更多詳細信息。 – 2011-02-28 18:55:38

1

的Visual Studio的大多數版本(如果這是你使用的是什麼)將允許您創建一個Web引用,它生成所有代碼以使用Web服務。

至於調用基於命令行參數的方法,你需要使用Reflection。見下:

static void Main(string[] args) 
{ 
    var service = new Service(); //this is your generated web service class 
    var method = service.GetType().GetMethod(args[0]); //gets the method from the command line 
    // create an array to hold the other arguments 
    var myArgs = new Object[args.Length-1]; 
    for(int i=0; i<myArgs.Length; ++i) 
    { 
    myArgs[i] = args[i+1]; 
    } 
    method.Invoke(service, myArgs); 
} 

請注意,這隻會在所有參數都是字符串時才起作用。如果你想調用其他類型的方法,你必須以某種方式將輸入字符串轉換爲適當的類型。此外,這是C#3或更高版本。

+0

我使用.NET 2.0,所以我認爲上面的代碼示例將完美地工作,還有一件事,如何打印在控制檯 – 2011-02-28 19:07:00

0

聽起來像你需要添加一個Web引用的服務。您可以使用if語句將參數與特定方法名稱進行比較並調用它們,也可以使用反射來查找和執行方法。

我知道在代碼中自動更新Web引用時可能出現在服務中的新方法,因爲添加Web引用會創建編譯到您的應用中的代碼,但您可以自己分析wsdl並創建肥皂請求並使用HttpWebRequest發送它。

+0

上由webmethods生成的結果,你可以請一些示例代碼建議! – 2011-02-28 18:59:52

相關問題