Ranjita Das
Ranjita Das

Reputation: 443

how to create multiple endpoint by coding?

Endpoint is created in app.config as given below, but how to create multiple endpoints with different addresses?

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>

but how can i generate like this Tcp//localhost/player/1 ( 1-1000).Anyone have any idea?

Upvotes: 2

Views: 111

Answers (1)

Jens Kloster
Jens Kloster

Reputation: 11287

This will create 1000 endpoints on a host.

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();

Edit

I agree with @JanW - that this approch is ludacris- and as @JanW sais, you should let the ServiceHost deal with concurrency, by configure the ServiceBehavior on your implementation.

However, to get a complete list of hosted endpoints do this:

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

Upvotes: 2

Related Questions