Anda di halaman 1dari 8

Create Simple Web Service in Visual Studio 2008 / 2010

This tutorial explains how we can create simple Web Services using Visual Studio 2008 or
Visual Studio 2010.

If you are interested in creating web services in java, please follow my post
Create Web Service in Java Using Apache Axis2 and Eclipse

1. Create the Web Service

First create new project and select "New ASP.NET Web Service Application" and I'm giving
the name "MyFirstWebService" to it, you can give any name to your project.

Now you can see auto generated code that you can add methods to create your web service.
You can see simple method "HelloWorld" and in this sample code I have removed it.

I'm going to add simple method called "simpleMethod" which takes a string as an input and
add "Hello" to beginning of that string. Now the code will appear like bellow.
?

1 using System;
2 using System.Collections.Generic;
using System.Linq;
3 using System.Web;
4 using System.Web.Services;
5 namespace MyFirstWebService
6 {
7 /// <summary>
8 /// Summary description for Service1
/// </summary>
9 [WebService(Namespace = "http://tempuri.org/")]
1 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
0 [System.ComponentModel.ToolboxItem(false)]
11 // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment
1 the following line.
2 // [System.Web.Script.Services.ScriptService]
1 public
{
class Service1 : System.Web.Services.WebService
3 [WebMethod]
1 public string simpleMethod(String srt)
4 {
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2 return "Hello "+srt;
2 }
2 [WebMethod]
public int anotherSimpleMethod(int firstNum, int secondNum)
3 {
2 return firstNum + secondNum;
4 }
2 }
5 }
2
6
2
7
2
8
2
9
3
0
3
1
3
2
Then you can run your code and you can see the resulting page as bellow.
2. Create the Client Program

We have created our simple web service and we have to create small
client program to use this web service. There you can open another
instant of Visual Studio and create new "Console Application" project.

Then you have to add Service Reference so that you can access your web service. Here are
the screen-shots.
Here you have to give the URL of the web service we created earlier.
As I said before previously created web service application should be
running on another instant of Visual Studio.

Note that I have set the "Web reference name" as "TestWeb".


Now you can update your client program using following code. Note the line 5 "using
WebServiceTest.TestWeb;".
?

1
2
3 using System;
using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6 using WebServiceTest.TestWeb;
7 namespace WebServiceTest
8 {
9 class Program
{
10 static void Main(string[] args)
11 {
12 Service1 webservice = new Service1();
13 string srt = webservice.simpleMethod("Saranga Rathnayake");
14 Console.WriteLine(srt);
15 Console.WriteLine(webservice .anotherSimpleMethod(4,3));
}
16 }
17 }
18
19
Now you can run the client program to see the result.

3. Publish Our Web Service in Internet Information Service (IIS)


Let's see how we can publish our web service in IIS. Otherwise you always need to run your
web service application in separate VS instant. There first stop the web service application
and go to the Solution Explore and Right Click on the project. Then select "Publish...".

Then the following window will appear and there you can directly publish to the IIS by
selecting "Web Deploy" as the publishing method. But here I'm going to use the "File
System as the publishing method. There you have to provide the target location. I have
created new folder called "MyApp" in my D drive and selected it.

Now click "Publish" and check the "MyApp" folder. There you will be able to see
Service1.asmx file, Web.config file and bin folder which contains the DLL file has been
generated. Then copy this "MyApp" folder to "wwwroot" folder. You may find it in
"C:\inetpub\wwwroot".

Now enable IIS in your computer and open IIS Manager. I'm going to add my service to
Default Web Site. There Right Click on the "Default Web Site" and click "Add Application...".
There you will get following window. Now you can provide appropriate Alias (I have given
testservice) and select the physical path of your application. There you can provide the path
to the folder we copied previously as following figure and click Ok.

You have to make sure that the application pool identity has Read access to the physical
path. So it is better if you copy your files to the "wwwroot" folder other than keep it in
separate partition. Please check the following screen-shot

Now restart the IIS and goto http://localhost/testservice/Service1.asmx. You will be able to
see the Web Service running.
Now you have published your web service in IIS and you can update the Client Program by
giving the new Web Reference URL using Properties Window.

Anda mungkin juga menyukai