Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One service, two behavior configurations

Tags:

c#

.net

wcf

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?

like image 511
Mijalko Avatar asked Aug 11 '10 07:08

Mijalko


2 Answers

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?

like image 115
Andrii Shvydkyi Avatar answered Sep 29 '22 21:09

Andrii Shvydkyi


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.

like image 45
VinayC Avatar answered Sep 29 '22 21:09

VinayC