Web Service in ASP.NET: Part 3

In this article you will learn how to consume a web service in a C# Windows Application by Proxy class without using the URL of the Web Service.

Before reading this article please read Web Service in ASP.NET: Part 1 and Web Service in ASP.NET: Part 2 first where we can learn how to create a web service.

Now let's see how to consume a web service in a C# Windows application using a Proxy class DLL without an URL of a “.asmx” file.

Step 1

Copy the URL of your web service file from the browser.

copy url

Step 2

Search the Visual Studio Command Prompt from the Start menu.

search

Step 3

Open the Visual Studio Command Prompt and create a directory on the specified location where you want to save your Proxy .cs/.vb file.

save your proxy

Step 4

After creating the directory we will write a Proxy class by “wsdl” command on a specified location. Just write wsdl and paste your URL that was copied from the web browser of the web service file .asmx and press Enter to create it.

web service file

It will create a Proxy class on the selected location.

Step 5

Now we'll create a new project in Visual Studio for creating a DLL file of this proxy class and then we can directly use this DLL file without adding a reference as a service and it also provides security to my web methods in a non-editable format.

Open Visual Studio then seelct "File" -> "New" -> "Project..." then select Class Library.

class liberary

Step 6

Open Solution Explorer and delete the existing Class file and the Proxy class will be created by command prompt with the WSLD command.

delet class

Now right-click on the project file again and select "Add" -> "Existing Item..." then find the Proxy class from that specified location.

existing item

After adding the source code file in My Solution we can see the proxy class file.

MyServiceClass

Check the code of MyServiceClass.cs.

cs code


But there are some errors attempting to find the namespace “using System.Web.Services; ” because there is a reference of the System.Web.Services.dll file so before building add a reference for this DLL in the .csproj file.

add reference

Find the DLL file System.Web.Services.dll and press OK.

System Web Services

Now build your project file again and see the output in the output window.

test web service

The Proxy class DLL file is now ready to use.

Step 7

Now create a Windows or ASP.Net web application to test this DLL file.

I wil add a new project in the same solution. Alternatively you can create a new solution.
Right-click on the solution file and select Add New Project.

Add New Project

Press OK.

Step 8

Now add a reference for the DLL file in the application project.

application project

Find by Browse DLL then add a reference and also the System.Web.Services.dll in this project.

Browse Dll and add reference

Step 9

Place a Button Object on the form to call the web service class method on Button click.

call method form

Create the object of the web service class on the button click code handler.

button click code handler

Now run your application to test the method and click the button.

test the method n click

Your output will be in the Message Box as in the code.

Thanks.