WebMethod attributes in. Net

Source: Internet
Author: User

Author: zfive5 (zhaozidong)
Email: zfive5@yahoo.com.cn



WebMethod has six attributes:
. Description
. EnableSession
. MessageName
. TransactionOption
. CacheDuration
. BufferResponse



1) Description:


Is the description of the webservice method. Just like the functional annotation of the webservice method, it can be seen by the caller.
.


C #:


[WebMethod (Description = "Author: ZFive5 Function: Hello World")]
Public string HelloWorld ()
{
Return "Hello World ";
}



WSDL:


-<PortType name = "Service1Soap">
-<Operation name = "HelloWorld">
<Documentation> Author: ZFive5 Function: Hello World </documentation>
<Input message = "s0: HelloWorldSoapIn"/>
<Output message = "s0: HelloWorldSoapOut"/>
</Operation>
</PortType>
-<PortType name = "Service1HttpGet">
-<Operation name = "HelloWorld">
<Documentation> Author: ZFive5 Function: Hello World </documentation>
<Input message = "s0: helloworldhttpgtin"/>
<Output message = "s0: HelloWorldHttpGetOut"/>
</Operation>
</PortType>
-<PortType name = "Service1HttpPost">
-<Operation name = "HelloWorld">
<Documentation> Author: ZFive5 Function: Hello World </documentation>
<Input message = "s0: HelloWorldHttpPostIn"/>
<Output message = "s0: HelloWorldHttpPostOut"/>
</Operation>
</PortType>

2) EnableSession:


Indicates whether the webservice starts the session flag, which is mainly completed by cookie. The default value is false.


C #:


Public static int I = 0;
[WebMethod (EnableSession = true)]
Public int Count ()
{
I = I + 1;
Return I;
}



In the ie Address Bar, enter:
Http: // localhost/WebService1/Service1.asmx/Count?


Click Refresh to see


......
<? Xml version = "1.0" encoding = "UTF-8"?>
<Int xmlns = "http://tempuri.org/"> 19 </int>

<? Xml version = "1.0" encoding = "UTF-8"?>
<Int xmlns = "http://tempuri.org/"> 20 </int>
......
......


It can be used to process the Web Service database access things. I have done some experiments!



3) MessageName:


The main implementation method is to rename the reload:


C #:


Public static int I = 0;
[WebMethod (EnableSession = true)]
Public int Count ()
{
I = I + 1;
Return I;
}


[WebMethod (EnableSession = true, MessageName = "Count1")]
Public int Count (int da)
{
I = I + da;
Return I;
}



The first method is accessed through count, and the second method is accessed through count1!



4) TransactionOption:
Indicates the transaction support of the XML Web services method.


This is an explanation in msdn:


Due to the stateless nature of HTTP, the XML Web services method can only participate in transactions as the root object.
If the COM object and the XML Web services method are involved in the same transaction
Is marked as running in a transaction, the XML Web services method can call these COM objects.
If a TransactionOption attribute is Required or RequiresNew, XML Web services
Method to call another XML Web services method whose TransactionOption attribute is Required or RequiresNew,
Each XML Web services method is involved in their own transactions, because the XML Web services method can only be used as
Root object.


If an exception is thrown from a Web service method or is not captured by this method, the transaction is automatically abandoned. If no exception occurs
Commit the transaction, unless the method explicitly calls SetAbort.


Disable
Indicates that the XML Web services method is not running within the scope of the transaction. When processing the request, there will be no transaction
To execute the XML Web services method.
[WebMethod (TransactionOption = TransactionOption. Disabled)]

NotSupported
Indicates that the XML Web services method is not running within the scope of the transaction. When a request is processed
To execute the XML Web services method.
[WebMethod (TransactionOption = TransactionOption. NotSupported)]

Supported (error in msdn. Correct it here)


If a transaction exists, it indicates that the XML Web services method runs within the transaction scope. If there is no transaction, there will be no transaction
Create XML Web services.
[WebMethod (TransactionOption = TransactionOption. Supported)]

Required
Indicates that the XML Web services method requires a transaction. Because Web service methods can only be used as root objects in transactions
This creates a new transaction for the Web service method.
[WebMethod (TransactionOption = TransactionOption. Required)]

RequiresNew
Indicates that the XML Web services method requires a new transaction. When processing the request, XML Web services will be created in the new transaction.
[WebMethod (TransactionOption = TransactionOption. RequiresNew)]

I have never practiced it here, so I can only copy msdn. Please forgive me.


C #
<% @ WebService Language = "C #" Class = "Bank" %>
<% @ Assembly name = "System. EnterpriseServices" %>

Using System;
Using System. Web. Services;
Using System. EnterpriseServices;

Public class Bank: WebService {

[WebMethod (TransactionOption = TransactionOption. RequiresNew)]
Public void Transfer (long Amount, long AcctNumberTo, long AcctNumberFrom ){
MyCOMObject objBank = new MyCOMObject ();

If (objBank. GetBalance (AcctNumberFrom) <Amount)
// Explicitly abort the transaction.
ContextUtil. SetAbort ();
Else {
// Credit and Debit methods explictly vote
// The code for their methods whether to commit or
// Abort the transaction.
ObjBank. Credit (Amount, AcctNumberTo );
ObjBank. Debit (Amount, AcctNumberFrom );
}
}
}



5) CacheDuration:
The Web supports high-speed cache output, so that webservice does not need to be executed multiple times, which can improve access efficiency,
CacheDuration is the attribute that specifies the cache time.


C #:
Public static int I = 0;
[WebMethod (EnableSession = true, CacheDuration = 30)]
Public int Count ()
{
I = I + 1;
Return I;
}



In the address bar of ie, enter:


Http: // localhost/WebService1/Service1.asmx/Count?


Refresh it! To make the output different, wait 30 seconds...
Because the code is executed again 30 seconds later, the returned results are all in the server cache.






6) BufferResponse


Configure the WebService method to send information to the request end after the response is completely buffered. Wait for the completion of common applications
Sent only after being buffered! Take a look at the following program:


C #:


[WebMethod (BufferResponse = false)]
Public void HelloWorld1 ()
{
Int I = 0;
String s = "";
While (I <100)
{
S = s + "I <br> ";
This. Context. Response. Write (s );
I ++;
}
Return;
}


[WebMethod (BufferResponse = true)]
Public void HelloWorld2 ()
{
Int I = 0;
String s = "";
While (I <100)
{
S = s + "I <br> ";
This. Context. Response. Write (s );
I ++;
}
Return;
}

We can see their differences from the results of the two methods in ie. The first is pushing technology!
If any data is returned immediately, the other is to return the information together to the request end.


My example breaks the webservice return structure, so let's look at the example in msdn.
Strange!


[C #]
<% @ WebService class = "Streaming" language = "C #" %>


Using System;
Using System. IO;
Using System. Collections;
Using System. Xml. Serialization;
Using System. Web. Services;
Using System. Web. Services. Protocols;


Public class Streaming {


[WebMethod (BufferResponse = false)]
Public TextFile GetTextFile (string filename ){
Return new TextFile (filename );
}


[WebMethod]
Public void CreateTextFile (TextFile contents ){
Contents. Close ();
}


}


Public class TextFile {
Public string filename;
Private TextFileReaderWriter readerWriter;


Public TextFile (){
}


Public TextFile (string filename ){
This. filename = filename;
}


[XmlArra

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.