Using properties in WebService

xiaoxiao2021-03-06  131

Publishing a method in WebService is simple, just identify the method you want to publish with the System.Web.Services.WebMethodAttribute property class, but this property can only be applied to the method without applying to the property. The MSDN documentation of this class can be seen (C #): [AttributeUSAGETARGETS.METHOD] PUBLIC Sealed Class WebMethodAttribute: Attribute, if we need to put a property into a web method, what? Before discussing this problem, let's take a look at why do we need to publish an attribute as a web method? Because some people may tell you this, you can rewrite your properties into two corresponding getxxxxx / setxxxxx methods to issue a WebMethod, respectively. Oh, yes, it seems that it seems to achieve the goal, but doing this to harm our interface definition, so that we can't enjoy the happiness of our properties (please don't ask me what happiness), the most important one The reason is that it is unable to complete the implementation of the interface. How do you say this? And look at the following code (C #):

Public interface iDataService {// This property indicates what database system currently used (for example: MS-SQLSERVER, ORCALE, IBM-DB2, etc.) INT DATAPROVIDER {Get;}

/ / This method performs a specified SQL script and returns its result set system.data.dataset execute (String SqlText);

/ / This method saves the specified data set to Void Update (System.Data.DataSet Dataset);}

Now we write a WebService to implement the iDataService interface, and this property must be included and published in the WebService. What should we do at this time? Oh, and see

Public Class DataService: System.Web.Services.Webserbice, iDataService {...

Public int dataProvider {[WebMethod (MessageName = "getDataProvider")] get {...}}

[WebMethod ()] public system.data.dataset execute (string sqltext) {...}}

OK, everyone sees, release a method to the web method to specify the webmethod attribute before its function, and the attribute is specified before its GET or SET device, and if you do not specify the MessageName property value of the property, release The attribute Web method name will be set to get_xxxxx and set_xxxxx.

The members in the WebService Agent class (by adding web references, Reference.cs) generated by vs.net are methods, so you need to manually modify the relevant methods in the proxy class as attributes, such as the relevant code of the local agent class in the above case. It looks like this:

[System.Web.Services.Protocols.SoapDocumentMethodAttribute (... ...)] [return: System.Xml.Serialization.XmlElementAttribute ( "GetDataProviderResult")] public int GetDataProvider () {object [] results = this.Invoke ( "GetDataProvider", new object [0]); return (int) results [0];}, then you only need to change the body part of the method to this:

public int GetDataProvider {[System.Web.Services.Protocols.SoapDocumentMethodAttribute (... ...)] [return: System.Xml.Serialization.XmlElementAttribute ( "GetDataProviderResult")] get {object [] results = this.Invoke ( "GetDataProvider", New Object [0]); Return (int) results [0];}}

By the above steps, you can use properties in WebService. Oh, happiness is back!

Later: In the development process, you may need to regularly publish a new method to your WebService page, the client program also needs to update the WebService synchronously to get the latest interface, when the client refresses the WebService reference, your proxy class will Lost your modifications you made. You can maintain a client agent class and compile this proxy class into a separate class library, and all clients that need to reference this Webservice simply refer to the proxy class in that class, which avoids multiple The developer changes the problem when updating the WebService reference, and simplifies the release of WebService. Of course, using this way you need to carefully consider your library version and some strategies of namespaces, which is also a quite interesting and artistic. In addition, there is also a need to give adequate attention from the WebService page size (including the number of methods) and performance. For a suggestion in this regard, please refer to my log: http://blog.9cbs.net/sw515// Archive / 2004/07/20 / 46349.aspx

转载请注明原文地址:https://www.9cbs.com/read-127183.html

New Post(0)