Web Service Attributes

Source: Internet
Author: User

Each web service requires a unique namespace, which enables Client ApplicationsProgramDifferentiate Web services that may use the same method name. The default namespace of the web service created in Visual Studio. NET is "http://tempuri.org /". Although a namespace is similar to a typical URL, it cannot be viewed in a web browser. It is only a unique identifier.

Web Service provides the following attributes.

L Description: the value of this attribute contains a descriptive message, which will be displayed in the XML Web Service Description file (such as service description and service help page) potential users that are generated and displayed to the XML Web Service.

L name: the value of this attribute contains the name of the XML Web Service. By default, this value is the name of the class that implements the XML Web Service.

L namespace: the value of this attribute contains the default namespace of the XML Web Service. The XML namespace provides a method to create a name in an XML document. The name can be identified by a uniform resource identifier (URI. You can use an XML namespace to uniquely identify elements or attributes in an XML document. Therefore, in the service description of XML Web Service, namespace is used as the default namespace for XML elements directly related to XML Web Service. If no namespace is specified, use the default namespace http://tempuri.org /.

ExampleCodeDescribes the usage of web service attributes:

// <Summary>

// Service1 Summary

/// </Summary>

[WebService (namespace = "http://tempuri.org /",

Description = "interface description text-test. ",

Name = "ltpservice")] // name of the Web Service

[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]

[Toolboxitem (false)]

Public class service1: system. Web. Services. WebService

{

}

Webmethod(Web service method) has the following six attributes.

L Description: The description of the web service method. Just like the functional annotation of the web service method, the annotation can be seen by the caller.

[Webmethod(Description = "query product prices by product number")]

Public String getproductprice2 (string productid)

{

Products pro = new products ();

Return pro. getprice (productid );

}

LEnablesession: Indicates whether the Web service starts the session flag, which is mainly completed by Cookie. The default value is false.

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 ?, Refresh to see:

<? XML version = "1.0" encoding = "UTF-8"?>

<Int xmlns = "http://tempuri.org/"> 1 </int>

<? XML version = "1.0" encoding = "UTF-8"?>

<Int xmlns = "http://tempuri.org/"> 2 </int>

//... The session value keeps refreshing

L messagename: mainly implements renaming after method overloading:

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;

}

1st methods are accessed through count, and 2nd methods are accessed through count1.

L transactionoption: indicates the transaction support of the web service method.

Due to the stateless nature of HTTP, the Web service method can only participate in transactions as the root object. If the COM object and the Web service method participate in the same transaction and are marked as running in the transaction in the component service management tool, the Web service method can call these COM objects. If a web service method whose transactionoption attribute is required or requiresnew calls another Web service method whose transactionoption attribute is required or requiresnew, each web service method participates in its own transactions, because the Web service method can only be used as the root object in the transaction. 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, the transaction is automatically committed unless the method explicitly calls setabort.

① Disabled

Indicates that the Web service method is not running within the scope of the transaction. When processing a request, the Web service method is executed without a transaction.

[Webmethod(Transactionoption = transactionoption. Disabled)]

② Notsupported

Indicates that the Web service method is not running within the scope of the transaction. When processing a request, the Web service method is executed without a transaction.

[Webmethod(Transactionoption = transactionoption. notsupported)]

③ Supported

If a transaction exists, it indicates that the Web service method runs within the transaction range. If no transaction exists, a Web Service is created without a transaction.

[Webmethod(Transactionoption = transactionoption. Supported)]

④ Required

Indicates that the Web service method requires a transaction. Because the Web service method can only be used as the root object to participate in the transaction, a new transaction will be created for the Web service method.

[Webmethod(Transactionoption = transactionoption. Required)]

⑤ Requiresnew

Indicates that the Web service method requires a new transaction. When processing the request, a Web Service is created in the new transaction.

[Webmethod(Transactionoption = transactionoption. requiresnew)]

Here is an example.

First, add reference in the class code: using system. enterpriseservices;, and then set the property transactionoption = transactionoption. requiresnew.

