服务参考的替代方案
我正在尝试通过解决一些痛点来协助一个项目团队简化他们的工作。
他们代码中的痛点之一是,他们通过服务引用(代理)使用 WCF 服务 [即 Visua Studio 2008 中的“添加服务引用”。这会产生很多问题,包括部署开销、Souce Control 获取最新版本更新代理等相关问题。
为了处理这些以及其他与服务引用相关的问题,我正在寻找服务引用的良好替代方案。我已经看过 ChannelFactory 并且我最倾向于它。这似乎是一个很好的解决方案。
然而问题是,有很多代码像这样使用这些服务,
BatchClient client = new BatchClient(); //Batchclient is a proxy
batchData = client.GetBatchData(batchNumber)
所以如果我走 ChannelFactory 路径,我需要在整个项目中更新所有像上面这样的代码片段。由于大量的变化,团队对这个选项不太满意。
我的问题是,除了“添加服务引用”之外,是否还有其他更好的替代方案,只需最少的代码更改即可使用?或者有什么方法可以使用 ChannelFactory 而不影响现有的代码片段?
I am trying to assist one project team to streamline their work by fixing some of the pain points.
One of the pain points they have in their code is that, they are using WCF service via service references (proxy) [i.e. "Add Service Reference" in Visua Studio 2008. This creates a lot of problem including deployment overhead, Souce Control get latest related problems of updating proxy etc.
In order to handle these and other related issues with service reference, I am looking for a good alternative to service references. I have already seen ChannelFactory and I am leaning towards that most. That seems to be a good solution altogether.
However the problem is that, there is a lot of code consuming these services like this
BatchClient client = new BatchClient(); //Batchclient is a proxy
batchData = client.GetBatchData(batchNumber)
So if I go the ChannelFactory path, I would need to update all code piece like the above throughout the project. Because of amount of changes, the team is not very comfortable with this option.
Question I have is that, is there any other better alternative to "Add service reference" which can be used with minimal code changes? Or is there any way I could use the ChannelFactory without affecting exsting code pieces?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我相信您可以通过创建自己的代理
BatchClient
来实例化通道,轻松解决“必须替换大量代码”的问题。它的工作方式基本上与当前使用的生成代码相同。但是在转向无代理、合同共享模型之前,我会真正考虑一下你为什么要迁移。我不再使用生成的代码选项,主要是因为熟悉 WCF 后就没有必要了。不过,我相信“添加服务引用...”模式对于 WCF 初学者来说更容易,而且根本不需要花费任何费用。
问自己以下问题(或者更确切地说,向我解释):
您正在考虑的模型更干净,并且可能允许更大的灵活性,但它并不简单。如果您的团队在维护生成的服务代理时遇到问题,那么我会解决这些问题,而不是通过删除代理将它们扔到 WCF 的深处。
Firstly, I believe you could easily fix your 'have to replace a lot of code' problem by creating your own proxy
BatchClient
which would instantiate the channel. It would work essentially the same way as the currently-used generated code does.However before moving to the proxy-less, contract-sharing model I'd really consider why you want to move. I don't use the generated code option anymore, primarily because with familiarity of WCF it's not necessary. However I believe that the 'add service reference...' pattern is easier for WCF beginners, and it really doesn't cost you at all.
Ask yourself the following questions (or rather, explain to me):
The model you are considering is cleaner, and perhaps allows for more flexibility, but it is no simpler. If your team are having problems with maintaining the generated service proxy then I'd address those problems rather than throwing them into the deep end of WCF by removing the proxy.
有一些文章建议使用 ClientBase 类来达到相同的目的。
看
a) https://aturcarablog.wordpress。 com/2016/08/07/alternative-way-to-consume-wcf-service/
b) http://www.codeproject.com/Articles/412363/How-to-Use-a-WCF-Service-without-Adding -a-Service
因此,您的代码可以重写为:
There are some article that suggest to use class ClientBase to achive the same purpose.
See
a) https://aturcarablog.wordpress.com/2016/08/07/alternative-way-to-consume-wcf-service/
b) http://www.codeproject.com/Articles/412363/How-to-Use-a-WCF-Service-without-Adding-a-Service
Thus, your code can be rewritten as: