Freigeben über


Generated Proxy Classes

Enables your application to call Web services asynchronously by using ECMAScript (JavaScript).

Namespace: Sys.Net

Inherits: None

Constructors

Name

Description

Generated Proxy Classes Constructor

Creates a new instance of the generated proxy class.

Members

Name

Description

Generated Proxy Classes Web Service Methods

Invoke the corresponding Web service methods.

Generated Proxy Classes defaultFailedCallback Property

Gets or sets the default failed callback function for the generated proxy class and its instances.

Generated Proxy Classes defaultSucceededCallback Property

Gets or sets the default succeeded callback function for the generated proxy class and its instances.

Generated Proxy Classes defaultUserContext Property

Gets or sets the default user context for the generated proxy class and its instances.

Generated Proxy Classes path Property

Gets and sets the Web service URL.

Generated Proxy Classes timeout Property

Gets or sets the time-out interval for the generated proxy class and its instances.

Generated Proxy Classes Failed Callback Function

Processes error information after a Web service method request has failed.

Generated Proxy Classes Succeeded Callback Function

Processes returned data after the Web service method request returns successfully.

Remarks

To invoke a method of a Web service, you call the corresponding method of the generated Web service proxy class. The generated proxy class executes a Web request to the Web service method.

Note

For AJAX-enabled ASP.NET applications to call Web services from script, the application must be configured to register the required ASP.NET HTTP handler. For more information, see Exposing Web Services to Clients.

The Web service that you are calling must be an .asmx file and must be qualified with the ScriptServiceAttribute attribute. Individual methods to be called from script must be qualified with the WebMethodAttribute attribute. Methods without this attribute are not exposed in the proxy class. For more information, see Exposing Web Services to Client Script in ASP.NET AJAX and Calling Web Services from Client Script in ASP.NET AJAX.

The proxy class is automatically generated when the ASP.NET Web page is rendered and is derived from the Sys.Net.WebServiceProxy class. The path property of the generated class references the Web service URL.

In your application, you can call the generated proxy class members. Alternatively, you can create instances of the generated proxy class. You can specify a succeeded callback function, a failed callback function, and, optionally, time-out value and user context as default properties for each instance. You can then use these proxy class instances to call Web service methods.

Example

The following code example shows how to use a Web service generated proxy class. The example shows a Web page, a client script, and a Web service called by the page through the client script.

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="https://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">

        <title>Using Generated Web Service Proxy Class</title>
        <style type="text/css">
            body {  font: 11pt Trebuchet MS;
                    font-color: #000000;
                    padding-top: 72px;
                    text-align: center }
           .text { font: 10pt Trebuchet MS; text-align: center }
        </style>    
    </head>

    <body>

        <h2>Using Generated Web Service Proxy Class</h2>

        <form id="form1" runat="server">

            <asp:ScriptManager runat="server" ID="scriptManager">
                <Services>
                    <asp:ServiceReference Path="UsingProxyClass.asmx" />
                </Services>
                <Scripts>
                    <asp:ScriptReference Path="UsingProxyClass.js" />
                </Scripts>
            </asp:ScriptManager>  

        </form>


        <center>
            <table style="font-size:12px;" >

                 <tr align="left">
                    <td class="text">Get Server Object:</td>
                    <td>
                     <button id="Button3"  
                        onclick="GetDefaultColor()">Get Default Color</button>
                    </td>
                </tr>

                 <tr align="left">
                    <td class="text">Pass Server Object:</td>
                    <td>
                     <button id="Button4"  
                        onclick="SetColor()">Set Color</button>
                    </td>
                </tr>

            </table> 

        </center>

        <hr />

        <!-- Display current color object. -->
        <span id="ResultId"></span>

    </body>

</html>
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="https://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">

        <title>Using Generated Web Service Proxy Class</title>
        <style type="text/css">
            body {  font: 11pt Trebuchet MS;
                    font-color: #000000;
                    padding-top: 72px;
                    text-align: center }
           .text { font: 10pt Trebuchet MS; text-align: center }
        </style>    
    </head>

    <body>

        <h2>Using Generated Web Service Proxy Class</h2>

        <form id="form1" runat="server">

            <asp:ScriptManager runat="server" ID="scriptManager">
                <Services>
                    <asp:ServiceReference Path="UsingProxyClass.asmx" />
                </Services>
                <Scripts>
                    <asp:ScriptReference Path="UsingProxyClass.js" />
                </Scripts>
            </asp:ScriptManager>  

        </form>


        <center>
            <table style="font-size:12px;" >

                 <tr align="left">
                    <td class="text">Get Server Object:</td>
                    <td>
                     <button id="Button3"  
                        onclick="GetDefaultColor()">Get Default Color</button>
                    </td>
                </tr>

                 <tr align="left">
                    <td class="text">Pass Server Object:</td>
                    <td>
                     <button id="Button4"  
                        onclick="SetColor()">Set Color</button>
                    </td>
                </tr>

            </table> 

        </center>

        <hr />

        <!-- Display current color object. -->
        <span id="ResultId"></span>

    </body>

