简体   繁体   English

在另一个项目 c# 中使用引用项目的 WCF 服务

[英]using a referenced project's WCF service in another project c#

I am struggling to understand certain referencing aspects when it comes to WCF and normal references.当涉及到 WCF 和普通引用时,我很难理解某些引用方面。 I have a solution with 4 projects.我有一个包含 4 个项目的解决方案。 One project is services and the other three had service references to this project.一个项目是服务,其他三个项目具有对该项目的服务引用。 One project is a base class library that is traditionally referenced in the other three.一个项目是传统上在其他三个项目中引用的基类库。

I had to update service references in a certain order or VS would get confused about ambiguous references.我必须按特定顺序更新服务引用,否则 VS 会对不明确的引用感到困惑。 This was due to the WCF reference being somehow imported through the normal reference so there were two namespaces for everything service related.这是因为 WCF 引用以某种方式通过普通引用导入,因此所有与服务相关的内容都有两个命名空间。

I tried to tidy this up by removing service references from all but the base library so this would be the arbiter of WCF connections and no more ambiguity.我试图通过从除基本库之外的所有服务引用中删除服务引用来整理它,这样这将成为 WCF 连接的仲裁者,不再有歧义。 However I am not sure it is even possible to use a service reference inside another project by way of reference and the services are throwing an endpoint not found exception.但是,我不确定是否可以通过引用在另一个项目中使用服务引用,并且这些服务会引发端点未找到异常。 I made sure the projects still had end points but they now do not have the references.我确保这些项目仍然有终点,但他们现在没有参考资料。 I assumed the endpoint would come through with the base reference but it appears it is not.我假设端点会通过基本参考,但它似乎不是。 Now classes in the base library are also in conflict between the base version and the service version of them.现在基础库中的类也在它们的基础版本和服务版本之间发生冲突。

Basically the references go a little like this:基本上参考文献有点像这样:

WCFProject ->serviceReference<- BaseLibrary ->reference<- SiteProject 1 and 2. WCFProject ->serviceReference<- BaseLibrary ->reference<- SiteProject 1 和 2。

How do I get SiteProjects to use WCF services without the service reference in the home project but the one in the BaseLibrary?如何让 SiteProjects 使用 WCF 服务而没有主项目中的服务引用但 BaseLibrary 中的服务引用?

对于那些偶然发现这一点的人,我找到了一种使用以下 SO 线程使用引用项目服务引用的方法: 找不到默认端点元素

I think you should start off by asking "why are these references service references, instead of binary references?".我认为您应该首先问“为什么这些引用是服务引用,而不是二进制引用?”。

There may well be a good reason for this, for example, the service will be deployed to a separate machine to the other projects in the solution.这很可能有一个很好的理由,例如,该服务将被部署到解决方案中的其他项目的单独机器上。 Another reason would be that to the capabilities provided by the service are not a natural fit for running in-process, and would make the consuming application too complex.另一个原因是服务提供的功能不适合在进程内运行,并且会使消费应用程序过于复杂。

In your scenario, the question would be:在您的情况下,问题是:

"For each of the three service consumers, would it be possible for me to take advantage of the capabilities of the service by simply referencing the service internals via a binary reference?" “对于三个服务消费者中的每一个,我是否可以通过二进制引用简单地引用服务内部来利用服务的功能?”

If the answer to this is yes then with a little refactoring you can simply remove the WCF service library code and the service references.如果答案是肯定的,那么只需稍加重构,您就可以简单地删除 WCF 服务库代码和服务引用。 Then you will simply have three projects which all host the desired capability in-process when they run.然后,您将只拥有三个项目,它们在运行时都承载了所需的进程内功能。

This is desirable for a few reasons, but here are two:出于几个原因,这是可取的,但这里有两个:

  1. In-process code is orders of magnitude faster than out-of-process code (like wcf services).进程内代码比进程外代码(如 wcf 服务)快几个数量级。
  2. In-process code is much simpler to manage and deploy.进程内代码的管理和部署要简单得多。

