Use attributes in WebService

Source: Internet
Author: User

It is very easy to publish a method in WebService. You only need to use System to publish the method. web. services. the WebMethodAttribute attribute class can be identified, but this attribute can only be applied to methods rather than properties. The MSDN document for this class can be seen (C #):
[AttributeUsage (AttributeTargets. Method)]
Public sealed class WebMethodAttribute: Attribute
So what if we need to publish an attribute into a Web method? Before discussing this question, let's take a look at why we need to publish a property as a Web method? As someone may tell you, you can rewrite your attributes into two corresponding getXXXXX/setXXXXX methods to publish them as webmethods respectively. Oh, yes, it seems that this can achieve the goal, but it damages our interface definition, in this way, we cannot enjoy the happiness attributes bring to us (Please do not ask me what happiness is). In fact, the most important reason is that we cannot implement interfaces. How can we do this? See the following code (C #):
Public interface IDataService
{
// This attribute indicates which database system is currently in use (for example, MS-SQLServer, Orcale, IBM-DB2, etc)
Int DataProvider
{
Get;
}
// This method executes a specified SQL script and returns its result set.
System. Data. DataSet Execute (String sqlText );
// This method saves the specified dataset to the database
Void Update (System. Data. DataSet dataSet );
}
Now we need to implement the IDataService interface to write a WebService. This attribute must be included and published in this WebService. What should we do at this moment? Haha and watch
Public class DataService: System. Web. Services. WebSerbice, IDataService
{
...
Public int DataProvider
{
[WebMethod (MessageName = "GetDataProvider")]
Get
{
...
}
}
[WebMethod ()]

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.