I came across WebMethods where parameter is passed as ref. But is this a good practice?
//Option1 with ref
[WebMethod]
public void Transaction2(ref Test test )
{}
As opposed to writing
//Option2
[WebMethod]
public Test Transaction1(Test test )
{}
But we should be aware that when we do like this in WebMethod it does not work the same as normal programming concept where a ref points to the same storage location of the calling variable.
If we look carefully in the Proxy that .Net Generates we will find the followings:
//Option1 with refpublic void Transaction2(ref Test test) {
object[] results = this.Invoke("Transaction2", new object[] {
test});
test = ((Test)(results[0]));
}
as opposed to
//Option2
public Test Transaction1(Test test) {
object[] results = this.Invoke("Transaction1", new object[] {
test});
return ((Test)(results[0]));
}
here the result is casted back to (Test) and value is assigned to the ref variable.
If we look at the soap response of both Transaction1 and Transaction2 we find pretty much the same response, and Test object is created as part of the Response with its value.
//Option 1 with ref
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Transaction2Response xmlns="http://tempuri.org/">
<test>
<input>string</input>
<output>string</output>
</test>
</Transaction2Response>
</soap:Body>
</soap:Envelope>
as opposed to
//Option 2
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Transaction1Response xmlns="http://tempuri.org/">
<Transaction1Result>
<input>string</input>
<output>string</output>
</Transaction1Result>
</Transaction1Response>
</soap:Body>
</soap:Envelope>
Though both the options results in similiar result still its strongly recommeded is to use a single class to encapsulate all the output parameter (if possible also input parameters). The reason is mainly considered from webservice's interoperability. Normally webservices are designed to be exposed to external systems and possibly it will be called from multiple heterogenous client platform. Also ref is not supported by many platforms. For example if I am not wrong even in the .Net world currently we cannot call a webmethod implemented with a ref parameter for the MS Ajax.Net implementations.