Search code examples
wcfwcf-binding

Each contract has to be hosted in one ServiceHost?


I want to expose two contracts in my server. The app config file of the service is:

<services>
      <service name="Interface1Service">
        <endpoint address="" binding ="customBinding" contract="Interface1"
                  bindingName="binding_Interface1" bindingConfiguration="CustomBinding_Interface1" name="epInterface1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8733/Interface1/" />
          </baseAddresses>
        </host>
      </service>

      <service name="Interface2Service">
        <endpoint address="" binding ="customBinding" contract="Interface2"
                  bindingName="binding_Interface2" bindingConfiguration="CustomBinding_Interface1"
                  name="epInterface2Service">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8733/Interface2/" />
          </baseAddresses>
        </host>
      </service>
    </services>

And in my console application to host the service I have:

ServiceHost miHost = new ServiceHost(typeof(Interface1Service));

    miHost.Open();

    ServiceHost miHost2 = new ServiceHost(typeof(Interface2Service));
    miHost2.Open();

    Console.ReadKey();

    miHost.Close();
    miHost2.Close();

My doubt if this is the correct way to expose the two services or there is any way to use only one serviceHost to do that.

Because I have seen that is very common to have many contracts for related operations, such as one interface for modify product, other to modify persons, other for orders... etc.

Thank you so much.


Solution

  • ..is any way to use only one serviceHost to do that?

    It is certainly possible to expose multiple service contracts in a single hosting container. You simply need to create a class which implements all the service interfaces:

    public class MyService : Interface1, Interface2
    {
        // implement your operations here...
    }
    
    // then create service host:
    var miHost = new ServiceHost(typeof(MyService)); 
    

    My doubt if this is the correct way to expose the two services..

    Even though WCF lets you do this, whether you should do this is another question.

    If the operations exposed by the services were deemed different enough that they were placed into separate service contracts, then this is a clear signal that these are in fact different services, and should therefore be treated as such.

    Having a common concrete implementation for all these different services introduces coupling and should probably not be done.