Create Proxy for ASP.NET Web Service

 

Whenever you create the web service and before providing this web service to client we have to create proxy for that web service. We can create the proxy manually, but it is very hard to create. To make it easy Microsoft provided best tool called wsdl that generates the code for proxy based on information provided in WSDL file.

 

To create the proxy; Open Microsoft Visual Studio => Create Asp.Net Web Service application

 

Now enter below command in Visual Studio Command Prompt as shown below.

     

           wsdl http://localhost:9560/Service1.asmx?wsdl

 

 

By executing the above command, it creates client file called Service1.cs which has below code.

 

//------------------------------------------------------------------------------

// <auto-generated>

//     This code was generated by a tool.

//     Runtime Version:2.0.50727.5472

//

//     Changes to this file may cause incorrect behavior and will be lost if

//     the code is regenerated.

// </auto-generated>

//------------------------------------------------------------------------------

 

using System;

using System.ComponentModel;

using System.Diagnostics;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Xml.Serialization;

 

//

// This source code was auto-generated by wsdl, Version=2.0.50727.1432.

//

 

 

///<remarks/>

[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.1432")]

[System.Diagnostics.DebuggerStepThroughAttribute()]

[System.ComponentModel.DesignerCategoryAttribute("code")]

[System.Web.Services.WebServiceBindingAttribute(Name = "Service1Soap", Namespace = "http://tempuri.org/")]

public partial class Service1 : System.Web.Services.Protocols.SoapHttpClientProtocol

{

 

    private System.Threading.SendOrPostCallback TestProxyOperationCompleted;

 

    ///<remarks/>

    public Service1()

    {

        this.Url = "http://localhost:9560/Service1.asmx";

    }

 

    ///<remarks/>

    public event TestProxyCompletedEventHandler TestProxyCompleted;

 

    ///<remarks/>

    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/TestProxy", RequestNamespace = "http://tempuri.org/", ResponseNamespace = "http://tempuri.org/", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]

    public string TestProxy()

    {

        object[] results = this.Invoke("TestProxy", new object[0]);

        return ((string)(results[0]));

    }

 

    ///<remarks/>

    public System.IAsyncResult BeginTestProxy(System.AsyncCallback callback, object asyncState)

    {

        return this.BeginInvoke("TestProxy", new object[0], callback, asyncState);

    }

 

    ///<remarks/>

    public string EndTestProxy(System.IAsyncResult asyncResult)

    {

        object[] results = this.EndInvoke(asyncResult);

        return ((string)(results[0]));

    }

 

    ///<remarks/>

    public void TestProxyAsync()

    {

        this.TestProxyAsync(null);

    }

 

    ///<remarks/>

    public void TestProxyAsync(object userState)

    {

        if ((this.TestProxyOperationCompleted == null))

        {

            this.TestProxyOperationCompleted = new System.Threading.SendOrPostCallback(this.OnTestProxyOperationCompleted);

        }

        this.InvokeAsync("TestProxy", new object[0], this.TestProxyOperationCompleted, userState);

    }

 

    private void OnTestProxyOperationCompleted(object arg)

    {

        if ((this.TestProxyCompleted != null))

        {

            System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));

            this.TestProxyCompleted(this, new TestProxyCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));

        }

    }

 

    ///<remarks/>

    public new void CancelAsync(object userState)

    {

        base.CancelAsync(userState);

    }

}

 

///<remarks/>

[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.1432")]

public delegate void TestProxyCompletedEventHandler(object sender, TestProxyCompletedEventArgs e);

 

///<remarks/>

[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.1432")]

[System.Diagnostics.DebuggerStepThroughAttribute()]

[System.ComponentModel.DesignerCategoryAttribute("code")]

public partial class TestProxyCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs

{

 

    private object[] results;

 

    internal TestProxyCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :

        base(exception, cancelled, userState)

    {

        this.results = results;

    }

 

    ///<remarks/>

    public string Result

    {

        get

        {

            this.RaiseExceptionIfNecessary();

            return ((string)(this.results[0]));

        }

    }

}

 

You required this proxy whenever you are providing the web service to the client.

 

We have to know about the code in proxy class. The file starts by declaring the Service1 class that derives from the class SoapHttpClientProtocol, which occurs in the namespace called System.Web.Services.Protocols.

 

public partial class Service1 : System.Web.Services.Protocols.SoapHttpClientProtocol

 

To build the proxy, place the code generated by WSDL into a C# Library project in Visual Studio .NET and then build the project to generate a DLL. Be sure to write down the location of that DLL, as you will need it when you build the client application.