自托管的WCF服务的多个实例多个、实例、WCF

2023-09-04 01:14:47 作者:花一样艳美的ㄝλ

我们必须从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.)

现在的QA,我们要同时运行的工人服务的多个实例(在同一台机器上的)。但是我们只要二审推出得到一个异常:

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的和端点地址硬codeD插入的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> 

应用code:

Application code :

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

我们如何提供不同的URI为每个实例使ATLEAST该端口将保持不变?

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?

推荐答案

如果你想拥有在服务的多个实例比它足有单一服务的主机 - 只是装点你的 WorkerService 的用的 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 的。或者,如果你想从配置文件做得比下面是简单的例子,你的code稍微修改

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://本地主机:8001 / WorkerAssignment

net.tcp://localhost:8001/WorkerAssignment

的net.tcp://本地主机:8001 / QAWorkerAssignment

net.tcp://localhost:8001/QAWorkerAssignment

 
精彩推荐
图片推荐