调用WCF服务的多种方法

发布于 2024-09-26 03:46:21 字数 916 浏览 9 评论 0原文

我有一个类处理我的应用程序中与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

汐鸠 2024-10-03 03:46:21

我将使用一些通用的、可配置的异常处理组件,该组件允许基本的异常处理处理(如日志记录、重新抛出等)与实际处理位置分离。此类组件的一个示例是 Microsoft 的异常处理应用程序块

然后你可能会得到这样的代码:

try
{
    results = _client.MethodCall(input parameteres);
    _client.Close();
}
catch (Exception ex)
{
    _client.CloseIfNeeded();
    if (!ex.Handle("Wcf.Policy")) throw;
}

其中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:

try
{
    results = _client.MethodCall(input parameteres);
    _client.Close();
}
catch (Exception ex)
{
    _client.CloseIfNeeded();
    if (!ex.Handle("Wcf.Policy")) throw;
}

where CloseIfNeeded denotes a custom extension method encapsulating the WCF channel closing logic, and the Handle 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:

  • instant configurability of exception handling behavior (policies)
  • extensibility with custom exception handlers bound to specific types of exceptions and exception policies
  • better manageability and readability of code
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文