However, if you do need the service's capabilities to remain wrapped in a WCF service, then you can still simplify your solution dramatically by:但是,如果您确实需要将服务的功能保留在 WCF 服务中,那么您仍然可以通过以下方式显着简化您的解决方案:

  1. Moving the service into its own solution, and将服务转移到自己的解决方案中,以及
  2. Getting rid of all the service references and using ChannelFactory<T>.CreateChannel() to call the service directly instead.摆脱所有服务引用并使用ChannelFactory<T>.CreateChannel()直接调用服务。

ChannelFactory works by allowing you to call a service by referencing the service binaries directly. ChannelFactory 的工作原理是允许您通过直接引用服务二进制文件来调用服务。 This negates the need for a service reference, which has no shared binary dependencies.这不需要服务引用,它没有共享的二进制依赖项。

This approach is desirable for many reasons, but here are two:出于多种原因,这种方法是可取的,但这里有两个:

  1. No need to service references, drastically simplifies your code.无需服务引用,大大简化了您的代码。
  2. When the service changes, there is no need to update anything;当服务发生变化时,不需要更新任何东西; the change is available via binary reference, and is as natural as consuming an assembly in-process.更改可通过二进制引用获得,并且与在进程中使用程序集一样自然。

I often see examples of WCf services included as projects within solutions, where the other projects in the solution consume the service via service reference.我经常看到 WCf 服务作为项目包含在解决方案中的示例,解决方案中的其他项目通过服务引用使用服务。 This in my mind is not good design.这在我看来不是好的设计。 Firstly, a service should never reside under a single solution with it's consumers, and secondly, consumers should consume the service directly and without using service references unless absolutely necessary.首先,服务应该驻留在与它的消费者一个单一的解决方案;其次,消费者应该直接和不使用服务的引用,除非绝对必要消费服务。

I am not familiar with the process of not using a reference and your solution looks interesting.我不熟悉不使用参考的过程,您的解决方案看起来很有趣。 I will read up on that我会详细阅读

Please do.请做。 Service references have no place in a scenario where you are in full control of service and consumers.在您完全控制服务和使用者的场景中,服务引用没有立足之地。 I know this sounds prescriptive but it is bourne out by experience.我知道这听起来很规范,但它是根据经验得出的。

The database model library resides in the BaseLibrary and these are used by way of reference in the service project数据库模型库驻留在 BaseLibrary 中,这些在服务项目中作为参考使用

Which of the projects reference this assembly?哪些项目引用了这个程序集? If the service uses it then you can move it out of the solution with the service.如果该服务使用它,那么您可以将它从该服务的解决方案中移出。 But do the clients also use this?但是客户也使用这个吗? If so, does this mean clients and service both access the same database?如果是这样,这是否意味着客户端和服务都访问同一个数据库? If not, then why do they both make use of the same data models?如果不是,那为什么他们都使用相同的数据模型?

Would moving services to another solution not negate this advantage of only having one place for interproject code?将服务转移到另一种解决方案是否会抵消项目间代码只有一个地方的这种优势?

I would strongly disagree that having a single place for interproject code is always a good reason to bunch projects together inside a solution.我强烈不同意项目间代码的单一位置总是将项目组合在一个解决方案中的一个很好的理由。 You can then end up with projects which are completely unrelated to each other (from a business, technology, or capability perspective) sharing a solution just so they can benefit from this "advantage".然后,您最终可以将彼此完全无关的项目(从业务、技术或能力的角度)共享一个解决方案,以便他们可以从这种“优势”中受益。 A better strategy in my opinion is to remove the common assembly into it's own solution, and then publish it to a local NuGet server.在我看来,更好的策略是将公共程序集移除到它自己的解决方案中,然后将其发布到本地 NuGet 服务器。 Any project which requires this assembly can then retrieve it from NuGet.任何需要此程序集的项目都可以从 NuGet 中检索它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM