Motive
The reason for writing this article is that a lot of people have published too many articles on creating web services and making it simple but non is actually simple. Almost all of those article focus on the boring theory and functionality of the web service and not on actually what we least require to create a simple web service. Beginners to web services like me get confused in the functionality and un necessary theory. With this confusion the actual cause of creating a very simple web service dies. I have made it very simple to create a .net web service and my readers can extend this article like instead of addition they can put in what ever functionality they want in the WebMethod to get this web service work for them according to their needs.
Some Boring Theory That We Can’t Get Rid Off
- When ever we create a web service we need some part of it to be accessed from outside. So that we can call it and make it do something for us. to make a method accessible to the outside world we need to add [WebMethod] Attribute on top of that method.
- The method that we need to expose to the outside world must be public.
- The file that we need to call is the one with “.asmx” extension to execute our web service.
- We can have a private method in our web service that can be called from with in our public method. Like we can have some classes that will have some method that will do some stuff for us. To get access to that functionality of those method. We need to call those methods from our public method by creating objects of those classes in the Service1.asmx file and calling those methods through those objects.
Now Coming To The Real Stuff.
My development environment
- Visual Studio 2010
- Windows 7 professional 64 bit
- Dot net framework 3.5
Creating the Web Service
Open your Visual Studio and click New Project (Section 1 in image). From the left side panel choose C#(Section 2 in image) , Web(Section 3 in image). From the Framework selection dropdown at the top select .Net Framework 3.5 (Section 4 in image) and from the list below that select ASP.Net Web Service Application (Section 5 in image). Give the name of the web service application (Section 6 in image) and the location (Section 7 in image) where it will be saved. Click OK (Section 8 in image)to create the project.
namespace WsMirTestWebService { /// <summary> /// Summary description for Service1 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService] public class Service1 : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } } }
This is what is created for us by default. now replace the method
[WebMethod] public string HelloWorld() { return "Hello World"; }
with your own method that you want to call from outside. Lets say we want to send two numbers to the web service and get the sum
[WebMethod] public int add(int a,int b) { int c = 0; c = a+b; return c; }
Testing Created Web Service
Now when we run our web service, we will see Service1.asmx in brower with all our WebMethods. Currently we have only one method i.e, add.
Clicking on the add link will take us to the second screen where we will supply two values to our WebMethod and will click the Invoke button to execute the add method.
We provide two values 7,3 and click Invoke button. This will execute our WebMethod add and if our method is returning some value it will show us the returned value.
https://mirnauman.wordpress.com/2012/08/15/publishing-a-dot-net-web-service-or-website/
The above link can be used to publish the created web service.
7 comments
Comments feed for this article
September 11, 2012 at 1:17 pm
Lê Xuân Nguyên
Reblogged this on Lê Xuân Nguyên.
October 18, 2012 at 5:39 pm
Android: Calling a web service using KSOAP2. Passing values to a web service. « Mir
[…] I ll assume that we have a web service developed and deployed for this purpose. For this tutorial we will use the web service that i have previously developed which receives two numbers and returns their sum. https://mirnauman.wordpress.com/2012/08/08/creating-a-simple-dot-net-web-service/ […]
November 20, 2012 at 7:20 pm
zohaib
hiii Mir its awsume work you have done…Realy good Amazing
November 21, 2012 at 4:13 am
Mir
thanks zohaib
May 30, 2013 at 1:08 am
TrangD
excuse me,
I want to add database to web service, than format it as json in order that an app android can use and update db from it. But I do not know how to do?
Can you write a tutorial show me how to do?
thank you very much!
January 17, 2014 at 10:38 pm
Find more
It ís difficult to find experienced people
for this topic, but you sound like you know what youíre
talking about! Thanks
March 5, 2014 at 2:48 am
Rehan Ahmed
Thanx dear you are great. 🙂