tags:

views:

39

answers:

2

Is it possible to have one service with two behavior configurations? As you know it is possible to have one service and more than one endpoint where you can specify different binding configuration. But I need to have, for the same service, different service behaviors.

If I try something like this:

<services>
  <service name="Service.Service1" behaviorConfiguration="Behavior1">
    <host>
      <baseAddresses>
          ...
      </baseAddresses>
    </host>

    <endpoint ...>
    </endpoint>
  </service>

  <service name="Service.Service1" behaviorConfiguration="Behavior2">
    <host>
      <baseAddresses>
          ...
      </baseAddresses>
    </host>

    <endpoint ...>
    </endpoint>
  </service>
</services>

... I got error "A child element named 'service' with same key already exists at the same configuration scope"

I know that I can create new class that inherits original service class but is there better solution?

+1  A: 

Seems that you should register the same service class with another service name. Service behavior is a part of service configuration so if you want to use different behaviors you should configure different services. My question to you: if you will have one service with two behaviors, how wcf will decide to use this one or another? Why you solution with inheritance is bad?

Andrey Shvydky
"if you will have one service with two behaviors, how wcf will decide to use this one or another?" - Maybe by different base address?"Why you solution with inheritance is bad?"- I already have several live services and it would be good if I can achieve desired changes by changing configuration only. The second thing is that, in this case, I will have two classes that are not different at all just to achieve configuration needs.
Mijalko
+1  A: 

Quite intriguing question... I am not certain if what I am describing is possible but solution would be some thing like below:

  1. Choose different name for service - there will be some scheme such as name="Service.Service1.Entry1"
  2. Write custom ServiceHost (perhaps coupled with IInstanceProvider) that will ignore the last .Entry1 part and creates the service instance using Service.Service1 name.

Yet another way would be injecting dynamic type named "Service.Service1.Entry1" inherited from Service.Service1 when application is initializing. This part is very much possible - i.e. for every mentioned service, you would create n subtypes dynamically at app start-up so that you can use them as intended.

VinayC