自托管WCF服务的多个实例 [英] Multiple instances of a self hosted WCF service

查看:149
本文介绍了自托管WCF服务的多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个从C#中的控制台应用程序运行的工作者"服务,为了进行开发,我们始终在运行该服务的单个实例,该实例会提取数据块并执行一些计算,这些数据块是由另一个服务提供的(跟踪剩余的数据量等等.

We have a "worker" service running from console application in c#, for development we were always running a single instance of this service, which fetches chunks of data and performs some calculations, these chunks of data are provided by another service (which keeps track of how much data is left etc.)

现在,在质量检查中,我们希望同时(在同一台计算机上)运行工作者"服务的多个实例.但是,一旦启动第二个实例,我们将立即获得异常:

Now in QA we want to run multiple instances of the "worker" service simultaneously (on the same machine).However we are get an exception as soon as the second instance is launched:

TransportManager无法使用来侦听提供的URI. NetTcpPortSharing服务:URI已在 服务.

The TransportManager failed to listen on the supplied URI using the NetTcpPortSharing service: the URI is already registered with the service.

我们正在使用netTcpBinding,并且端点地址被硬编码到app.config中,并且保持不变,因此,我假设我们收到此错误.

We are using netTcpBinding and the endpoint address is hardcoded into the app.config and remains the same and because of that I assume we are getting this error.

<services>
    <service behaviorConfiguration="CoreBehavior" name="WorkerService">
        <endpoint address="net.tcp://localhost:8001/WorkerAssignment" binding="netTcpBinding" contract="IWorkerService" bindingConfiguration="CoreTcpBinding"/>
    </service>
</services>
<bindings>
    <netTcpBinding>
        <binding name="CoreTcpBinding" portSharingEnabled="true">
            <security mode="None"/>
        </binding>
    </netTcpBinding>
</bindings> 

应用代码:

var host = new ServiceHost(typeof(WorkerService));
host.Open();

我们如何为每个实例提供不同的URI,以使端口至少保持不变?

How do we provide a different URI for each instance so that atleast the port will remain the same ?

或者是否可以通过其他方法来运行同一服务的多个实例?

OR If there is a different way to run multiple instances of the same service?

推荐答案

如果您想拥有 service 的多个实例,而只拥有一个服务主机就足够了-只需装饰您具有 ServiceBehaviorAttribute

If you want to have multiple instances of the service than it is enough to have single service host - just decorate you WorkerService with ServiceBehaviorAttribute

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Percall)] 
public class WorkerService : IWorkerService 
{
  //...service data
}

这将确保对服务的每次调用都将首先创建服务的新实例.创建服务类的其他方法可以找到

This will make sure that every call to the service will first create new instance of the service. Other ways of creating service class can be found here

但是,如果您希望拥有多个服务主机,那么不可能有两个将完全相同的URL承载相同服务的服务主机.

If, however you would like to have multiple service hosts than it is impossible to have two service hosts that will host same service on completely the same url.

另一种情况是,如果您想让一个服务主机在具有相同基址和自定义uri的多个端点上托管相同服务.在这种情况下,您可以使用重载的ServiceHost构造函数或研究方法 AddBaseAddress AddServiceEndpoint .或者,如果您想从配置文件中执行此操作,请参考以下简单示例,对代码稍加修改

Another case would be if you want to have one service host hosting the same service on multiple endpoints with the same base address and custom uri's. In this case you can make use of overloaded ServiceHost constructor or investigate methods AddBaseAddress , AddServiceEndpoint. Or if you want to do it from configuration file than here's simple example with your code slightly modified

<service behaviorConfiguration="CoreBehavior" name="WorkerService">
    <endpoint address="WorkerAssignment" binding="netTcpBinding" contract="IWorkerService"/>
    <endpoint address="QAWorkerAssignment" binding="netTcpBinding" contract="IWorkerService"/>
  <host>
    <baseAddresses>
      <add baseAddress="net.tcp://localhost:8001/" />
    </baseAddresses>
  </host>
</service>

使用此配置,您将有两个服务端点

with this configuration you will have two endpoints for your service

net.tcp://localhost:8001/WorkerAssignment

net.tcp://localhost:8001/WorkerAssignment

net.tcp://localhost:8001/QAWorkerAssignment

net.tcp://localhost:8001/QAWorkerAssignment

这篇关于自托管WCF服务的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