您的位置:首页 > 编程语言 > ASP

ASP.NET创建Web服务之设计方针

2013-09-13 19:22 423 查看
使用ASP.NET构造一个简单的XML Web服务是相对容易的,然而,XML Web服务的真正的强大的功能只有等你研究了基础结构以后才能领悟。XML Web服务是建立在.NET框架和公共语言运行时间基础上的。一个XML Web服务可以利用这些技术。例如,ASP.NET支持的性能、状态管理和验证全都可被用来构造XML Web服务。

  XML Web服务的基础结构是构建来符合象SOAP、XML和WSDL这样的行业标准,并且它允许其他的平台的客户端与XML Web服务互操作。只要一个客户端可以发送符合标准的SOAP消息、依据格式化的服务描述,那么这个客户端可以调用一个使用ASP.NET创建的XML Web服务(不管客户端存在于何种平台)。

  当你使用ASP.NET构造一个XML Web服务时,它自动支持客户端使用SOAP、HTTP-GET和HTTP-POST协议通讯。即使HTTP-GET和HTTP-POST支持使用URL编码的变量名/变量值对来传送消息,支持这两个协议的数据类型也没有支持SOAP协议的数据类型丰富。在SOAP中,使用XML把数据传送到XML Web服务或从XML Web服务取回消息,你可以使用支持丰富的数据类型集的XSD模式定义复杂的数据类型。使用ASP.NET构造一个XML Web服务的开发者不必明确地定义复杂的数据类型。他们可以只构造一个管理类。ASP.NET处理定义到一个XSD模式的映射类和到XML数据的映射对象实例,以便通过网络传输。

  重要的是要注意XML Web服务并不能取代DCOM,我们应该说XML Web服务是跨越使用行业标准的平台通信的一种消息传递基础结构。

  因为ASP.NET提供了为XML Web服务内部工作的基础结构,开发者可以集中精力来实现他们的特定的XML Web服务的功能。使用ASP.NET开发一个XML Web服务从下面三步开始:

  1. 创建一个带有.asmx扩展名的文件。

  2. 在这个文件里面,使用一条指令声明XML Web服务。

  3. 定义组成XML Web服务功能的XML Web服务方法。

  XML Web服务是一个强大的技术,用于提供可通过因特网变成访问的服务。下面的建议将帮助你创建高效的XML Web服务:

  XML Web服务既支持同步的又支持异步的客户端和存放XML Web服务的服务器之间的通信。在同步通信情况下,客户端发送一个对服务的请求到服务主机服务器上等待响应。这防止客户端在等待结果的时候,执行其它的操作。然而异步通信导致客户端在等待相应的时候继续处理其它的任务。客户端在可用的时候响应服务请求的结果。

  当你使用Web服务描述语言工具(Wsdl.exe)来创建你的代理类的时候,它产生类中的方法的标准的、同步版本和异步版本。异步版本由两个方法组成,称为Begin和End。Begin方法用于初始化XML Web服务,而End方法取得结果。

  使用异步通信能够改善系统使用率和避免客户端在它等待XML Web服务结果的时候的延迟。

  下面的代码示例显示如何从一个客户应用程序产生一个到XML Web服务的异步调用。

[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>

[Visual Basic]

<%@ Page Language="VB" %>

<%@ Import Namespace="System.Net" %>

<html>

<script language="VB" runat="server">

Sub EnterBtn_Click(Src As Object, E As EventArgs)

Dim math As New MyMath.Math()

' Call to Add XML Web service method asynchronously

' and then wait for it to complete.

Dim result As IAsyncResult = _

math.BeginAdd(Convert.ToInt32(Num1.Text), _

Convert.ToInt32(Num2.Text), _

Nothing, _

Nothing)

' Wait for asynchronous call to complete.

result.AsyncWaitHandle.WaitOne()

' Complete the asynchronous call to Add XML Web service method.

Dim addtotal As Single = math.EndAdd(result)

' Display results in a Label control.

Total.Text = "Total: " & addtotal.ToString()

End Sub

</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>

  想获得更多关于异步通信的信息,请参阅后面的"和XML Web服务异步地通讯"。

  通过因特网产生许多服务请求可能影响客户应用程序的性能。当设计你的XML Web服务时,通过创建把有关信息集中在一起的方法可以有效利用服务请求。例如,假定你有一个XML Web服务,用来检索一本书的信息。我们可以创建一个在一条服务请求中返回所有的信息的方法,来代替单独的检索书名、作者和出版社的方法。一次传送大块的信息比多次传送小块的信息更有效率。

  下面的代码示例解释如何把有关信息组织到单个XML Web服务方法中。

[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;

}

}

[Visual Basic]

