2

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,

Manoj Singh
  • 7,569
  • 34
  • 119
  • 198
  • 6
    What is the question? – Dan Abramov Feb 28 '11 at 18:45
  • Guys I want to create console application which consume my webservice and then from console window, I will pass arguments to the web method and it will return results accordingly – Manoj Singh Feb 28 '11 at 18:50
  • 1
    For clarification... Are "admin" and "password" windows credentials? Or are they arguments for the "MyDetails" method? – dana Feb 28 '11 at 19:00
  • @dana they are arguments to the webmethod "MyDetails", In above I have given just two arguments, however argument list can be more , so I am looking to create more generic code, so that I can use any webservice and any method inside it and just need to pass arguments from console. – Manoj Singh Feb 28 '11 at 19:05

4 Answers4

2

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;
    }
}
sth
  • 222,467
  • 53
  • 283
  • 367
Daniel Ahrnsbrak
  • 1,067
  • 8
  • 15
2

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();
    }
}
dana
  • 17,267
  • 6
  • 64
  • 88
  • @thanks dana, so print the result generated by s.MyMethod(); on the console window, we just need to take all values and use console.print, so that we can see the result on console window itself – Manoj Singh Feb 28 '11 at 18:55
  • @Dana, why we are using s.Credentials = new NetworkCredentials(userName, password); as these userName and password are arguments to my web method, and these arguments can be dynamic also, so need more generic code – Manoj Singh Feb 28 '11 at 19:10
  • @MKS - Sorry about that. I was confused about the question and have updated my code to use dynamic args for the invoked method. – dana Feb 28 '11 at 19:21
  • @Dana this line "Type[] argTypes = mi.GetGenericArguments(); " is returning 0 dimensions, I think because my method is not generic, please suggest another way – Manoj Singh Mar 01 '11 at 08:21
  • @MKS - Alright, I think I got it. I am using the `MethodInfo.GetParameters()` method to retrieve the parameters. I also added some code to print the results a little prettier. – dana Mar 01 '11 at 16:57
  • THanks @Dana, can you please suggest how to pass the "out" type of parameter from args as my last parameter is of type global::GenericWebserviceChecker.GenericWS.WSIResult crisresult = new global::GenericWebserviceChecker.GenericWS.WSIResult();, which will be pass in my webmethod to return the result from webservice. For Example my webmethod require this type of values from myProfile = service.GetLiteMemberProfile(String.Concat("00",user), password, out crisresult);, Please suggest – Manoj Singh Mar 02 '11 at 09:14
  • @MKS - The output parameters should be copied to the argument array that was passed to the `Invoke()` method. I have updated my code to check for and print these. Interestingly enough, I have never used output parameters for a webservice, but it seems like this should work. – dana Mar 02 '11 at 17:03
1

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.

Tony Casale
  • 1,537
  • 8
  • 7
  • I am using .NET 2.0, so I assume above code sample will work in it perfectly, and one more thing, How to print the results generated by webmethods on console – Manoj Singh Feb 28 '11 at 19:07
0

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.

Jason Goemaat
  • 28,692
  • 15
  • 86
  • 113