当前位置: 首页 > news >正文

WebService 方法特性描述

WebMethod有6个属性:
.Description
.EnableSession
.MessageName
.TransactionOption
.CacheDuration
.BufferResponse
1)Description:
是对webservice方法描述的信息。就像webservice方法的功能注释,可以让调用者看见
的注释。
C#:
[WebMethod(Description="Author:ZFive5Function:HelloWorld")]
publicstringHelloWorld()
{
return"HelloWorld";
}
WSDL:
-<portTypename="Service1Soap">
-<operationname="HelloWorld">
<documentation>Author:ZFive5Function:HelloWorld</documentation>
<inputmessage="s0:HelloWorldSoapIn"/>
<outputmessage="s0:HelloWorldSoapOut"/>
</operation>
</portType>
-<portTypename="Service1HttpGet">
-<operationname="HelloWorld">
<documentation>Author:ZFive5Function:HelloWorld</documentation>
<inputmessage="s0:HelloWorldHttpGetIn"/>
<outputmessage="s0:HelloWorldHttpGetOut"/>
</operation>
</portType>
-<portTypename="Service1HttpPost">
-<operationname="HelloWorld">
<documentation>Author:ZFive5Function:HelloWorld</documentation>
<inputmessage="s0:HelloWorldHttpPostIn"/>
<outputmessage="s0:HelloWorldHttpPostOut"/>
</operation>
</portType>
2)EnableSession:
指示webservice否启动session标志,主要通过cookie完成的,默认false。
C#:
publicstaticinti=0;
[WebMethod(EnableSession=true)]
publicintCount()
{
i=i 1;
returni;
}
在ie地址栏输入:
http://localhost/WebService1/Service1.asmx/Count?
点刷新看看
......
<?xmlversion="1.0"encoding="utf-8"?>
<intxmlns="http://tempuri.org/">19</int>
<?xmlversion="1.0"encoding="utf-8"?>
<intxmlns="http://tempuri.org/">20</int>
......
......
通过它实现webservice数据库访问的事物处理,做过实验,可以哦!
3)MessageName:
主要实现方法重载后的重命名:
C#:
publicstaticinti=0;
[WebMethod(EnableSession=true)]
publicintCount()
{
i=i 1;
returni;
}
[WebMethod(EnableSession=true,MessageName="Count1")]
publicintCount(intda)
{
i=i da;
returni;
}
通过count访问的是第一个方法,而通过count1访问的是第二个方法!
4)TransactionOption:
指示XMLWebservices方法的事务支持。
这是msdn里的解释:
由于HTTP协议的无状态特性,XMLWebservices方法只能作为根对象参与事务。
如果COM对象与XMLWebservices方法参与相同的事务,并且在组件服务管理工
具中被标记为在事务内运行,XMLWebservices方法就可以调用这些COM对象。
如果一个TransactionOption属性为Required或RequiresNew的XMLWebservices
方法调用另一个TransactionOption属性为Required或RequiresNew的XMLWebservices方法,
每个XMLWebservices方法将参与它们自己的事务,因为XMLWebservices方法只能用作事务中的
根对象。
如果异常是从Web服务方法引发的或未被该方法捕获,则自动放弃该事务。如果未发生异常,则自动提
交该事务,除非该方法显式调用SetAbort。
禁用
指示XMLWebservices方法不在事务的范围内运行。当处理请求时,将在没有事务
的情况下执行XMLWebservices方法。
[WebMethod(TransactionOption=TransactionOption.Disabled)]
NotSupported
指示XMLWebservices方法不在事务的范围内运行。当处理请求时,将在没有事务的
情况下执行XMLWebservices方法。
[WebMethod(TransactionOption=TransactionOption.NotSupported)]
Supported(msdn里写错了,这里改正)
如果有事务,指示XMLWebservices方法在事务范围内运行。如果没有事务,将在没有事务的情况
下创建XMLWebservices。
[WebMethod(TransactionOption=TransactionOption.Supported)]
必选
指示XMLWebservices方法需要事务。由于Web服务方法只能作为根对象参与事务,因
此将为Web服务方法创建一个新事务。
[WebMethod(TransactionOption=TransactionOption.Required)]
RequiresNew
指示XMLWebservices方法需要新事务。当处理请求时,将在新事务内创建XMLWebservices。
[WebMethod(TransactionOption=TransactionOption.RequiresNew)]
这里我没有实践过,所以只能抄袭msdn,这里请包涵一下了
C#
<@WebServiceLanguage="C#"Class="Bank">
<@assemblyname="System.EnterpriseServices">
usingSystem;
usingSystem.Web.Services;
usingSystem.EnterpriseServices;
publicclassBank:WebService{
[WebMethod(TransactionOption=TransactionOption.RequiresNew)]
publicvoidTransfer(longAmount,longAcctNumberTo,longAcctNumberFrom){
MyCOMObjectobjBank=newMyCOMObject();
if(objBank.GetBalance(AcctNumberFrom)<Amount)
//Explicitlyabortthetransaction.
ContextUtil.SetAbort();
else{
//CreditandDebitmethodsexplictlyvotewithin
//thecodefortheirmethodswhethertocommitor
//abortthetransaction.
objBank.Credit(Amount,AcctNumberTo);
objBank.Debit(Amount,AcctNumberFrom);
}
}
}
5)CacheDuration:
Web支持输出高速缓存,这样webservice就不需要执行多遍,可以提高访问效率,
而CacheDuration就是指定缓存时间的属性。
C#:
publicstaticinti=0;
[WebMethod(EnableSession=true,CacheDuration=30)]
publicintCount()
{
i=i 1;
returni;
}
在ie的地址栏里输入:
http://localhost/WebService1/Service1.asmx/Count?
刷新它,一样吧!要使输出不一样,等30秒。。。
因为代码30秒后才被再次执行,之前返回的结果都是在服务器高速缓存里的内容。
6)BufferResponse
配置WebService方法是否等到响应被完全缓冲完,才发送信息给请求端。普通应用要等完
全被缓冲完才被发送的!看看下面的程序:
C#:
[WebMethod(BufferResponse=false)]
publicvoidHelloWorld1()
{
inti=0;
strings="";
while(i<100)
{
s=s "i<br>";
this.Context.Response.Write(s);
i ;
}
return;
}
[WebMethod(BufferResponse=true)]
publicvoidHelloWorld2()
{
inti=0;
strings="";
while(i<100)
{
s=s "i<br>";
this.Context.Response.Write(s);
i ;
}
return;
}
从两个方法在ie里执行的结果就可以看出他们的不同,第一种,是推技术哦!
有什么数据马上返回,而后一种是把信息一起返回给请求端的。
我的例子本身破坏了webservice返回结构,所以又拿出msdn里的例子来,不要
怪哦!
[C#]
<@WebServiceclass="Streaming"language="C#">
usingSystem;
usingSystem.IO;
usingSystem.Collections;
usingSystem.Xml.Serialization;
usingSystem.Web.Services;
usingSystem.Web.Services.Protocols;
publicclassStreaming{
[WebMethod(BufferResponse=false)]
publicTextFileGetTextFile(stringfilename){
returnnewTextFile(filename);
}
[WebMethod]
publicvoidCreateTextFile(TextFilecontents){
contents.Close();
}
}
publicclassTextFile{
publicstringfilename;
privateTextFileReaderWriterreaderWriter;
publicTextFile(){
}
publicTextFile(stringfilename){
this.filename=filename;
}
[XmlArrayItem("line")]
publicTextFileReaderWritercontents{
get{
readerWriter=newTextFileReaderWriter(filename);
returnreaderWriter;
}
}
publicvoidClose(){
if(readerWriter!=null)readerWriter.Close();
}
}
publicclassTextFileReaderWriter:IEnumerable{
publicstringFilename;
privateStreamWriterwriter;
publicTextFileReaderWriter(){
}
publicTextFileReaderWriter(stringfilename){
Filename=filename;
}
publicTextFileEnumeratorGetEnumerator(){
StreamReaderreader=newStreamReader(Filename);
returnnewTextFileEnumerator(reader);
}
IEnumeratorIEnumerable.GetEnumerator(){
returnGetEnumerator();
}
publicvoidAdd(stringline){
if(writer==null)
writer=newStreamWriter(Filename);
writer.WriteLine(line);
}
publicvoidClose(){
if(writer!=null)writer.Close();
}
}
publicclassTextFileEnumerator:IEnumerator{
privatestringcurrentLine;
privateStreamReaderreader;
publicTextFileEnumerator(StreamReaderreader){
this.reader=reader;
}
publicboolMoveNext(){
currentLine=reader.ReadLine();
if(currentLine==null){
reader.Close();
returnfalse;
}
else
returntrue;
}
publicvoidReset(){
reader.BaseStream.Position=0;
}
publicstringCurrent{
get{
returncurrentLine;
}
}
objectIEnumerator.Current{
get{
returnCurrent;
}
}
}

转载于:https://www.cnblogs.com/SissyNong/archive/2010/01/05/1639652.html

相关文章:

  • VC6.0 +WDK 开发驱动的环境配置
  • [架构模式实践]如何不让第三方服务/组件的故障阻碍开发和测试进度
  • RedHat AS4 下安装基于bind的DNS服务器
  • nd5sum
  • jqueryeditable plugin
  • 迷望的人生
  • lftp使用简介
  • 用SQL存储过程将数据导出到Excel
  • 不使用反射的实体类方案
  • ASP删除文章时,需要删除eWebEditor上传文件
  • USB移动硬盘WinPE启动盘的制作
  • debain下装memcached
  • cto开博第一篇--迎接21世纪的第一个虎年
  • ftp服务器的架设
  • Moq测试基础说谈(二)——Mock方法,方法参数
  • $translatePartialLoader加载失败及解决方式
  • C++11: atomic 头文件
  • nginx 配置多 域名 + 多 https
  • session共享问题解决方案
  • 闭包,sync使用细节
  • 极限编程 (Extreme Programming) - 发布计划 (Release Planning)
  • 跨域
  • 名企6年Java程序员的工作总结,写给在迷茫中的你!
  • 思否第一天
  • nb
  • MiKTeX could not find the script engine ‘perl.exe‘ which is required to execute ‘latexmk‘.
  • 06-01 点餐小程序前台界面搭建
  • Nginx实现动静分离
  • #QT(智能家居界面-界面切换)
  • #stm32驱动外设模块总结w5500模块
  • #我与Java虚拟机的故事#连载01:人在JVM,身不由己
  • (+4)2.2UML建模图
  • (10)工业界推荐系统-小红书推荐场景及内部实践【排序模型的特征】
  • (原)Matlab的svmtrain和svmclassify
  • (转)树状数组
  • .bat批处理(十):从路径字符串中截取盘符、文件名、后缀名等信息
  • .Net 路由处理厉害了
  • .NET 命令行参数包含应用程序路径吗?
  • .NET 中 GetHashCode 的哈希值有多大概率会相同(哈希碰撞)
  • .netcore如何运行环境安装到Linux服务器
  • .Net面试题4
  • .NET性能优化(文摘)
  • @data注解_一枚 架构师 也不会用的Lombok注解,相见恨晚
  • [BeginCTF]真龙之力
  • [C#] 基于 yield 语句的迭代器逻辑懒执行
  • [C/C++]数据结构----顺序表的实现(增删查改)
  • [C++] 默认构造函数、参数化构造函数、拷贝构造函数、移动构造函数及其使用案例
  • [C++]:for循环for(int num : nums)
  • [Codeforces] combinatorics (R1600) Part.2
  • [codevs 1288] 埃及分数 [IDdfs 迭代加深搜索 ]
  • [Django ]Django 的数据库操作
  • [EFI]Dell Inspiron 15 5567 电脑 Hackintosh 黑苹果efi引导文件
  • [HEOI2013]ALO
  • [ios] IOS文件操作的两种方式:NSFileManager操作和流操作【转】
  • [iOS]-UIKit