Creating a Web Service in .NET

Creating a web service:

  • Open Visual Studio 2010
  • Click File > New Project
  • Under Visual C# tab, select ASP .NET Web Application
  • Right Click WebApplication (in solution Explorer) and click on Add > New Item

t-2

  • Scroll down until you find the option “Web Service” and select it
  • A function “HelloWorld” will already be present in the code that appears. Here we can add our own code. Eg: Matrix Addition function, Addition of 2 numbers etc.
  • Execute the program
  • The output will be similar to the below image.

t-3

Creating a client as console application:

  • Open another Visual Studio 2010 window
  • Click File > New Project
  • Select the Console Application

t-4

  • Right Click ConsoleApplication in the Solution Explorer and select Add Service Reference

t-5

  • Paste the Web service URL (Previously Noted) in the address field and click on Go. Select the web service that appears below and click OK. Note that the Namespace field denotes the name of the service reference which is being added.

6

  • Add the following code in Main() function to access the web service

ServiceReference1.WebService1SoapClient obj = new ServiceReference1.WebService1SoapClient();

Console.WriteLine(obj.HelloWorld());

Console.ReadLine();

t-6

Creating a client as a Windows Forms Application:

  • Open another Visual Studio 2010 window
  • Click File >New Project
  • Select Windows Forms Application
  • When the Form.cs window appears, click on toolbox at the left hand side of the window. Insert a text box and a button on the form.

t-1

  • Double Click the button so that the code window appears. Add a service reference to the WindowsFormsApplication.

7

  • Paste the Web service URL (Previously Noted) in the address field and click on Go. Select the web service that appears below and click OK. Note that the Namespace field denotes the name of the service reference which is being added.

8

  • Add the following code in the button_click function to print “hello world” in the text box.

ServiceReference1.WebService1SoapClient obj = new ServiceReference1.WebService1SoapClient();

textBox1.Text = obj.HelloWorld();

  • Execute the program. Click the button to print the output in the text box.

 

Advertisements

2 thoughts on “Creating a Web Service in .NET

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s