服务参考的替代方案

发布于 2024-11-03 22:54:42 字数 672 浏览 4 评论 0原文

我正在尝试通过解决一些痛点来协助一个项目团队简化他们的工作。

他们代码中的痛点之一是,他们通过服务引用(代理)使用 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 技术交流群。

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

发布评论

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

评论(2

黄昏下泛黄的笔记 2024-11-10 22:54:42

首先,我相信您可以通过创建自己的代理 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):

  • What is the deployment overhead? I don't see any. The proxy that you've generated will work after deployment. You change the service address & behaviours in the configuration file.
  • Problems with source control / get latest? This should not be a problem, either. Just get latest, and use the latest. If the service has changed - and you want to take advantage of the changes - you will need to get latest version of the service contract, if you're using a shared contract file and instantiating your own channel.
  • Updating the proxy? Lack of familiarity with how the proxy works, what it is and what it does might cause this, but again it's no easier without the proxy. Really, you just right click up select 'update service reference', right?

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.

拥抱影子 2024-11-10 22:54:42

有一些文章建议使用 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

因此,您的代码可以重写为:

using (var batchClient = new ServiceWrapper<IYourWcfService>("YourEndpointConfigurationName"))
{
    batchData = batchClient.Proxy.GetBatchData(batchNumber);
}

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:

using (var batchClient = new ServiceWrapper<IYourWcfService>("YourEndpointConfigurationName"))
{
    batchData = batchClient.Proxy.GetBatchData(batchNumber);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文