2

端点是在 app.config 中创建的,如下所示,但是如何创建具有不同地址的多个端点?

应用程序配置

 <service name="PokerService.PlayerService" behaviorConfiguration="ServiceBehaviorPlayer">
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:5054" />
      </baseAddresses>
    </host>
    <!-- Service Endpoints -->
    <endpoint address="player" binding="netTcpBinding" bindingConfiguration="PlayerBinding" contract="PokerService.IPlayerService" />
    <endpoint address="player/mex" binding="mexTcpBinding" name="ServiceBehaviorPlayer" contract="IMetadataExchange" />
  </service>
</services>

但我怎么能像这样生成Tcp//localhost/player/1 ( 1-1000)。有人知道吗?

4

1 回答 1

2

这将在主机上创建 1000 个端点。

var host = new ServiceHost(typeof(PokerService.PlayerService));
 for(int i = 1; i <= 1000; i++)
 {
   host.AddServiceEndpoint(typeof(PokerService.IPlayerService), 
                                  new NetTcpBinding(),
                                  @"net.tcp://localhost:5054/player/"+i);
 }
 host.Open();

编辑

我同意@JanW - 这个方法是ludacris - 并且作为@JanW sais,你应该让ServiceHost处理并发,通过在你的实现上配置ServiceBehavior 。

但是,要获取托管端点的完整列表,请执行以下操作:

foreach (var e in host.Description.Endpoints)
{
    Console.WriteLine(e.Address);
}
于 2013-04-05T09:57:56.507 回答