How to Access a Web Service in Console Application

Introduction

This article shows how to access a Web Service in a console application that exists in a web application. So I will do something like:

  • Create a web application and add a Web Service with some code to it.
  • Create a console application and add a reference of a Web Service to it.
  • Write some code in the console application for calling the web method of the Web Service and run it to see the output.

Use the following procedure to understand this in details.

Step 1: Create a Web application and add a Web Service with some code in it.

Create a web application named "WebApplication1".

Web Service in Console Application1

Create a Web Service named "Web Service 1" in the web application.

Web Service in Console Application2

Write a web method in the service that accepts 2 int values as input parameters and after addition of both values, return a string value that contains the addition.

[
WebMethod]
       
public string Addition(int value1, int value2)
        {
           
int result = value1 + value2;
           
return "Addition= "+ result.ToString();
        }


Web Service in Console Application3

Run the Web Service and note down the URL of the service.

Web Service in Console Application4

Step 2: Create a Console application and add a reference of the Web Service to it.

Add a new project after the right-click of the solution file then seelct "Add" -> "New Project".

Web Service in Console Application5

Note: You can also create a new project in a new solution file.

Create a console application named "ConsoleApplication1".

Web Service in Console Application6

Now you have 2 two projects in your solution file.

Web Service in Console Application7

Now add the reference of the Web Service to the console application and to do this, right-click on the console application and then click on "Add Service Reference...".

Web Service in Console Application8

Click on the "Advanced" button.

Web Service in Console Application9

Click on the "Add Web Reference..." button.

Web Service in Console Application10

Write the URL of the Web Service that you have earlier specified after running the Web Service, and click on the arrow (->) button.

Web Service in Console Application11

You can see both of the web methods of your Web Service. Before the add reference, you can change the web reference name as you prefer or need; here I used "MyService". Now click on "Add Refernece".

Web Service in Console Application12

Step 3: Write some code in the console application to call the web method of the Web Service and run to see the output.

Write the code using the following:

  • Create an object of your service that will be accessed by the web reference name. The Web Service name here is "MyService.WebService1" as in the following:

    MyService.
    WebService1 obj = new MyService.WebService1();
        
  • Read the 2 int values from the user as in the following:
        

      int value1 = Convert.ToInt32(Console.ReadLine());

      int value2 = Convert.ToInt32(Console.ReadLine());

  • Call the web method with 2 input parameters that gets from the user by the object of the Web Service and save it into a string variable named "result".

    string result = obj.Addition(value1, value2);

     
  • Print the result as in the following:

     Console.WriteLine(result);

Web Service in Console Application13

Set the console application project as the StartUp project by a right-click.

Web Service in Console Application14

Run the application and provide the 2 integer values, one by one like here:

Type "1" and press Enter.
Type "2" and press Enter.

See the results as in the following:

Web Service in Console Application15


Similar Articles