如何创建C#控制台应用程序来cosume的.NET Web服务

如何创建C#控制台应用程序来cosume的.NET Web服务

问题描述:

我期待创建C#控制台应用程序,它会用我的期望web服务(HTTP://localhost/MyService/MyService.asmx)。

I am looking to create C# console application, which will use my desired webservice (http://localhost/MyService/MyService.asmx).

我的控制台应用程序会消耗上面的web服务并调用它里面的Web方法,我宁愿从控制台窗口作为参数传递的价值观,说如果从名称为MyDetailsWeb方法,因此,从控制台应用程序如果我通过管理和 。密码,那么它会给我的控制台窗口中的结果。

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:

运行>> myconsoleservice.exe MyDetails管理员密码

run>> myconsoleservice.exe MyDetails admin password

编辑:我要创建控制台应用程序,这将消耗我的web服务和所有的参数,Web方法将参数传递。

I want to create console application which will consume my webservice and all the parameters to Web method will be passed from arguments.

感谢。

最好的问候,

我的企图......

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://*.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();
    }
}