Pass by reference to a Web Service

Few days back someone asked me if we can pass value by reference to a web method. I thought why would some one want to pass parameter's reference to a web method because parameter's reference will point to memory location in client machine whereas web method will be invoked on the server machine where that reference will be invalid.
Anyways, the reality is that you can pass value by reference to a web method if it allows so. In fact you can also create web method that will take "out" parameter.

In Web Methods parameters can be passed by reference using “ref” keywords just like any other method.
Like –
public void WebMethod2(ref int iparam)
iparam = iparam + 100;

And in the proxy class at the client end it is declared as below:

public void WebMethod2(ref int iparam)
object[] results = this.Invoke("WebMethod2", new object[] {iparam});
iparam = ((int)(results[0]));

Web Methods can also have “out” parameters.
Like -
public string WebMethod1(string str,out int iReturn)
string temp = "Hello " + str;
iReturn = temp.Length;
return temp;

And in the proxy class at the client end it is declared as below:

public string WebMethod1(string str, out int iReturn)
object[] results = this.Invoke("WebMethod1", new object[] {str});
iReturn = ((int)(results[1]));
return ((string)(results[0]));

Error: A name was started with an invalid character.

Sometimes when we try to browse our newly hosted website or web service, we encounter with this error. The reason is that we forgot to configure ASP.NET version in the IIS or selected wrong version when we were hosting it.

So now its time to correct this issue, follow these steps:

1. Open IIS management console.
2. Right click on the virtual directory of your web service and choose properties from the shortcut menu.
3. Now click on the ASP.NET tab and choose appropriate ASP.NET version from the dropdown.
4. Now click on Apply button to apply new setting and then click on OK button.

Now try again to access you website/web service, you should be able to do so…

Facts about Web Services

1. Web service class can have overloaded constructors (parameterized) but client can use only default parameter less constructor to instantiate web service object.
2. service constructor(s) can not be made web method using “WebMethod” attribute.
3. If web services is updated then the client needs to updated the web reference to get the changes reflected in the proxy class and if the change affecting any web method call in the client then client needs to be rebuilt after code change as well.
4. Web methods can have public, private, protected, internal and protected internal access modifiers but only public web method will be exposed to the client.
5. Web service class could be public or internal. [Class can not be declared as private, protected or protected internal].
6. Web method could be declared as virtual method.
7. Public class can not be derived from web service class as it lessens the accessibility of base web service class. But you can derive non public class.
8. Web service class can have non “[WebMethod]” functions as well.
9. Web service class can implement interface and declare implemented interface function as “[WebMethod]” so they can be exposed to clients.
10. Web Method can be declared as static but then it will not be exposed to client.
11. Web Service class can be declared as sealed class.