Question

In my project I have two services Service1 and Service2 (i.e.service contracts).I want these two to be self hosted using console.For this I am using service named "myservice" and implementing both interfaces i.e. IService1 and IService2 like

public class myservice : IService1,IService2
...
....


ServiceHost serviceHost = new ServiceHost(typeof(myservice));
serviceHost.Open();

Endpoints used :

 <service behaviorConfiguration="myBehavior" name="myservice">
    <endpoint address="sa1" binding="netTcpBinding" contract="IService1"/>
    <endpoint address="sa2" binding="netTcpBinding" contract="IService2"/>
  <host>
    <baseAddresses>
      <add baseAddress="net.tcp://localhost:8001/" />
    </baseAddresses>
  </host>
</service>

I want these two be seperate service i.e. service named Service1 and Service2 (instead of myservice) with tcp binding and self hosting.Any code/help/suggestions appreciated.

Était-ce utile?

La solution

If you must have two separate services - then you need two separate service implementation classes and also two separate ServiceHost instances:

public class Service1 : IService1
{
  ...
}

public class Service2 : IService2
{
  ...
}


ServiceHost serviceHost1 = new ServiceHost(typeof(Service1));
serviceHost1.Open();

ServiceHost serviceHost2 = new ServiceHost(typeof(Service2));
serviceHost2.Open();

Endpoint config:

<service name="YourNamespace.Service1" behaviorConfiguration="myBehavior" >
    <endpoint address="sa1" binding="netTcpBinding" contract="IService1" />
    <host>
       <baseAddresses>
         <add baseAddress="net.tcp://localhost:8001/" />
       </baseAddresses>
    </host>
</service>
<service name="YourNamespace.Service2" behaviorConfiguration="myBehavior" >
    <endpoint address="sa2" binding="netTcpBinding" contract="IService2" />
    <host>
       <baseAddresses>
         <add baseAddress="net.tcp://localhost:8002/" />
       </baseAddresses>
    </host>
</service>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top