Generate .Net web service from WSDL file “identical”, best practices


The goal from generating .Net web service from a WSDL file is to generate a typically web service that send and receive identically SOAP messages for both you web service and the native WSDL that you need to generate the web service from it.

To achieve this goal with  a simplest way (i will describe in the end of this post way it is a simplest 🙂 ”  follow the steps:

1-use WSDL.exe to generate an interface from the given WSDL file , to reach the “WSDL.exe”  open start menu -> all programs -> Microsoft Visual Studio -> Visual Studio Tools -> Visual Studio command prompt.

2-Type this command “WSDL.exe  <path to WSDL file in your machine or URL>  /Language:CS   /Out:<Filename “path to the output file”>  /serverInterface

3-Open the output folder that you specified you will find a C# file that contains all generated classes from the WSDL + an interface that contains a definition for  web methods in this service , you will find that each web methods has its necessary attributes that makes this methods identically with WSDL file “see the image below for example”

Generated WSDL interface from WSDL.exe

Generated WSDL interface from WSDL.exe

Now lets move to our web service that we need it to be typical the WSDL

1-From the Visual Studio in your web service project add a web reference to the WSDL file , this will generate proxy classes

2-Create a web service then open the generate c# file , copy and paste the web method definition with attributes “as it is ” and put it in your service

3-create the implementation body for the method and make it return the response that specified in method return type

4-you will notice that the refrence for the request and the response will come from the generated proxy classes from Visual Studio when the web reference added “step 1”

So after that your web service WSDL totally will be diffrenent from the original WSDL file but the soap message for both will be typically and that what you need exactaly to make your web service identically with the native WSDL

Why this is a simplest way , we can simply create our web service implement the generated interface why we didn’t do that ?

The problem is you must cast the parameters  for each web method from the type that generated from “WSDL.exe” to the type that generated from the proxy classes “from add web reference” , for sure the casting will not doable specially in non-primitive types , in this case you must copy all properties values in this type to the same in the other one !

And you do the same step in the web method return that will cost you a lot of implementation steps.

With copy and paste the method in your web service will use the proxy types for return and parameters that doesn’t need a casting or copying from object to another , you can operate in the parameter object as it is and return the object to be return directly also.

hope it will help you.


One thought on “Generate .Net web service from WSDL file “identical”, best practices

  1. Γυρίζω στο ιντερνέτ σήμερα για περισσότερες από τρεις ώρες και
    δενβρήκα πολλά άρθρα που να είναι εξίσου ενδιαφέροντα με το δικό
    σας. Πιστεύω ότι αν όλοι οι ιδιοκτήτες των blogs έφτιαχναν τέτοιο ποιοτικό
    περιεχόμενο το ιντερνετ, θα ήταν πολύ καλύτερο για

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s