生成复杂参数类型的客户端代理
生成复杂参数类型的客户端代理
•使用GenerateScriptTypeAttribute标记要生成代理的参数类型
•可以标记在类、接口以及方法上
•生成的代理中将包括客户端类型的代理
•调用方法时可以创建“具体类型”
–new [Namespaces].ClassName()
–没有其他构造方法(构造函数)
aspx
Color.cs
ColorService.asmx
•使用GenerateScriptTypeAttribute标记要生成代理的参数类型
•可以标记在类、接口以及方法上
•生成的代理中将包括客户端类型的代理
•调用方法时可以创建“具体类型”
–new [Namespaces].ClassName()
–没有其他构造方法(构造函数)
aspx
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="ColorService.asmx" InlineScript="true" />
</Services>
</asp:ScriptManager>
<input type="button" value="Reverse Color" onclick="ReverseColor()" />
<script language="javascript" type="text/javascript">
function ReverseColor()
{
// var color = {"Red" : 50, "Green" : 100, "Blue" : 200};
var color = new ComplexType.Color();
color.Red = 50;
color.Green = 100;
color.Blue = 200;
ColorService.Reverse(color, onSucceeded);
}
function onSucceeded(result)
{
alert(String.format(
"Red: {0}\nGreen: {1}\nBlue: {2}",
result.Red,
result.Green,
result.Blue));
}
</script>
</form>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="ColorService.asmx" InlineScript="true" />
</Services>
</asp:ScriptManager>
<input type="button" value="Reverse Color" onclick="ReverseColor()" />
<script language="javascript" type="text/javascript">
function ReverseColor()
{
// var color = {"Red" : 50, "Green" : 100, "Blue" : 200};
var color = new ComplexType.Color();
color.Red = 50;
color.Green = 100;
color.Blue = 200;
ColorService.Reverse(color, onSucceeded);
}
function onSucceeded(result)
{
alert(String.format(
"Red: {0}\nGreen: {1}\nBlue: {2}",
result.Red,
result.Green,
result.Blue));
}
</script>
</form>
Color.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace ComplexType
{
/// <summary>
/// Summary description for Color
/// </summary>
public class Color
{
public Color() { }
public Color(byte red, byte green, byte blue)
{
this.Red = red;
this.Green = green;
this.Blue = blue;
}
public byte Red;
public byte Green;
public byte Blue;
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace ComplexType
{
/// <summary>
/// Summary description for Color
/// </summary>
public class Color
{
public Color() { }
public Color(byte red, byte green, byte blue)
{
this.Red = red;
this.Green = green;
this.Blue = blue;
}
public byte Red;
public byte Green;
public byte Blue;
}
}
ColorService.asmx
<%@ WebService Language="C#" Class="ColorService" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using ComplexType;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class ColorService : System.Web.Services.WebService
{
[WebMethod]
[GenerateScriptType(typeof(Color))]
public Color Reverse(Color color)
{
return new Color(
(byte)(255 - color.Red),
(byte)(255 - color.Green),
(byte)(255 - color.Blue));
}
}
注意这里的ColorService类方法Reverse设置了[GenerateScriptType(typeof(Color))]标记要生成代理的参数类型,这样才可以使用Color作为参数的类型
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using ComplexType;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class ColorService : System.Web.Services.WebService
{
[WebMethod]
[GenerateScriptType(typeof(Color))]
public Color Reverse(Color color)
{
return new Color(
(byte)(255 - color.Red),
(byte)(255 - color.Green),
(byte)(255 - color.Blue));
}
}