How to consume a web service in code directly
19-Dec-1313 Leave a comment
As well as using a wizard it is possible to access web services using code. These are the steps.
- Open a Visual Studio Command Prompt
- Navigate to a suitable directory e.g. c:\Temp
- Use WSDL. Examples
C# is default language
wsdl http: // …myservice.asmx
For VB use
wsdl /language:vb http://.. myservice.asmx
Your service might be .svc - Press return and the wsdl program should write out a file for your consumption
- This creates a web service proxy, which will talk to the service.
- In your project create a folder say named Service
- Add in your new file
- Add reference to System.Web.Services
- Add code similar to:
myService MyService1 = new myService()
var result = myService.myMethod();
To create a batch file to do this automatically
Thanks to:
call “C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat” x86
wsdl /language:vb /out:MyServiceName.vb http:// … myservice.svc
pause
Why does the consumed service return arrays when I specified a List(of T)
WCF tries hard to not create services which are .Net dependent. So by default the return of List(Of T) will return an array.
If you add a service using the .Net UI, then there is an option:
Configure Service Reference > Always Generate Message Contracts > Collection Type > Change to what you want, which may be a System.Collections.Generic.List.
There is a similar option with svcutil and probably wsdl, but I have not used this yet.
The End.