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 ()]