首页 > 编程 > .NET > 正文

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

2024-07-10 12:56:04
字体:
来源:转载
供稿:网友
使用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服务时,请确保使用标准的面向对象编程操作。使用封装来隐藏实现细节。对于更复杂的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服务的示例。不要依靠服务描述或服务帮助页面作为你唯一的文档。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表