勤奋的码农
欢迎光临我的淘宝店:小丑情趣内衣店

每个 Web Service都需要唯一的命名空间,它可使客户端应用程序区分出可能使用相同方法名称的 Web Service。在 Visual Studio.NET中创建的Web Service的默认命名空间是"http://tempuri.org/"。尽管命名空间类似于典型的URL,但在Web浏览器中是不能查看的,它只是一个唯一标识符。

Web Service(Web服务)提供以下属性。

Description:此属性的值包含描述性消息,此消息将在XML Web Service的说明文件(例如服务说明和服务帮助页)生成后显示给XML Web Service的潜在用户。

Name:此属性的值包含XML Web Service的名称。在默认情况下,该值是实现XML Web Service的类的名称。

Namespace:此属性的值包含 XML Web Service的默认命名空间。XML命名空间提供了一种在XML文档中创建名称的方法,该名称可由统一资源标识符(URI)标识。使用XML命名空间,可以唯一标识XML文档中的元素或属性。因而,在 XML Web Service的服务说明中,Namespace被用做与XML Web Service直接相关的 XML 元素的默认命名空间。如果不指定命名空间,则使用默认命名空间 http://tempuri.org/

以下示例代码说明了Web Service属性的用法:

  1. //<summary>  
  2. //Service1 的摘要说明  
  3. //</summary>  
  4. [WebService(Namespace = "http://tempuri.org/",   
  5.     Description = "接口的描述说明文字-测试。",  
  6.     Name = "LTPService")] //Web Service 的名称  
  7. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  8. [ToolboxItem(false)]  
  9. public class Service1 : System.Web.Services.WebService  
  10. {  

WebMethod(Web服务方法)有以下6个属性。

Description:是对Web Service方法的描述信息。就像Web Service方法的功能注释,可以让调用者看见的注释。

  1. [WebMethod(Description="根据产品编号查询产品的价格")]  
  2.  
  3. public string GetProductPrice2(string ProductId)  
  4. {  
  5.     Products pro = new Products();  
  6.     return pro.GetPrice(ProductId);  

EnableSession:指示Web Service是否启动Session标志,主要通过Cookie完成,默认为false。

  1. public static int i = 0;  
  2. [WebMethod(EnableSession = true)]  
  3. public int Count()  
  4. {  
  5.     i = i + 1;  
  6.     return i;  

在IE地址栏中输入:http://localhost/WebService1/Service1.asmx/Count?,刷新看看:

  1. <?xml version="1.0" encoding="utf-8" ?>   
  2.   <int xmlns="http://tempuri.org/">1</int>   
  3.  
  4. <?xml version="1.0" encoding="utf-8" ?>   
  5.    <int xmlns="http://tempuri.org/">2</int>   
  6.  
  7. //…不停地刷新,Session的值一直存在 

MessageName:主要实现方法重载后的重命名:

  1. public static int i = 0;  
  2. [WebMethod(EnableSession = true)]  
  3. public int Count()  
  4. {  
  5.     i = i + 1;  
  6.     return i;  
  7. }  
  8. [WebMethod(EnableSession = true, MessageName = "Count1")]  
  9. public int Count(int da)  
  10. {  
  11.     i = i + da;  
  12.     return i;  

通过count访问的是第1个方法,而通过count1访问的是第2个方法。

TransactionOption:指示Web Service方法的事务支持。

由于HTTP协议的无状态特性,Web Service方法只能作为根对象参与事务。如果COM对象与Web Service方法参与相同的事务,并且在组件服务管理工具中被标记为在事务内运行,则Web Service方法可以调用这些COM对象。如果一个TransactionOption属性为Required或RequiresNew的Web Service方法调用另一个TransactionOption属性为Required 或RequiresNew的Web Service方法,则每个 Web Service方法将参与它们自己的事务,因为Web Service方法只能用做事务中的根对象。如果异常是从Web服务方法引发的或未被该方法捕获,则自动放弃该事务。如果未发生异常,则自动提交该事务,除非该方法显式调用 SetAbort。

①Disabled

指示Web Service方法不在事务的范围内运行。当处理请求时,将在没有事务的情况下执行Web Service方法。

  1. [WebMethod(TransactionOption = TransactionOption.Disabled)] 

②NotSupported

指示Web Service方法不在事务的范围内运行。当处理请求时,将在没有事务的情况下执行Web Service方法。

  1. [WebMethod(TransactionOption= TransactionOption.NotSupported)] 

③Supported

如果有事务,则指示Web Service方法在事务范围内运行。如果没有事务,则将在没有事务的情况下创建Web Service。

  1. [WebMethod(TransactionOption= TransactionOption.Supported)]  

④Required

指示Web Service方法需要事务。由于Web服务方法只能作为根对象参与事务,因此将为 Web 服务方法创建一个新事务。

  1. [WebMethod(TransactionOption= TransactionOption.Required)] 

⑤RequiresNew

指示Web Service方法需要新事务。当处理请求时,将在新事务内创建Web Service。

  1. [WebMethod(TransactionOption= TransactionOption.RequiresNew)]  

这里我们来看一个例子。

首先在类代码中添加引用:using System.EnterpriseServices;,然后设置属性TransactionOption = TransactionOption.RequiresNew。

例如:

  1. [WebMethod(TransactionOption = TransactionOption.RequiresNew)]  
  2. public int DeleteProduct(string ProductId)  
  3. {  
  4.     String deleteCmdSQL = "delete from P_Product where ProductId='" +  
  5.  
  6.     ProductId + "'";  
  7.     String exceptionCmdSQL = "DELETE FROM NonExistingTable WHERE   
  8.  
  9.     ProductId='" + ProductId + "'";  
  10.     SqlConnection sqlConn = new SqlConnection(  
  11.         Maticsoft.DBUtility.PubConstant.ConString);  
  12.     sqlConn.Open();  
  13.     SqlCommand deleteCmd = new SqlCommand(deleteCmdSQL, sqlConn);  
  14.     SqlCommand exceptionCmd = new SqlCommand(exceptionCmdSQL, sqlConn);  
  15.     //这个命令正确执行             
  16.     deleteCmd.ExecuteNonQuery();  
  17.       
  18.     //这个命令执行时会发生异常,所以,第一个命令会自动回滚。因为这个方法被设置为事务模式  
  19.     //发生异常时,ASP.NET会自动中断事务并回滚  
  20.     int cmdResult = exceptionCmd.ExecuteNonQuery();  
  21.     sqlConn.Close();  
  22.      

在上面的示例中,如果数据库操作引发异常,则事务将自动中止;否则将提交事务。

CacheDuration:设置响应应在缓存中保留的秒数。这样Web Service就不需要重复执行多遍,可以提高访问效率,而CacheDuration就是指定缓存时间的属性。

  1. public static int i = 0;  
  2. [WebMethod(EnableSession = true, CacheDuration = 30)]  
  3. public int Count()  
  4. {  
  5.     i = i + 1;  
  6.     return i;  

在IE的地址栏里输入:http://localhost/WebService1/Service1.asmx/Count?。

刷新它,内容一样!要使输出不一样,需等30秒。因为代码要在30秒后才被再次执行,之前返回的结果都是在服务器高速缓存里的内容。

有两个问题可以影响ASP.NET 2.0 Web服务应用程序中的输出缓存。

在ASP.NET 2.0中,测试页的HTTP方法已从Get更改为 Post。但是Post通常不进行缓存。如果在ASP.NET 2.0 Web 服务应用程序的测试页中改为使用Get,则缓存将正常工作。

此外,HTTP指示用户代理(浏览器或调用应用程序)应该可以通过将"Cache-Control"设置为"no-cache"以重写服务器缓存。因此,当 ASP.NET 应用程序找到"no-cache"标头时,会忽略缓存的结果。

BufferResponse:配置Web Service方法是否等到响应被完全缓冲完才发送信息给请求端。普通应用要等完全被缓冲完才被发送。

posted on 2013-08-18 18:52  勤奋的码农  阅读(214)  评论(0编辑  收藏  举报

友荐云推荐