This article shows how to access a Web Service in a console application that exists in a web application.
IntroductionThis article shows how to access a Web Service in a console application that exists in a web application. So I will do something like:
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". Create a Web Service named "Web Service 1" in the web application.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(); }Run the Web Service and note down the URL of the service.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".Note: You can also create a new project in a new solution file.Create a console application named "ConsoleApplication1".Now you have 2 two projects in your solution file.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...". Click on the "Advanced" button.Click on the "Add Web Reference..." button.Write the URL of the Web Service that you have earlier specified after running the Web Service, and click on the arrow (->) button.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".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:
int value1 = Convert.ToInt32(Console.ReadLine());
int value2 = Convert.ToInt32(Console.ReadLine());
string result = obj.Addition(value1, value2);
Console.WriteLine(result);
Set the console application project as the StartUp project by a right-click.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:
Learn API Testing