I am looking to create C# console application, which will use my desired webservice (http://localhost/MyService/MyService.asmx).
My Console application will consume above webservice and will call web methods inside it, I would prefer to pass the values from console window as arguments, say if there is web method from name "MyDetails", so from console application if I pass "admin" and its "password" then it will give the results on my console window.
For example if I try to run from console window as below:
run>> myconsoleservice.exe MyDetails admin password
Edit: I want to create console application which will consume my webservice and all the parameters to Web method will be passed from arguments.
Thanks.
Best Regards,
My attempt...
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();
}
}
Right click on "References" in your project and choose "Add Web Reference."
To use arguments,
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;
}
}
Most versions of Visual Studio (if that's what you're using) will allow you to create a Web Reference, which generates all the code to consume a web service.
As for calling the methods based on arguments in the command line, you'll need to use Reflection
. See below:
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);
}
Note that this will only work if all your arguments are strings. If you want to call methods with other types you'll have to somehow convert the input strings to the proper types. Also, this is C# 3 or higher.
Sounds like you need to add a web reference to the service. You can either use if statements to compare the argument to certain method names and call them, or use reflection to find and execute the methods.
I do know of a way in code to automatically update the web reference with new methods that might appear in the service because adding a web reference creates code that is compiled into your app, but you could parse the wsdl yourself and create the soap request and send it using HttpWebRequest.
精彩评论