11777

How to create c# console application to cosume the .net webservice [closed]

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

<strong>Edit:</strong> 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,

Answer1:

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(); } }

Answer2:

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; } }

Answer3:

Most versions of Visual Studio (if that's what you're using) will allow you to create a <strong>Web Reference</strong>, 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.

Answer4:

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.

Recommend

  • Can't access tempconvert web services
  • Android soap webservice Asynctask app force stop error
  • ksoap 2 communication - sending the asynctask result to UI thread and print
  • Binding a method to a column in datagrid WPF
  • Getting a Template/Generic java.lang.reflect.Method object from org.aspectj.lang.ProceedingJoinPoint
  • Error creating glassfish as a service
  • Update property from interface
  • How to pass parameters to a method by reflection
  • In C#, how do I add event handlers to an object based on names?
  • Get MethodInfo for any method with any signature (delegate for any signature)
  • “org.xmlpull.v1.XmlPullParserException: expected: START_TAG” Error
  • How to set THTTPRio.Converter.Options to soLiteralParams in OnBeforeExecuteEvent
  • Get form values in Symfony2 controller , Child does not exist
  • Ninject: One interceptor instance per one class instance being intercepted?
  • Getting ORA-01861 - Literal does not match format string on SQLPlus Only
  • Use Mockito-core for create mock of final class?
  • F# comparing discriminated unions' by case identifier
  • Android get Serial Number
  • Opening a floating menu (context menu) in Android?
  • Error inflating class ir.noghteh.JustifiedTextView
  • Can you set a background imaged to a PdfPTable when using iText
  • Why String.ToString()? [duplicate]
  • How can I write a where clause in SQL to filter a DATETIME column by the time of day?
  • Task.IsCancelled doesn't work
  • Upgrade to g++ 4.7 (with c++11 support): any ABI incompatibility?
  • how to show filtered JSON data from two different key values in react native
  • First time running a ASP site, and getting server error in '/' Application
  • On which thread JavaFX change listeners are executed ?
  • Pythons argparse default value doesn't work
  • Detection of framework usage on Mac system?
  • Memory error in python- how to use more memory
  • Sort List of Strings By Version
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • apache spark aggregate function using min value
  • Django query for large number of relationships
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?