<%@ WebService Language="VB" Class="DataService" %>

Imports System

Imports System.Data

Imports System.Data.SqlClient

Imports System.Web.Services

Public Class DataService

<WebMethod> _

Public Function GetTitleAuthors() As DataSet

Dim myConnection As New SqlConnection("Persist Security Info=False;Integrated Security=SSPI;server=localhost;database=pubs")

Dim myCommand1 As New SqlDataAdapter("select * from Authors", myConnection)

Dim myCommand2 As New SqlDataAdapter("select * from Titles", myConnection)

Dim ds As New DataSet()

myCommand1.Fill(ds, "Authors")

myCommand2.Fill(ds, "Titles")

Return ds

End Function

End Class

  当设计你的XML Web服务时,请确保使用标准的面向对象编程操作。使用封装来隐藏实现细节。对于更
4000
复杂的XML Web服务,你可以使用继承和多态性来再次使用代码并简化你的设计。

  下面的代码示例显示如何使用继承来创建一个执行数学计算的XML Web服务。

[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;

 }

}

[Visual Basic]

<%@ WebService Language="VB" Class="Add" %>

Imports System

Imports System.Web.Services

MustInherit Public Class MathService : Inherits WebService

<WebMethod> _

Public MustOverride Function CalculateTotal(a As Single, _

b As Single) As Single

End Class

Public Class Add : Inherits MathService

<WebMethod> Public Overrides Function CalculateTotal(a As Single, b As Single) As Single

Return a + b

End Function

End Class

Public Class Subtract : Inherits MathService

<WebMethod> Public Overrides Function CalculateTotal(a As Single, b As Single) As Single

Return a - b

End Function

End Class

Public Class Multiply : Inherits MathService

<WebMethod> Public Overrides Function CalculateTotal(a As Single, b As Single) As Single

Return a * b

End Function

End Class

Public Class Divide : Inherits MathService

<WebMethod> Public Overrides Function CalculateTotal(a As Single, b As Single) As Single

 If b = 0 Then

  Return - 1

 Else

  Return a / b

 End If

End Function

End Class

  使用输出缓冲来改善你的XML Web服务的性能。当输出缓冲开启时,服务请求的结果被保存在输出缓冲中一段指定的时间。如果一个类似的XML Web服务请求被产生,结果可以从缓冲中取得,而不用重新计算。这样就通过减少XML Web服务服务器所需的处理来改善了XML Web服务的反馈时间。高速缓存可在客户端和服务器两者上执行。Duration属性允许你指定高速缓冲保存XML Web服务输出的时间。

  在客户端上使用输出高速缓冲的指令是:

 

<%@ 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)

{

 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>

[Visual Basic]

<%@ Page Language="VB" %>

<%@ Import Namespace="System.Net" %>

<%@ OutputCache Duration="60" VaryByParam="none" %>

<html>

<script language="VB" runat="server">

Sub EnterBtn_Click(Src As Object, e As EventArgs)

 Dim math As New MyMath.Math()

 ' Call the XML Web service.

 Dim addtotal As Single = math.Add(Convert.ToInt32(Num1.Text), Convert.ToInt32(Num2.Text))

 ' Display the results in a Label control.

 Total.Text = "Total: " & addtotal.ToString()

End Sub

</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服务方法上使用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;

}

}

[Visual Basic]

<%@ WebService Language="VB" Class="MathService" %>

Imports System

Imports System.Web.Services

Public Class MathService

Inherits WebService

<WebMethod(CacheDuration := 60)> _

Public Function Add(a As Single, b As Single) As Single

Return a + b

End Function

<WebMethod(CacheDuration := 60)> _

Public Function Subtract(a As Single, b As Single) As Single

Return a - b

End Function

<WebMethod(CacheDuration := 60)> _

Public Function Multiply(a As Single, b As Single) As Single

Return a * b

End Function

<WebMethod(CacheDuration := 60)> _

Public Function Divide(a As Single, b As Single) As Single

If b = 0 Then

Return - 1

End If

Return a / b

End Function

End Class

 

  当设计你的XML Web服务时,努力遵循如何格式化模式的结构。

  XML Web服务使用SOAP作为主要的传送和序列化协议。一个SOAP消息由一个可选择的头体和消息体组成。头部分包含可以被Web服务器体系结构处理的信息。SOAP没有定义任何头。消息体部分包含由应用程序处理的信息,例如用于XML Web服务的参数或返回值。

  提供用于你的XML Web服务的文档,如一个静态HTML文件,描述你的服务的操作和数据结构。还包括如何使用这个XML Web服务的示例。不要依靠服务描述或服务帮助页面作为你唯一的文档
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: