dannystommen
January 14th, 2009, 08:33 AM
I my WCF project, by every call I make to the host, I have a try/catch block, which look like the next:
try {
//call WCF methods
}
catch (FaultException<MyCustomException> ex) {
//do stuff
}
catch (FaultException ex) {
//do stuff
}
catch (CommunicationException) {
//do stuff
}
catch (TimeoutException) {
//do stuff
}
catch (Exception ex) {
// do stuff
}
With different exceptions I want to do different things. To avoid copy/paste this to every call I make to the WCF host, I want to make this generic.
I create the next solution:
public static class WCFclient{
public static void InvokeCall(Action action){
try {
action();
}
catch (FaultException<MyCustomException> ex) {
//do stuff
}
catch (FaultException ex) {
//do stuff
}
catch (CommunicationException) {
//do stuff
}
catch (TimeoutException) {
//do stuff
}
catch (Exception ex) {
// do stuff
}
}
}
public class Form1{
private void button1_Click(object sender, EventArgs e) {
TestProject.MyFirstServiceClient proxy = new TestProject.MyFirstServiceClient();
WCFclient.InvokeCall(delegate{
_service.Open();
_service.CallFirstMethod();
});
//close service
}
private void button2_Click(object sender, EventArgs e) {
TestProject.MyFirstServiceClient proxy = new TestProject.MyFirstServiceClient();
WCFclient.InvokeCall(delegate{
_service.Open();
_service.CallSecondMethod();
});
//close service
}
}
So far, it works. But it goes wrong when I call the InvokeCall with another service (when an exception happens). The reason for this is because the type of 'MyCustomException' is TestProject.MyFirstServiceClient.MyCustomException. When I call a method of MySecondServiceClient, and the custom exception is thrown, the type of this exception is TestProject.MySecondServiceClient.MyCustomException and it goes directly to the standard FaultException.
So I need make it more generic. What I am trying to do is something like
public static void InvokeCall<T>(Action action){
...
}
But now I need to cast the exception to the correct type.
How do I do this? Is the only way to do this using Reflection?
try {
//call WCF methods
}
catch (FaultException<MyCustomException> ex) {
//do stuff
}
catch (FaultException ex) {
//do stuff
}
catch (CommunicationException) {
//do stuff
}
catch (TimeoutException) {
//do stuff
}
catch (Exception ex) {
// do stuff
}
With different exceptions I want to do different things. To avoid copy/paste this to every call I make to the WCF host, I want to make this generic.
I create the next solution:
public static class WCFclient{
public static void InvokeCall(Action action){
try {
action();
}
catch (FaultException<MyCustomException> ex) {
//do stuff
}
catch (FaultException ex) {
//do stuff
}
catch (CommunicationException) {
//do stuff
}
catch (TimeoutException) {
//do stuff
}
catch (Exception ex) {
// do stuff
}
}
}
public class Form1{
private void button1_Click(object sender, EventArgs e) {
TestProject.MyFirstServiceClient proxy = new TestProject.MyFirstServiceClient();
WCFclient.InvokeCall(delegate{
_service.Open();
_service.CallFirstMethod();
});
//close service
}
private void button2_Click(object sender, EventArgs e) {
TestProject.MyFirstServiceClient proxy = new TestProject.MyFirstServiceClient();
WCFclient.InvokeCall(delegate{
_service.Open();
_service.CallSecondMethod();
});
//close service
}
}
So far, it works. But it goes wrong when I call the InvokeCall with another service (when an exception happens). The reason for this is because the type of 'MyCustomException' is TestProject.MyFirstServiceClient.MyCustomException. When I call a method of MySecondServiceClient, and the custom exception is thrown, the type of this exception is TestProject.MySecondServiceClient.MyCustomException and it goes directly to the standard FaultException.
So I need make it more generic. What I am trying to do is something like
public static void InvokeCall<T>(Action action){
...
}
But now I need to cast the exception to the correct type.
How do I do this? Is the only way to do this using Reflection?