Try – Catch – Finally & return

One of the most frequently asked questions in C# interview and most easy questions

try

{

set connections & execute query & other operations

return;

}

catch(Exception  exc)

{

throw;

}

finally

{

// other operations

}

Now question is wht will happen ? is it going to execute code in the finally() loop ….

Answer is YES !!

finally() block will execute irrespective of the return in the try loop.

Webservices – Method Overloading

Now webservice means software system designed to support interoperable machine-to-machine interaction over a network, also Overloading means method name remains same but the signatures must be different

Lets start doing it bit coding way ! Create web service WebServiceOverLoad by default webservice create
HelloWorld() method default,we start with overloading this method.as given below.

[WebService(Namespace = “http://tempuri.org/“)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebServiceOverLoad : System.Web.Services.WebService {

public WebServiceOverLoad () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public string HelloWorld() {
return “Hello World”;
}

[WebMethod]
public string HelloWorld(string name)
{
return “Hello” + name;
}

[WebMethod]
public string HelloWorld(string name,int one)
{
return “Hello” + name + ” ” + one;
}
}

when we compile this webservice we will get the following server error , suggestiong that we need to enter MessageName property of the WebMethod() for the overloaded methods.

Both System.String HelloWorld(System.String) and System.String HelloWorld() use the message name ‘HelloWorld’. Use the MessageName property of the WebMethod custom attribute to specify unique message names for the methods.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Both System.String HelloWorld(System.String) and System.String HelloWorld() use the message name ‘HelloWorld’. Use the MessageName property of the WebMethod custom attribute to specify unique message names for the methods.

So after the activation of the MessageName property as follows

[WebMethod (MessageName=”With name”)]
public string HelloWorld(string name)
{
return “Hello” + name;
}

[WebMethod (MessageName=”With name and number”)]
public string HelloWorld(string name,int one)
{
return “Hello” + name + ” ” + one;
}

so after compilation

Service ‘WebServiceOverLoad’ does not conform to WS-I Basic Profile v1.1. Please examine each of the normative statement violations below. To turn off conformance check set the ConformanceClaims property on corresponding WebServiceBinding attribute to WsiClaims.None.

Now we got this error because as we are using WebServiceBinding as default , it is binding to

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] make it to WsiProfiles.None

[WebServiceBinding((ConformsTo = WsiProfiles.None)]

So this way we can do Method Overloading in WebServices.

In short MessageName & WsiProfiles.None is the main to remeber.