如何创建和管理wcf服务客户端?
起初,我将它们视为任何依赖项,将它们在构造函数中传递给使用服务的类:
var serviceConsumer = new ServiceConsumer(new MailingServiceClient())
问题是,一旦从服务抛出异常,它就会进入错误状态,并且不会回复任何请求,因此需要重新初始化到期的。
此外,我逐渐熟悉了以下事实:wcf 服务可能多次无法正确处置,因为生成的代理的处置方法已损坏(调用关闭而不检查故障状态),并遇到了几种方法来克服该问题:
因为我不会就像消费者代码出于测试原因自己创建服务客户端一样,我的问题是:
如何维护工作的无故障服务?
我怎样才能仍然使用依赖注入来实现可测试性?
At first I treated them as any dependency passing them in the ctor to the class consuming the service:
var serviceConsumer = new ServiceConsumer(new MailingServiceClient())
The problem was that once an exception was thrown from the service it entered a faulted state and would not reply to any requests, so re-initialization was due.
Further more, I became familiar with the fact that wcf services may not be disposed properly on several occasions, because the dispose method of the generated proxy is broken (calls close without checking the fault state) and encountered a couple of ways to overcome that:
- wrapping every call to every service.
- override the default IDisposable behavior of the scvutil.exe-generated class
Since I wouldn't like the consumer code to create the service client himself for testing reasons, my questions are:
how can I maintain a working un-faulted service?
how can I still use dependency injection for testability?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到了一个很好的解决方案。
一个替代 VS 的代理生成器,并在默认代理周围生成一个包装器,用于处理代理故障、超时和正确处理。
http://wcfproxygenerator.codeplex.com/
似乎对我来说工作得很好。
Found a nice solution.
a proxy generator that replaces the VS one, and generates a wrapper around the default proxy that deals with proxy faults, timeouts, and correct disposal.
http://wcfproxygenerator.codeplex.com/
seems to be working fine for me.