Monday, 22 September 2014

How to generate web service from wsdl file

This is a common scenario faced by developers who do web service based integrations. The partner if its an external company, might send over the wsdl file and ask to create a web service out of it so they can consume your webservice.

This is when the partner demands more control and they want to make sure all their partners host similar webservices so that the partner's code for the web service call is consistent.

Now to solve the matter, we do not have a GUI genie (wizard) for our help. We will have to do each step manually taking help of command line.

Here are the steps:
1) From the WSDL file create an Interface class using wsdl tool. For this, bring up the Visual Studio Command Prompt Window and run the below command.
wsdl.exe yourFile.wsdl /l:CS /serverInterface
Note: You can decide the language of the Interface Class generated. In parameter /l, use VB or CS for your Visual Basic or CSharp respectively. Finally the command will create a new .cs or .vb file.

2) Create a new .NET Web Service (.asmx).

3) Import the file created in the above step into your project.

4) In your .asmx.cs file in Code-View, modify class as below to implement the interface generated in the above step:
public class MyWebService : System.Web.Services.WebService, IWsdlService
{
// Web methods and other calls goes here....
}
5) Implement the methods mentioned in the Interface class. Finally your webservice class will be something like this:
public class MyWebService : System.Web.Services.WebService, IWsdlService
{
[WebMethod]
public string MyWeMethod1()
{
// your business logic goes here...
return "MyWeMethod1 Result";
}
[WebMethod]
public string MyWeMethod2()
{
// your business logic goes here...
return "MyWeMethod2 Result";
}

No comments:

Post a Comment