国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .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服務的示例。不要依靠服務描述或服務幫助頁面作為你唯一的文檔。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 陇南市| 嘉鱼县| 福建省| 平利县| 于都县| 唐海县| 武汉市| 太白县| 同心县| 江安县| 九台市| 瑞昌市| 保德县| 康平县| 即墨市| 攀枝花市| 佛学| 沁水县| 沙雅县| 贡嘎县| 延长县| 资中县| 珠海市| 上饶县| 任丘市| 黄平县| 靖江市| 鹤庆县| 阿巴嘎旗| 永登县| 大兴区| 黎城县| 准格尔旗| 沂水县| 盐源县| 高唐县| 东阳市| 襄垣县| 衡阳市| 朔州市| 胶州市|