如何通过编码创建多个端点?

时间:2013-04-05 09:43:47

标签: c# wcf c#-4.0

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

的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)。任何人都有任何想法?

1 个答案:

答案 0 :(得分: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 - 这个approch是ludacris-并且作为@JanW sais,你应该让ServiceHost通过在你的实现上配置ServiceBehavior来处理并发性。

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

foreach (var e in host.Description.Endpoints)
{
    Console.WriteLine(e.Address);
}