</html>
// The Web service default color.
var defaultRgb;

// The proxy class instance.
var proxyInstance;

// The page feedback display element.
var displayResult;

// This function intializes the global variables and
// assigns default values to the generated proxy.
function pageLoad() 
{
    // Get page feedback display element.
    displayResult = 
        document.getElementById("ResultId");

    // Assign default values to the generated proxy.
    Samples.AspNet.UsingProxyClass.set_timeout(200);
    Samples.AspNet.UsingProxyClass.set_defaultUserContext("Default context");
    Samples.AspNet.UsingProxyClass.set_defaultSucceededCallback(SucceededCallback);
    Samples.AspNet.UsingProxyClass.set_defaultFailedCallback(FailedCallback);
}

// This function shows how to get 
// a server object.
function GetDefaultColor()
{
    // Gets the default color obiect.
    Samples.AspNet.UsingProxyClass.GetDefaultColor();  

}

// This function shows how to instantiate
// the proxy class to assign its default values.
function SetColor()
{
    // Instantiate a color object.
    var color = 
        new Samples.AspNet.ColorObject();

    // Define a color array (blue).
    var colorArray = new Array("00", "00", "FF");

    // Assign the new values to the server color object.
    color.message = "The new color is Blue";
    color.rgb = colorArray;


    // Assign default values for the generated proxy using
    // a proxy instance.
    proxyInstance = new  Samples.AspNet.UsingProxyClass();
    proxyInstance.set_timeout(1000);
    proxyInstance.set_defaultUserContext("New context");
    proxyInstance.set_defaultSucceededCallback(SucceededCallback);
    proxyInstance.set_defaultFailedCallback(FailedCallback);

    // Set the default color object.
    proxyInstance.SetColor(color);  
}

// Callback function invoked when the call to 
// the Web service methods succeeds.
function SucceededCallback(result, userContext, methodName)
{ 
    var message;
    switch(methodName)
    {
        case ("GetDefaultColor"):
        case ("SetColor"):
        {
            // Get the server default color.
            message = result.message;
            defaultRgb = result.rgb;


            // Transform the rgb array into a string.
            var serverColor = defaultRgb[0]+ defaultRgb[1] + defaultRgb[2];

            // Display the result.
            displayResult.style.color = "yellow";
            displayResult.style.fontWeight = "bold";
            displayResult.style.backgroundColor = "#" + serverColor;
            DisplayMessage(message);
            break;
        }
        default:
        {
            DisplayMessage("Method unknown");
        }
    }       
}

// Callback function invoked when the call to 
// the Web service methods fails.
function FailedCallback(error, userContext, methodName) 
{
    if(error !== null) 
    {
        displayResult.innerHTML = "An error occurred: " + 
            error.get_message();
    }
}

function DisplayMessage(message)
{
    if (document.all) 
        displayResult.innerText = message;
    else
        // Firefox
        displayResult.textContent = message;    
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
<%@ WebService Language="C#" Class="Samples.AspNet.UsingProxyClass" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using System.Collections.Generic;

namespace Samples.AspNet
{
    // Define the color type to
    // exchange with the client.
    public class ColorObject
    {
        public string message;
        public string[] rgb; 

        public ColorObject()
        {
            this.message = "The default color is Red";
            this.rgb = new string[] { "FF", "00", "00" };
        }
    }

    [WebService(Namespace = "https://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService]
    public class UsingProxyClass : 
        System.Web.Services.WebService
    {


        // Note, because the ColorObject is the returned type
        // it does not require that you apply
        // the attribute [GenerateScriptType(typeof(ColorObject))]
        // to the service class to allow client script
        // access.
        [WebMethod]
        public ColorObject GetDefaultColor()
        {
            // Instantiate the default color object.
            ColorObject co = new ColorObject();
            return co;
        }

        [WebMethod]
        public ColorObject SetColor(ColorObject color)
        {
            // Instantiate the color object.
            ColorObject co = new ColorObject();

            // Assign the passed values.
            co.message = color.message;
            co.rgb = color.rgb;

            return co;
        }
    }

}

See Also

Concepts

Sys.Net.WebServiceProxy Class

Other Resources

Exposing Web Services to Client Script

Calling Web Services from Client Script