2

For a special service testing setup we need to create multiple WCF ServiceHost instances for the same service type, using different WCF XML configurations.

I thought of the following:

// App.config:
<services>
  <!-- does not work, name must be type, but can't be type to be unique... -->
  <service name="Service1">
    <endpoint ...>
    <host>
      ...
    </host>
  </service>
  <service name="Service2">
    ...
  </service>
</services>

// My Tests.cs:
var sh1 = new ServiceHost(typeof(Service));
var sh2 = new ServiceHost(typeof(Service));
// connect sh1 to "Service1" configuration and sh2 to "Service2" configuration

How to properly connect the ServiceHost instances with the corresponding configuration elements? I tried the following:

sh1.Description.ConfigurationName = "Service1";

However, that does not work. It still requires me to set the service's name attribute to the fully qualified type name of the service. But I can't, because it has to be unique...

How to host the same service type twice within a single application using WCF XML configuration?

D.R.
  • 20,268
  • 21
  • 102
  • 205
  • Possible duplicate of [WCF Service Name & Binding Name](http://stackoverflow.com/questions/2585256/wcf-service-name-binding-name) – Clay Nov 17 '15 at 10:40
  • Edited the question to elaborate a bit more - it is not a duplicate of the linked question. – D.R. Nov 17 '15 at 11:59

2 Answers2

2

Service name must be fully qualified. So something like:

<service name="MyNamespace.Service1">

should work.

how to host the very same service type twice (with different configuration)

You can define multiple <endpoint/> nodes per service you define. Each endpoint can use a different binding configuration.

How to host the same service type twice within a single application using WCF XML configuration?

OK I understand what you want to do now. So I don't think it's possible using the XML config. The reason for this is that the XML config is convention based. One of the prequisites for the convention states that you are only allowed to host a single instance of each service type, although each of these can have multiple endpoints defined.

It will probably be possible to do what you want by removing your XML config and doing it in code instead, but there may still be some other built in limitation to prevent you from doing this.

Your idea of subclassing the service is probably the best way.

tom redfern
  • 30,562
  • 14
  • 91
  • 126
  • OK. Then how to host the very same service type twice (with different configuration) within a single host application? – D.R. Nov 17 '15 at 10:44
  • Thanks, I tried that earlier, however: how to tell `ServiceHost` to take only one of the defined endpoints and not host both endpoints? – D.R. Nov 17 '15 at 10:59
1

A possible workaround is to derive a dummy Service2 from the original Service and host two different types...but that's not really what I want to do.

D.R.
  • 20,268
  • 21
  • 102
  • 205