How to do it: Generate a proxy class through command-line tools and consume an ASP.NET Web Service

Code Samples
Table of Contents
Summary

Visual Studio solution-ASP.NET Web Service Client (WSDL.exe)

Description

The proxy client is generated at Windows command-line. The URL of the web service  is required:

Generate proxy client through at command line through WSDL.exe

 

Operations
File "Program.cs"
How to do it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASPNET
{
    class Program
    {
        static void Main(string[] args)
        {
            var _proxy = new ITestService();
            _proxy.Url = "http://localhost:49549/TestServiceImpl.asmx";
            var lvCategory = _proxy.getCategory(1);
        }
    }
}
File "proxy,cs"
How to do it
//------------------------------------------------------------------------------
// <auto-generated>
//     Este código foi gerado por uma ferramenta.
//     Versão de Tempo de Execução:4.0.30319.269
//
//     As alterações a este ficheiro poderão provocar um comportamento incorrecto e perder-se-ão se
//     o código for regenerado.
// </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=4.0.30319.1.
// 

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="ITestService", Namespace="http://www.training.com/")]
public partial class ITestService : System.Web.Services.Protocols.SoapHttpClientProtocol {

    private System.Threading.SendOrPostCallback getCategoryOperationCompleted;

    private System.Threading.SendOrPostCallback addCategoryOperationCompleted;

    private System.Threading.SendOrPostCallback getCategoriesOperationCompleted;

    /// <remarks/>
    public ITestService() {
        this.Url = "http://localhost:49549/TestServiceImpl.asmx";
    }

    /// <remarks/>
    public event getCategoryCompletedEventHandler getCategoryCompleted;

    /// <remarks/>
    public event addCategoryCompletedEventHandler addCategoryCompleted;

    /// <remarks/>
    public event getCategoriesCompletedEventHandler getCategoriesCompleted;

    /// <remarks/>
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://www.training.com/getCategory", RequestNamespace="http://www.training.com/", ResponseNamespace="http://www.training.com/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    [return: System.Xml.Serialization.XmlElementAttribute("Category", Namespace="www.training.com")]
    public Category getCategory(int pId) {
        object[] results = this.Invoke("getCategory", new object[] {
                    pId});
        return ((Category)(results[0]));
    }

    /// <remarks/>
    public System.IAsyncResult BegingetCategory(int pId, System.AsyncCallback callback, object asyncState) {
        return this.BeginInvoke("getCategory", new object[] {
                    pId}, callback, asyncState);
    }

    /// <remarks/>
    public Category EndgetCategory(System.IAsyncResult asyncResult) {
        object[] results = this.EndInvoke(asyncResult);
        return ((Category)(results[0]));
    }

    /// <remarks/>
    public void getCategoryAsync(int pId) {
        this.getCategoryAsync(pId, null);
    }

    /// <remarks/>
    public void getCategoryAsync(int pId, object userState) {
        if ((this.getCategoryOperationCompleted == null)) {
            this.getCategoryOperationCompleted = new System.Threading.SendOrPostCallback(this.OngetCategoryOperationCompleted);
        }
        this.InvokeAsync("getCategory", new object[] {
                    pId}, this.getCategoryOperationCompleted, userState);
    }

    private void OngetCategoryOperationCompleted(object arg) {
        if ((this.getCategoryCompleted != null)) {
            System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
            this.getCategoryCompleted(this, new getCategoryCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
        }
    }

    /// <remarks/>
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://www.training.com/addCategory", RequestNamespace="http://www.training.com/", ResponseNamespace="http://www.training.com/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public int addCategory([System.Xml.Serialization.XmlElementAttribute(Namespace="www.training.com")] Category Category) {
        object[] results = this.Invoke("addCategory", new object[] {
                    Category});
        return ((int)(results[0]));
    }

    /// <remarks/>
    public System.IAsyncResult BeginaddCategory(Category Category, System.AsyncCallback callback, object asyncState) {
        return this.BeginInvoke("addCategory", new object[] {
                    Category}, callback, asyncState);
    }

    /// <remarks/>
    public int EndaddCategory(System.IAsyncResult asyncResult) {
        object[] results = this.EndInvoke(asyncResult);
        return ((int)(results[0]));
    }

    /// <remarks/>
    public void addCategoryAsync(Category Category) {
        this.addCategoryAsync(Category, null);
    }