For example:

[Webmethod(Transactionoption = transactionoption. requiresnew)]

Public int deleteproduct (string productid)

{

String deletecmdsql = "delete from p_product where productid = '" +

Productid + "'";

String exceptionexcepsql = "delete from nonexistingtable where

Productid = '"+ productid + "'";

Sqlconnection sqlconn = new sqlconnection (

Maticsoft. dbutility. pubconstant. constring );

Sqlconn. open ();

Sqlcommand deletecmd = new sqlcommand (deletecmdsql, sqlconn );

Sqlcommand exceptioncmd = new sqlcommand (exceptionexcepsql, sqlconn );

// The command is correctly executed.

Deletecmd. executenonquery ();

// An exception occurs during command execution. Therefore, the first command is automatically rolled back. This method is set to the transaction mode.

// When an exception occurs, ASP. NET will automatically interrupt the transaction and roll back

Int cmdresult = predictioncmd. executenonquery ();

Sqlconn. Close ();

Return cmdresult;

}

In the preceding example, if an exception occurs in a database operation, the transaction is automatically aborted; otherwise, the transaction is committed.

L cacheduration: set the number of seconds for the response to be retained in the cache. In this way, the Web service does not need to be executed multiple times, which can improve the access efficiency. cacheduration is the attribute that specifies the cache time.

Public static int I = 0;

[Webmethod(Enablesession= True, cacheduration = 30)]

Public int count ()

{

I = I + 1;

Return I;

}

Enter http: // localhost/webservice1/service1.asmx/count? In the address bar of IE ?.

Refresh it with the same content! To make the output different, it takes 30 seconds. Because the code is re-executed 30 seconds later, the returned results are all in the server cache.

Two problems affect the output cache in ASP. NET 2.0 web service applications.

In ASP. NET 2.0, the HTTP Method of the test page has been changed from get to post. However, post is usually not cached. If get is used on the test page of ASP. NET 2.0 web service application, the cache will work normally.

In addition, HTTP indicates that the user agent (browser or application calling) should be able to rewrite the server cache by setting "cache-control" to "no-Cache. Therefore, when an ASP. NET application finds the "no-Cache" header, the cached results are ignored.

L bufferresponse: Configure whether the Web service method sends information to the request end after the response is fully buffered. Normal applications must be completely buffered before being sent.

 

Calling a Web Service in. NET is actually as simple as creating a web service. (Complete sample code: CD \ code \ ch07 \ webappclient)

The implementation steps are as follows.

(1) first, create an ASP. NET web application.

(2) Add a web service reference.

Right-click the project and select the "add web reference" command from the shortcut menu, as shown in 7-6. The "add web reference" dialog box is displayed, as shown in 7-7.

Figure 7-6 select "add web reference" command figure 7-7 "add web reference" dialog box

Adding a reference is divided into the following three cases.

L if it is a Web service in this solution, select the first one, and the program will automatically switch to the service address in the solution.

L if it is a Web service on the computer, the system will automatically find the Web service on the computer for you to choose from.

L in either case, the final goal is to input the corresponding address in the URL for loading. Therefore, we can enter the address of the web service in the URL, both local and public networks are the same. Then, click the "go" button to list the Web service test page and method description, and verify whether the project can use the web service, as shown in 7-8.

Figure 7-8 preview web reference

In this case, enter a name in the "Web reference name" text box, and we will use this name in the code to access the selected web service programmatically, click Add reference.

(3) In this case, a directory named web references is added to the project, as shown in 7-9. The automatically generated proxy class is placed here.

Figure 7-9 Web reference

(4) Click the "show all files" button in Solution Explorer of the Project. Then you can see the proxy class named reference. CS.

(5) using this proxy class in code is basically the same as using a local common class.

Productservice. ltpservice service = new productservice. ltpservice ();

String price = service. getproductprice ("001 ");

so far, ASP. NET has finished calling Web service, and only two lines of code have been written.

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.