Have you ever thought of sharing your functionality to outside world where someone like you would like to consume that functionality in his app ? How you have been doing that till now....by sharing shared library or by dynamic link libraries or by sharing the actual code ? :) And how many times you got feedback asking to resolve compatibility issues due to language constraints?
The best way to share your functionality/logic with others is through Web Services. W3C established two frameworks SOAP and REST for web services. They have defined the standard format in which two systems will interact using web services.
The main differences between SOAP and REST based services:
- REST based services are light weight as compared to SOAP
- SOAP based services are secured as compared to REST. If you are creating a REST based service, then you need to explicitly take care of securing it.
- REST based services work closely with OAuth: the open authentication model that is followed by most Web2.0 apps.
- SOAP and REST based services accept input in XML and JSON format.
- REST based services provides response in XML and JSON. SOAP does the same but the response is wrapped within SOAP envelop with header and body.
NOTE: I have used Visual Studio 2010 for describing this.
2. Right click on solution in Solution explorer> click on Add New Item
3. Select Web Service, set its name as MyService.asmx and click ok:
4. Now you can see the web service "MyService.asmx" file and code file "MyService.vb" is added to solution
5. Double click on MyService.asmx to open it :
I have written web service method "Greetings" as example. You can write your own web service method in similar manner.
The important thing to keep in mind is that when you are publishing your web service make sure you set the appropriate namespace for your service. Currently it is by default value "http://tempuri.org".
6. Hit Run(F5) to start Cassini( your local web server that comes with VS). This will take you to the service description page where service is available for consumption:
7. Click on the service name "Greetings" to open the page from where you can test the web service:
Enter the name parameter and hit "Invoke" button and see the result :
8. You can hit navigate to the URL : http://localhost:30149/WebSite1/MyService.asmx?WSDL
to view the WSDL file for this service:
You may then share this WSDL with the consumer of your service. Then it is very easy to generate the client using WSDL to invoke the service.
I hope you find this post helpful in understanding how to create SOAP based web services on the fly with .Net framework.