    /// <remarks/>
    public void addCategoryAsync(Category Category, object userState) {
        if ((this.addCategoryOperationCompleted == null)) {
            this.addCategoryOperationCompleted = new System.Threading.SendOrPostCallback(this.OnaddCategoryOperationCompleted);
        }
        this.InvokeAsync("addCategory", new object[] {
                    Category}, this.addCategoryOperationCompleted, userState);
    }

    private void OnaddCategoryOperationCompleted(object arg) {
        if ((this.addCategoryCompleted != null)) {
            System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
            this.addCategoryCompleted(this, new addCategoryCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
        }
    }

    /// <remarks/>
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://www.training.com/getCategories", RequestNamespace="http://www.training.com/", ResponseNamespace="http://www.training.com/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public Category[] getCategories() {
        object[] results = this.Invoke("getCategories", new object[0]);
        return ((Category[])(results[0]));
    }

    /// <remarks/>
    public System.IAsyncResult BegingetCategories(System.AsyncCallback callback, object asyncState) {
        return this.BeginInvoke("getCategories", new object[0], callback, asyncState);
    }

    /// <remarks/>
    public Category[] EndgetCategories(System.IAsyncResult asyncResult) {
        object[] results = this.EndInvoke(asyncResult);
        return ((Category[])(results[0]));
    }

    /// <remarks/>
    public void getCategoriesAsync() {
        this.getCategoriesAsync(null);
    }

    /// <remarks/>
    public void getCategoriesAsync(object userState) {
        if ((this.getCategoriesOperationCompleted == null)) {
            this.getCategoriesOperationCompleted = new System.Threading.SendOrPostCallback(this.OngetCategoriesOperationCompleted);
        }
        this.InvokeAsync("getCategories", new object[0], this.getCategoriesOperationCompleted, userState);
    }

    private void OngetCategoriesOperationCompleted(object arg) {
        if ((this.getCategoriesCompleted != null)) {
            System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
            this.getCategoriesCompleted(this, new getCategoriesCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
        }
    }

    /// <remarks/>
    public new void CancelAsync(object userState) {
        base.CancelAsync(userState);
    }
}

/// <remarks/>
// CODEGEN: Não foram encontrados métodos no WSDL deste protocolo.
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="TestServiceImplSoap", Namespace="http://www.training.com/")]
public partial class TestServiceImplSoap : System.Web.Services.Protocols.SoapHttpClientProtocol {

    /// <remarks/>
    public TestServiceImplSoap() {
        this.Url = "http://localhost:49549/TestServiceImpl.asmx";
    }
}

/// <observações/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="www.training.com")]
public partial class Category {

    private int idField;

    private string nameField;

    private string descriptionField;

    /// <observações/>
    public int Id {
        get {
            return this.idField;
        }
        set {
            this.idField = value;
        }
    }

    /// <observações/>
    public string Name {
        get {
            return this.nameField;
        }
        set {
            this.nameField = value;
        }
    }

    /// <observações/>
    public string Description {
        get {
            return this.descriptionField;
        }
        set {
            this.descriptionField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
public delegate void getCategoryCompletedEventHandler(object sender, getCategoryCompletedEventArgs e);

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class getCategoryCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {

    private object[] results;

    internal getCategoryCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :
            base(exception, cancelled, userState) {
        this.results = results;
    }

    /// <remarks/>
    public Category Result {
        get {
            this.RaiseExceptionIfNecessary();
            return ((Category)(this.results[0]));
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
public delegate void addCategoryCompletedEventHandler(object sender, addCategoryCompletedEventArgs e);

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class addCategoryCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {

    private object[] results;

    internal addCategoryCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :
            base(exception, cancelled, userState) {
        this.results = results;
    }

    /// <remarks/>
    public int Result {
        get {
            this.RaiseExceptionIfNecessary();
            return ((int)(this.results[0]));
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
public delegate void getCategoriesCompletedEventHandler(object sender, getCategoriesCompletedEventArgs e);

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class getCategoriesCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {

    private object[] results;

    internal getCategoriesCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :
            base(exception, cancelled, userState) {
        this.results = results;
    }

    /// <remarks/>
    public Category[] Result {
        get {
            this.RaiseExceptionIfNecessary();
            return ((Category[])(this.results[0]));
        }
    }
}