重用尝试捕获的WCF呼叫 [英] Reusing try catch for wcf call
问题描述
我有一系列调用wcf服务的方法,并且所有方法都具有相同的try catch代码
Response Method1(请求请求)
{
响应响应= null;
using(ChannelFactory< IService1> factory = new ChannelFactory< IService1>(myEndpoint)))
{
IService1 channel = factory.CreateChannel();
try
{
response = channel.Operation(request);
}
catch(CommunicationException ex)
{
//处理异常
}
catch(TimeoutException ex)
{
//处理异常
}
catch(异常例外)
{
//处理异常
}
}
返回响应;
}
依此类推(对于不同的服务,我有6种这样的方法)。我如何封装所有服务调用并在一个方法中处理异常
EDIT
根据Nathan A的建议,我创建了一个简单的通用方法:
受保护的TResult ExecuteAndCatch< TResult>(Func& TResult> serviceCall,T请求)
其中T:请求
其中TResult:响应
{
try
{
return serviceCall(request);
}
捕获(exCommunicationException ex)
{
}
捕获(TimeoutException ex)
{
}
catch(Exception例如)
{
}
返回null;
}
新方法是这样的
Response NewMethod1(请求请求)
{
响应response = null;
using(ChannelFactory< IService1> factory = new ChannelFactory< IService1>(myEndpoint)))
{
IService1 channel = factory.CreateChannel();
response = channel.Operation(request);
}
返回响应;
}
而我想这样称呼它
响应响应= ExecuteAndCatch< Response>(NewMethod1,new Request())
我在做什么错了?
使用包装函数。
看看这篇文章: http://mytenpennies.wikidot.com/blog:writing-wcf-wrapper-and-catching-common-exceptions
这是文章中的一个示例:
private void ExecuteAndCatch< T> (动作T动作,T t){
try {
action(t);
成功=真;
}
catch(TimeoutException){
成功=假;
Message =引发超时异常。;
}
catch(CommunicationException){
成功=假;
Message =引发通信异常。;
}
}
I have a series of methods that call wcf services and all of them have the same try catch code
Response Method1(Request request)
{
Response response = null;
using(ChannelFactory<IService1> factory = new ChannelFactory<IService1>(myEndpoint))
{
IService1 channel = factory.CreateChannel();
try
{
response = channel.Operation(request);
}
catch(CommunicationException ex)
{
// Handle Exception
}
catch(TimeoutException ex)
{
// Handle Exception
}
catch(Exception ex)
{
// Handle Exception
}
}
return response;
}
And so on (I have 6 methods like this for different services).. how can i encapsulate all the service calls and handle the exceptions in a single method
EDIT
Following Nathan A's advice I created a simple generic method:
protected TResult ExecuteAndCatch<TResult>(Func<T, TResult> serviceCall, T request)
where T : Request
where TResult : Response
{
try
{
return serviceCall(request);
}
catch (CommunicationException ex)
{
}
catch (TimeoutException ex)
{
}
catch (Exception ex)
{
}
return null;
}
The new methods would like this
Response NewMethod1(Request request)
{
Response response = null;
using(ChannelFactory<IService1> factory = new ChannelFactory<IService1>(myEndpoint))
{
IService1 channel = factory.CreateChannel();
response = channel.Operation(request);
}
return response;
}
and i'm trying to call it like
Response response = ExecuteAndCatch<Response>(NewMethod1, new Request())
What am I doing wrong?
Use a wrapper function.
Take a look at this article: http://mytenpennies.wikidot.com/blog:writing-wcf-wrapper-and-catching-common-exceptions
Here's an example from the article:
private void ExecuteAndCatch<T> (Action<T> action, T t) {
try {
action (t);
Success = true;
}
catch (TimeoutException) {
Success = false;
Message = "Timeout exception raised.";
}
catch (CommunicationException) {
Success = false;
Message = "Communication exception raised.";
}
}
这篇关于重用尝试捕获的WCF呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!