Web 服务方法可能支持泛型或 T 类型列表数组类型的参数或返回值。在这种情况下,ASP.NET 自动生成 T 类型的代理类以便用于客户端脚本。但是,如果泛型类型接受多个类型参数(例如 Dictionary<string, <T>>),则 ASP.NET 不会自动生成这些类型的代理类。为了使 ASP.NET 能够生成 T 类型的代理类,必须使用 T 类型的 GenerateScriptTypeAttribute 属性限定使用该类型的 Web 服务类。下面的示例演示当参数的类型为泛型或数组时,如何从客户端脚本调用 Web 服务方法。该示例的第一部分演示一个网页,该网页使用客户端脚本调用 Web 服务<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server"> <title>Using Generics Proxy Types</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 Generics Proxy Types</h2>
<form id="form1" runat="server"> <asp:ScriptManager runat="server" ID="scriptManager">
<Services>
<asp:ServiceReference Path="WebService.asmx" />
<asp:ServiceReference Path="WebService2.asmx" />
</Services>
<Scripts>
<asp:ScriptReference Path="generics.js" />
</Scripts>
</asp:ScriptManager> </form>
<center>
<table style="font-size:12px;" > <tr align="left">
<td class="text">Generic List:</td>
<td>
<button id="Button1"
onclick="GenericList()">Get List</button>
</td>
</tr> <tr align="left">
<td class="text">Generic Dictionary:</td>
<td>
<button id="Button2"
onclick="GenericDictionary()">Get Dictionary</button>
</td>
</tr> <tr align="left">
<td class="text">Generic Custom Type Dictionary:</td>
<td>
<button id="Button3"
onclick="GenericCustomTypeDictionary()">Get Dictionary</button>
</td>
</tr>
<tr align="left">
<td class="text">Generic Dictionary:</td>
<td>
<button id="Button5"
onclick="PassGenericDictionary()">Pass Dictionary</button>
</td>
</tr> <tr align="left">
<td class="text">Array:</td>
<td>
<button id="Button4"
onclick="ArrayType()">Get Array</button>
</td>
</tr> </table>
</center> <hr /> <!-- Display current color object. -->
<span id="ResultId"></span> </body></html>该示例的下一部分演示网页用于调用 Web 服务的客户端脚本。// 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.TestService.set_defaultUserContext("Default context");
Samples.AspNet.TestService.set_defaultSucceededCallback(SucceededCallback);
Samples.AspNet.TestService.set_defaultFailedCallback(FailedCallback);
} // Get a generic List.
function GenericList()
{
Samples.AspNet.TestService.GetGenericList();
} // Get a generic Dictionary.
function GenericDictionary()
{
Samples.AspNet.TestService.GetGenericDictionary();
} // Get a generic Dictionary of custom types.
function GenericCustomTypeDictionary()
{
Samples.AspNet.TestService.GetGenericCustomTypeDictionary();
}
// Pass a generic dictionary of custom types
// to Webservice.
function PassGenericDictionary()
{ var simple = new Samples.AspNet.SimpleClass2();
simple.s = "WebService proxy."; Samples.AspNet.TestService.PassGenericDictionary(
{"first":simple});
} // Get an Array.
function ArrayType()
{
Samples.AspNet.TestService.GetArray();
}
// Callback function invoked when the call to
// the Web service methods succeeds.
function SucceededCallback(result, userContext, methodName)
{
var message;
switch(methodName)
{
case ("GetGenericList"):
{
var i = 0;
var message = new Array();
for(var item in result)
{
message[i] = "List element " + i + ": " + result[item].s;
i++;
}
DisplayMessage(message.toString());
break;
}
case ("GetGenericDictionary"):
{
var i = 0;
var message = new Array();
for(var item in result)
{
message[i] = item + ": " + result[item];
i++;
}
DisplayMessage(message.toString());
break;
} case ("GetGenericCustomTypeDictionary"):
{
var i = 0;
var message = new Array();
for(var item in result)
{
message[i] = item + ": " + result[item].s;
i++;
}
DisplayMessage(message.toString());
break;
} case ("PassGenericDictionary"):
{ DisplayMessage(result); break;
} case ("GetArray"):
{
var i = 0;
var message = new Array();
for(var item in result)
{
message[i] = result[item];
i++;
}
DisplayMessage(message.toString());
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();
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server"> <title>Using Generics Proxy Types</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 Generics Proxy Types</h2>
<form id="form1" runat="server"> <asp:ScriptManager runat="server" ID="scriptManager">
<Services>
<asp:ServiceReference Path="WebService.asmx" />
<asp:ServiceReference Path="WebService2.asmx" />
</Services>
<Scripts>
<asp:ScriptReference Path="generics.js" />
</Scripts>
</asp:ScriptManager> </form>
<center>
<table style="font-size:12px;" > <tr align="left">
<td class="text">Generic List:</td>
<td>
<button id="Button1"
onclick="GenericList()">Get List</button>
</td>
</tr> <tr align="left">
<td class="text">Generic Dictionary:</td>
<td>
<button id="Button2"
onclick="GenericDictionary()">Get Dictionary</button>
</td>
</tr> <tr align="left">
<td class="text">Generic Custom Type Dictionary:</td>
<td>
<button id="Button3"
onclick="GenericCustomTypeDictionary()">Get Dictionary</button>
</td>
</tr>
<tr align="left">
<td class="text">Generic Dictionary:</td>
<td>
<button id="Button5"
onclick="PassGenericDictionary()">Pass Dictionary</button>
</td>
</tr> <tr align="left">
<td class="text">Array:</td>
<td>
<button id="Button4"
onclick="ArrayType()">Get Array</button>
</td>
</tr> </table>
</center> <hr /> <!-- Display current color object. -->
<span id="ResultId"></span> </body></html>该示例的下一部分演示网页用于调用 Web 服务的客户端脚本。// 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.TestService.set_defaultUserContext("Default context");
Samples.AspNet.TestService.set_defaultSucceededCallback(SucceededCallback);
Samples.AspNet.TestService.set_defaultFailedCallback(FailedCallback);
} // Get a generic List.
function GenericList()
{
Samples.AspNet.TestService.GetGenericList();
} // Get a generic Dictionary.
function GenericDictionary()
{
Samples.AspNet.TestService.GetGenericDictionary();
} // Get a generic Dictionary of custom types.
function GenericCustomTypeDictionary()
{
Samples.AspNet.TestService.GetGenericCustomTypeDictionary();
}
// Pass a generic dictionary of custom types
// to Webservice.
function PassGenericDictionary()
{ var simple = new Samples.AspNet.SimpleClass2();
simple.s = "WebService proxy."; Samples.AspNet.TestService.PassGenericDictionary(
{"first":simple});
} // Get an Array.
function ArrayType()
{
Samples.AspNet.TestService.GetArray();
}
// Callback function invoked when the call to
// the Web service methods succeeds.
function SucceededCallback(result, userContext, methodName)
{
var message;
switch(methodName)
{
case ("GetGenericList"):
{
var i = 0;
var message = new Array();
for(var item in result)
{
message[i] = "List element " + i + ": " + result[item].s;
i++;
}
DisplayMessage(message.toString());
break;
}
case ("GetGenericDictionary"):
{
var i = 0;
var message = new Array();
for(var item in result)
{
message[i] = item + ": " + result[item];
i++;
}
DisplayMessage(message.toString());
break;
} case ("GetGenericCustomTypeDictionary"):
{
var i = 0;
var message = new Array();
for(var item in result)
{
message[i] = item + ": " + result[item].s;
i++;
}
DisplayMessage(message.toString());
break;
} case ("PassGenericDictionary"):
{ DisplayMessage(result); break;
} case ("GetArray"):
{
var i = 0;
var message = new Array();
for(var item in result)
{
message[i] = result[item];
i++;
}
DisplayMessage(message.toString());
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();
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Script.Services; // Custom type used by WebService.
public class SimpleClass
{
private string _s = "SimpleClass1";
public String s {
get { return _s; }
set { _s = value; }
}
} // Custom type used by WebService.
public class SimpleClass2
{
private string _s = "SimpleClass2"; public String s
{
get { return _s; }
set { _s = value; }
}
} // The following service allows the generation
// of the SimpleClass2 type proxy by applying the
// attribute: GenerateScriptType. This assures
// that the a SimpleClass2 object can be
// passed by the client script to the
// PassGenericDictionary method.
// Note if you comment out the GenerateScriptType
// you get an error because the SimpleClass2 type proxy
// is not generated.
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[GenerateScriptType(typeof(SimpleClass2))]
[ScriptService]
public class TestService : System.Web.Services.WebService
{
// The following method return a list of SimpleClass
// objects.
// Note the SimpleClass type proxy is automatically
// generated because used in the return parameter.
[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public List<SimpleClass> GetGenericList()
{
List<SimpleClass> GetGenericList = new List<SimpleClass>();
SimpleClass simple1 = new SimpleClass();
SimpleClass simple2 = new SimpleClass();
simple1.s = "Generics first instance";
simple2.s = "Generics second instance";
GetGenericList.Add(simple1);
GetGenericList.Add(simple2);
return GetGenericList;
} // The following method return a Dictionary of strings.
[WebMethod]
public Dictionary<String, String> GetGenericDictionary()
{
//Instantiate the dictionary object.
Dictionary<String, String> result =
new Dictionary<string, string>(); //Add items.
result.Add("0000FF", "Blue");
result.Add("FF0000", "Red");
result.Add("00FF00", "Green");
result.Add("000000", "Black"); return result;
} // The following method return a Dictionary of
// SimpleClass objects.
// Note the SimpleClass type proxy object is automatically
// generated because used in the return parameter.
[WebMethod]
public Dictionary<String, SimpleClass> GetGenericCustomTypeDictionary()
{
//Instantiate the dictionary object.
Dictionary<String, SimpleClass> result =
new Dictionary<string, SimpleClass>(); SimpleClass simple = new SimpleClass();
simple.s = "custom type instance"; //Add items.
result.Add("Custom type", simple); return result;
} // The following method accept a Dictionary of
// SimpleClass2 objects.
// Note the SimpleClass2 type proxy object is
// not automatically generated because used in
// an input parameter. You must enable its
// generation by applying the GenerateScriptType
// attribute to the service.
[WebMethod]
public String PassGenericDictionary(
Dictionary<string, SimpleClass2> d)
{ string value = d["first"].s; string message = "Dictionary element value: " + value; return message;
}
// This function returns an array.
[WebMethod]
public ArrayList GetArray()
{
//Instantiate the array object.
ArrayList result = new ArrayList(); //Add items.
result.Add("First element: " + "Test1");
result.Add("Second element: " + "Test2"); return result;
}
}}