调用WCF服务的多种方法
我有一个类处理我的应用程序中与 WCF 服务的所有交互,并且似乎 MSDN 说在 WCF 中使用 using)_ 语句是不好的 - 我可以明白为什么这不好并同意它(http:// /msdn.microsoft.com/en-us/library/aa355056.aspx)
我的问题是他们建议的实现方法意味着我有 10 个方法[作为我的服务中的 10 个公共方法],它们将具有相同的结构代码和这当然不遵循 DRY 原则 - 代码看起来类似于以下内容:
try
{
results = _client.MethodCall(input parameteres);
_client.Close();
}
catch (CommunicationException)
{
if (_client != null && _client.State != CommunicationState.Closed)
{
_client.Abort();
}
}
catch (TimeoutException)
{
if (_client != null && _client.State != CommunicationState.Closed)
{
_client.Abort();
}
}
catch (Exception ex)
{
if (_client != null && _client.State != CommunicationState.Closed)
{
_client.Abort();
}
throw;
}
这还没有任何日志记录,但是当然,当我开始记录它时,我将不得不在几乎 10 个不同的地方添加日志记录工作有
没有人对我如何在重用代码方面更加足智多谋有任何建议,
谢谢
保罗
I have a class that handles all the interaction in my application with my WCF service and it seems that MSDN say that the use of Using)_ statement with WCF is bad - I can see why this is bad and agree with it (http://msdn.microsoft.com/en-us/library/aa355056.aspx)
my problem is that their suggested method of implementation will mean that i have 10 methods [as 10 public methods in my service] that will have the same structure code and this of course does not follow the DRY principal - the code looks similar to the following:
try
{
results = _client.MethodCall(input parameteres);
_client.Close();
}
catch (CommunicationException)
{
if (_client != null && _client.State != CommunicationState.Closed)
{
_client.Abort();
}
}
catch (TimeoutException)
{
if (_client != null && _client.State != CommunicationState.Closed)
{
_client.Abort();
}
}
catch (Exception ex)
{
if (_client != null && _client.State != CommunicationState.Closed)
{
_client.Abort();
}
throw;
}
This doesn't have any logging yet but of course when I do come to start logging it then I will have to add the logging work in almost 10 different places
does anyone have any tips on how I can be a bit more resourceful here in reusing code
thanks
paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将使用一些通用的、可配置的异常处理组件,该组件允许基本的异常处理处理(如日志记录、重新抛出等)与实际处理位置分离。此类组件的一个示例是 Microsoft 的异常处理应用程序块。
然后你可能会得到这样的代码:
其中
CloseIfNeeded
表示封装WCF通道关闭逻辑的自定义扩展方法,Handle
异常方法调用异常处理机制,传入应在该位置应用的例外策略的名称。在大多数情况下,您可以将异常处理逻辑减少到相当多的一两行代码,这会给您带来以下好处:
I would use some general-purpose, configurable exception handling component that allows basic exception handling processing like logging, re-throwing etc. to be decoupled from the actual place of handling. One example of such a component is Microsoft's Exception Handling Application Block.
Then you could end up with a code like this:
where
CloseIfNeeded
denotes a custom extension method encapsulating the WCF channel closing logic, and theHandle
exception method calls the exception handling mechanism, passing in a name of the exception policy that shall be applied on this place.In most cases, you can reduce exception handling logic to a decent one or two lines of code, giving you several benefits: