生成されたプロキシ クラス

更新 : 2007 年 11 月

アプリケーションで、ECMAScript (JavaScript) を使用して Web サービスを非同期に呼び出すことができます。

名前空間 : Sys.Net

継承 : なし

コンストラクタ

名前

説明

生成されたプロキシ クラス コンストラクタ

生成されたプロキシ クラスの新しいインスタンスを作成します。

メンバ

名前

説明

生成されたプロキシ クラスの Web サービス メソッド

対応する Web サービス メソッドを呼び出します。

生成されたプロキシ クラスの defaultFailedCallback プロパティ

生成されたプロキシ クラスとそのインスタンスの既定の失敗コールバック関数を取得または設定します。

生成されたプロキシ クラスの defaultSucceededCallback プロパティ

生成されたプロキシ クラスとそのインスタンスの既定の成功コールバック関数を取得または設定します。

生成されたプロキシ クラスの defaultUserContext プロパティ

生成されたプロキシ クラスとそのインスタンスの既定のユーザー コンテキストを取得または設定します。

生成されたプロキシ クラスの path プロパティ

Web サービスの URL を取得および設定します。

生成されたプロキシ クラスの timeout プロパティ

生成されたプロキシ クラスとそのインスタンスのタイムアウト間隔を取得または設定します。

生成されたプロキシ クラスの失敗コールバック関数

Web サービス メソッド要求が失敗した後にエラー情報を処理します。

生成されたプロキシ クラスの成功コールバック関数

Web サービス メソッド要求が正常に完了した後、返されたデータを処理します。

解説

Web サービスのメソッドを呼び出すには、生成された Web サービス プロキシ クラスの対応するメソッドを呼び出します。生成されたプロキシ クラスが Web 要求を Web サービス メソッドに対して実行します。

Bb310823.alert_note(ja-jp,VS.90).gifメモ :

AJAX 対応の ASP.NET アプリケーションが Web サービスをスクリプトから呼び出すには、必要な ASP.NET HTTP ハンドラを登録するようアプリケーションを設定しておく必要があります。詳細については、「クライアント スクリプトへの Web サービスの公開」を参照してください。

呼び出す Web サービスは、.asmx ファイルであり、ScriptServiceAttribute 属性で修飾されている必要があります。スクリプトから呼び出される個々のメソッドは、WebMethodAttribute 属性で修飾されたメソッドである必要があります。この属性を持たないメソッドは、プロキシ クラスで公開されます。詳細については、「クライアント スクリプトへの Web サービスの公開」および「クライアント スクリプトからの Web サービスの呼び出し」を参照してください。

プロキシ クラスは、ASP.NET Web ページが描画されたときに自動的に作成され、Sys.Net.WebServiceProxy クラスから派生されます。生成されたクラスの path プロパティは、Web サービスの URL を参照します。

アプリケーションでは、生成されたプロキシ クラス メンバを呼び出すことができます。代わりに、生成されたプロキシ クラスのインスタンスを作成することもできます。各インスタンスの既定のプロパティとして、成功コールバック関数、失敗コールバック関数、およびオプションでタイムアウト値とユーザー コンテキストを指定できます。その上で、プロキシ クラス インスタンスを使用し、Web サービス メソッドを呼び出すことができます。


Web サービスで生成されたプロキシ クラスを使用する方法を次のコード例に示します。この例には、Web ページ、クライアント スクリプト、およびページからクライアント スクリプトを介して呼び出される Web サービスが示されています。

Visual Basic
<%@ Page Language="VB" %>

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


<html  >
    <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>
C#
<%@ Page Language="C#" %>

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


<html  >
    <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>
PH
// 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();
C#
<%@ 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 = "http://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;
        }
    }

}
参照

概念

タグ :


Page view tracker