asp.net 创建 xml web service 设计指南
2006-07-24 23:16
274 查看
XML Web services 是一个强大的技术,用来提供可以从整个 Internet 以编程方式进行访问的服务。下面的建议将帮助您创建专业的 XML Web services:
XML Web services 支持客户端和承载该 XML Web services 的服务器之间的同步和异步通信。在同步通信情况下,客户端向服务主机服务器发送对服务的请求并等待响应。这阻止客户端在等待结果时执行其他操作。但是,异步通信让客户端在等待响应时继续处理其他任务。客户端在它可用时才响应服务请求的结果。
当您使用 Web 服务描述语言工具 (Wsdl.exe) 创建代理类时,它在该类中生成方法标准的同步版本和异步版本。异步版本由两个方法组成,分别叫做 Begin 和 End。Begin 方法用于启动 XML Web services,而 End 方法检索结果。
使用异步通信提高了系统利用率,避免当客户端等待 XML Web services 结果时在客户端上造成延迟。
下面的代码示例演示如何从客户端应用程序对 XML Web services 进行异步调用。
[C#]
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net" %>
<html>
<script language="C#" runat="server">
void EnterBtn_Click(Object Src, EventArgs E)
{
MyMath.Math math = new MyMath.Math();
// Call to Add XML Web service method asynchronously
// and then wait for it to complete.
IAsyncResult result =
math.BeginAdd(Convert.ToInt32(Num1.Text),
Convert.ToInt32(Num2.Text),
null,
null);
// Wait for asynchronous call to complete.
result.AsyncWaitHandle.WaitOne();
// Complete the asynchronous call to Add XML Web service method.
float total = math.EndAdd(result);
// Display results in a Label control.
Total.Text = "Total: " + total.ToString();
}
</script>
<body>
<form action="MathClient.aspx" runat=server>
<font face="Verdana">
Enter the two numbers you want to add and then press
the Total button.
<p>
Number 1:
<asp:textbox id="Num1"
runat=server/>
+
Number 2:
<asp:textbox id="Num2"
runat=server/>
=
<asp:button id="Total_Button"
text="Total"
OnClick="EnterBtn_Click"
runat=server/>
<p>
<asp:label id="Total" runat=server/>
</font>
</form>
</body>
</html>
通过 Internet 进行大量的服务请求可能影响客户端应用程序的性能。当设计您的 XML Web services 时,通过创建将相关的信息聚集在一起的方法对服务请求进行有效的利用。例如,假设您有一个 XML Web services,该服务检索关于一本图书的信息。创建一个方法在一个服务请求中返回所有信息,而不是让多个单独的方法分别检索书名、作者和出版商。一次传输一大块信息比多次传输小块信息更有效。
下面的代码示例演示如何将相关的信息聚集到一个 XML Web services 方法中。
[C#]
<%@ WebService Language="C#" Class="DataService" %>
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
public class DataService {
[WebMethod]
public DataSet GetTitleAuthors() {
SqlConnection myConnection = new SqlConnection("Persist Security Info=False;Integrated Security=SSPI;server=localhost;database=pubs");
SqlDataAdapter myCommand1 = new SqlDataAdapter ("select * from Authors", myConnection);
SqlDataAdapter myCommand2 = new SqlDataAdapter("select * from Titles", myConnection);
DataSet ds = new DataSet();
myCommand1.Fill(ds, "Authors");
myCommand2.Fill(ds, "Titles");
return ds;
}
}
当设计您的 XML Web services 时,一定要遵循标准的面向对象的编程惯例。使用封装来隐藏实现细节。对于更加复杂的 XML Web services,您可以使用继承和多态性来重复利用代码并简化设计。
下面的代码示例演示如何使用继承来创建执行数学计算的 XML Web services。
[C#]
<%@ WebService Language="C#" Class="Add" %>
using System;
using System.Web.Services;
abstract public class MathService : WebService
{
[WebMethod]
abstract public float CalculateTotal(float a, float b);
}
public class Add : MathService
{
[WebMethod]
override public float CalculateTotal(float a, float b)
{
return a + b;
}
}
public class Subtract : MathService
{
[WebMethod]
override public float CalculateTotal(float a, float b)
{
return a - b;
}
}
public class Multiply : MathService
{
[WebMethod]
override public float CalculateTotal(float a, float b)
{
return a * b;
}
}
public class Divide : MathService
{
[WebMethod]
override public float CalculateTotal(float a, float b)
{
if (b==0)
return -1;
else
return a / b;
}
}
使用输出缓存来提高您的 XML Web services 的性能。当打开输出缓存时,服务请求的结果在一段指定的时间内存储在输出缓存中。如果发出了类似的 XML Web services 请求,则可以从缓存中获得结果,而不是重新进行计算。这通过减少 XML Web services 服务器需要进行的处理缩短了 XML Web services 的反应时间。可以在客户端和服务器上执行缓存。Duration 属性允许您指定对 XML Web services 的输出进行缓存的时间量。
在客户端上启用输出缓存的指令是:
下面的代码示例演示如何在客户端应用程序上使用 Duration 属性指定 60 秒的输出缓存。
您还可以使用 WebMethod 特性类的 CacheDuration 属性在服务器上启用缓存。下面的代码示例演示如何在 XML Web services 方法上使用 CacheDuration 属性指定 60 秒的输出缓存。
XML Web services 支持客户端和承载该 XML Web services 的服务器之间的同步和异步通信。在同步通信情况下,客户端向服务主机服务器发送对服务的请求并等待响应。这阻止客户端在等待结果时执行其他操作。但是,异步通信让客户端在等待响应时继续处理其他任务。客户端在它可用时才响应服务请求的结果。
当您使用 Web 服务描述语言工具 (Wsdl.exe) 创建代理类时,它在该类中生成方法标准的同步版本和异步版本。异步版本由两个方法组成,分别叫做 Begin 和 End。Begin 方法用于启动 XML Web services,而 End 方法检索结果。
使用异步通信提高了系统利用率,避免当客户端等待 XML Web services 结果时在客户端上造成延迟。
下面的代码示例演示如何从客户端应用程序对 XML Web services 进行异步调用。
[C#]
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net" %>
<html>
<script language="C#" runat="server">
void EnterBtn_Click(Object Src, EventArgs E)
{
MyMath.Math math = new MyMath.Math();
// Call to Add XML Web service method asynchronously
// and then wait for it to complete.
IAsyncResult result =
math.BeginAdd(Convert.ToInt32(Num1.Text),
Convert.ToInt32(Num2.Text),
null,
null);
// Wait for asynchronous call to complete.
result.AsyncWaitHandle.WaitOne();
// Complete the asynchronous call to Add XML Web service method.
float total = math.EndAdd(result);
// Display results in a Label control.
Total.Text = "Total: " + total.ToString();
}
</script>
<body>
<form action="MathClient.aspx" runat=server>
<font face="Verdana">
Enter the two numbers you want to add and then press
the Total button.
<p>
Number 1:
<asp:textbox id="Num1"
runat=server/>
+
Number 2:
<asp:textbox id="Num2"
runat=server/>
=
<asp:button id="Total_Button"
text="Total"
OnClick="EnterBtn_Click"
runat=server/>
<p>
<asp:label id="Total" runat=server/>
</font>
</form>
</body>
</html>
通过 Internet 进行大量的服务请求可能影响客户端应用程序的性能。当设计您的 XML Web services 时,通过创建将相关的信息聚集在一起的方法对服务请求进行有效的利用。例如,假设您有一个 XML Web services,该服务检索关于一本图书的信息。创建一个方法在一个服务请求中返回所有信息,而不是让多个单独的方法分别检索书名、作者和出版商。一次传输一大块信息比多次传输小块信息更有效。
下面的代码示例演示如何将相关的信息聚集到一个 XML Web services 方法中。
[C#]
<%@ WebService Language="C#" Class="DataService" %>
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
public class DataService {
[WebMethod]
public DataSet GetTitleAuthors() {
SqlConnection myConnection = new SqlConnection("Persist Security Info=False;Integrated Security=SSPI;server=localhost;database=pubs");
SqlDataAdapter myCommand1 = new SqlDataAdapter ("select * from Authors", myConnection);
SqlDataAdapter myCommand2 = new SqlDataAdapter("select * from Titles", myConnection);
DataSet ds = new DataSet();
myCommand1.Fill(ds, "Authors");
myCommand2.Fill(ds, "Titles");
return ds;
}
}
当设计您的 XML Web services 时,一定要遵循标准的面向对象的编程惯例。使用封装来隐藏实现细节。对于更加复杂的 XML Web services,您可以使用继承和多态性来重复利用代码并简化设计。
下面的代码示例演示如何使用继承来创建执行数学计算的 XML Web services。
[C#]
<%@ WebService Language="C#" Class="Add" %>
using System;
using System.Web.Services;
abstract public class MathService : WebService
{
[WebMethod]
abstract public float CalculateTotal(float a, float b);
}
public class Add : MathService
{
[WebMethod]
override public float CalculateTotal(float a, float b)
{
return a + b;
}
}
public class Subtract : MathService
{
[WebMethod]
override public float CalculateTotal(float a, float b)
{
return a - b;
}
}
public class Multiply : MathService
{
[WebMethod]
override public float CalculateTotal(float a, float b)
{
return a * b;
}
}
public class Divide : MathService
{
[WebMethod]
override public float CalculateTotal(float a, float b)
{
if (b==0)
return -1;
else
return a / b;
}
}
使用输出缓存来提高您的 XML Web services 的性能。当打开输出缓存时,服务请求的结果在一段指定的时间内存储在输出缓存中。如果发出了类似的 XML Web services 请求,则可以从缓存中获得结果,而不是重新进行计算。这通过减少 XML Web services 服务器需要进行的处理缩短了 XML Web services 的反应时间。可以在客户端和服务器上执行缓存。Duration 属性允许您指定对 XML Web services 的输出进行缓存的时间量。
在客户端上启用输出缓存的指令是:
<%@ OutputCache Duration="60" %>
下面的代码示例演示如何在客户端应用程序上使用 Duration 属性指定 60 秒的输出缓存。
[C#] <%@ Page Language="C#" %> <%@ Import Namespace="System.Net" %> <%@ OutputCache Duration="60" VaryByParam="none" %> <html> <script language="C#" runat="server"> void EnterBtn_Click(Object Src, EventArgs e) 4000 { MyMath.Math math = new MyMath.Math(); // Call the XML Web service. float total = math.Add(Convert.ToInt32(Num1.Text), Convert.ToInt32(Num2.Text)); // Display the results in a Label control. Total.Text = "Total: " + total.ToString(); } </script> <body> <form action="MathClient.aspx" runat=server> <font face="Verdana"> Enter the two numbers you want to add and press the Total button. <p> Number 1: <asp:textbox id="Num1" runat=server/> + Number 2: <asp:textbox id="Num2" runat=server/> = <asp:button id="Total_Button" text="Total" OnClick="EnterBtn_Click" runat=server/> <p> <asp:label id="Total" runat=server/> </font> </form> </body> </html>
您还可以使用 WebMethod 特性类的 CacheDuration 属性在服务器上启用缓存。下面的代码示例演示如何在 XML Web services 方法上使用 CacheDuration 属性指定 60 秒的输出缓存。
[C#] <%@ WebService Language="C#" Class="MathService" %> using System; using System.Web.Services; public class MathService : WebService { [WebMethod(CacheDuration=60)] public float Add(float a, float b) { return a + b; } [WebMethod(CacheDuration=60)] public float Subtract(float a, float b) { return a - b; } [WebMethod(CacheDuration=60)] public float Multiply(float a, float b) { return a * b; } [WebMethod(CacheDuration=60)] public float Divide(float a, float b) { if (b==0) return -1; return a / b; } }
相关文章推荐
- 使用 ASP.NET 创建的 XML Web 服务的设计指南
- 使用 ASP.NET 所创建的 XML Web 服务的设计指南
- Vs2010设计ASP.NET页面时出现"创建控件时出错"
- 【初学者指南】在ASP.NET MVC 5中创建GridView
- [深入剖析ASP.NET组件设计]一书中HttpApplication对象创建的细节(转)
- ASP.NET创建Web服务之设计方针
- ASP.NET Core 中文文档 第二章 指南(2)用 Visual Studio 和 ASP.NET Core MVC 创建首个 Web API
- ASP.NET创建Web服务之设计方针
- 【初学者指南】在ASP.NET MVC 5中创建GridView
- ASP.NET中Web DataGrid的使用指南(转自前沿设计网-找到这个真不容易啊!)
- ASP.NET创建Web服务之设计方针
- ASP.NET设计网络硬盘之创建文件夹与设置权限
- 【初学者指南】在ASP.NET MVC 5中创建GridView
- ASP.NET创建Web服务之设计方针
- ASP.NET Core 中文文档 第二章 指南(1)用 Visual Studio Code 在 macOS 上创建首个 ASP.NET Core 应用程序
- 组件设计:补白[深入剖析ASP.NET组件设计]一书中HttpApplication对象创建的细节
- ASP.NET Core 中文文档 第二章 指南(2)用 Visual Studio 和 ASP.NET Core MVC 创建首个 Web API
- 【初学者指南】在ASP.NET MVC 5中创建GridView
- asp.net core高级EF Core2.0数据模型设计创建
- [读书笔记]组件设计:补白[深入剖析ASP.NET组件设计]一书中HttpApplication对象创建的细节