简体   繁体   English

.NET服务主机调用自己的服务

[英].NET Service Host Calling Its Own Service

I have multiple WCF services self hosted in a windows service. 我有自己托管在Windows服务中的多个WCF服务。 One of these WCF services needs to call another one of the WCF services hosted in the same windows service. 其中一个WCF服务需要调用在同一Windows服务中托管的另一个WCF服务。 This may need to run on the same machine or on the same windows service installed on another machine. 这可能需要在同一台机器上或在另一台机器上安装的相同Windows服务上运行。 Do I need the application to add a reference to itself or is there an easier way of calling one of it's own services. 我是否需要应用程序添加对自身的引用,或者是否有更简单的方法来调用它自己的服务之一。 I know how to do the different machine bit by changing the endpoint address but am not too clear on whether I should be adding a reference to itself or not. 我知道如何通过更改端点地址来执行不同的机器位,但我不清楚是否应该添加对自身的引用。 ie do I need to use the same code that I would use from a separate client. 即我是否需要使用我将从单独的客户端使用的相同代码。

The concept here is called "location transparency". 这里的概念称为“位置透明度”。 That is, there is no (technical) difference in calling a (WCF) service that lifes in the same process or on another machine. 也就是说,调用在同一进程或另一台机器上运行的(WCF)服务没有(技术上)差异。

Generally, this is considered a good thing, since you could change the location of your services after/during deployment, depending on your needs (stability or resource consumption of individual services). 通常,这被认为是一件好事,因为您可以在部署之后/期间更改服务的位置,具体取决于您的需求(单个服务的稳定性或资源消耗)。

You could optimize for the fact that you are running on the same machine by configuring the Named Pipe Binding - whether that actually makes any noticeable difference of course depends on how long your service operations run anyway, doing their actual task (see here for more/good information on choosing an appropriate binding). 您可以通过配置命名管道绑定来优化您在同一台计算机上运行的事实 - 当然是否真正产生任何明显的差异取决于您的服务操作运行多长时间,执行其实际任务(请参阅此处了解更多/选择合适的绑定的好信息)。

Finally, if it really matters, you could create your own binding , possibly taking advantage of the fact that both services life in the same process - most likely not a trivial task. 最后,如果它真的很重要,你可以创建自己的绑定 ,可能利用两个服务生命在同一个过程中的事实 - 很可能不是一个简单的任务。

At any rate, however, you want to make sure that the actual service implementations have no dependency on the transport or binding being used, so to hold up the location transparency. 但是,无论如何,您希望确保实际的服务实现不依赖于正在使用的传输或绑定,因此要保持位置透明性。

If you want to access a WCF service, regardless of whether it's hosted in-process or not, generating a proxy for it is a good idea and an easy way of accessing it. 如果您想访问WCF服务,无论它是否在进程中托管,为它生成代理是一个好主意,也是一种访问它的简单方法。

It's not necessary, though, you can create a Channel to the service yourself using ChannelFactory . 但是,您可以使用ChannelFactory自行创建服务Channel But if you can, why not generate a proxy for it? 但如果可以,为什么不为它生成代理呢?

You can just create a new instance of the service with new keyword (targeting the concrete implementation of the service - not the interface, of course). 您可以使用new关键字创建服务的新实例(目标是服务的具体实现 - 当然不是接口)。 I do this all the time and it works fine. 我一直这样做,它工作正常。 Also, a project can't add a reference to itself or that would cause a rip in the space time continuum ;) 此外,项目不能添加对自身的引用或者会在时空连续体中引起撕裂;)

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

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