Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Single Service Implemation, Multiple Behaviours. Possible

Tags:

wcf

I have an IIS7 hosted service that I need to expose to two different clients. For one of the client I need to enforce a more strict Throttling behaviour than the other.

This means I need to define Two tags and because these can only be referenced from a tag, then I need two of these also?

I have defined the following web.config. Problem is, when I try to browse to either service so that I can pull the metadata then I get the following error:

Parser Error Message: A child element named 'service' with same key already exists at the same configuration scope.
Collection elements must be unique within the same configuration scope (e.g. the same application.config file). Duplicate key value:  'WCFTwoEndpoints.Calculate'.

Am I going about this the right way?

<system.serviceModel>
    <services>
      <service name="WCFTwoEndpoints.Calculate" behaviorConfiguration ="NotThrottled">
        <endpoint address="http://localhost/WCFTwoEndpoints/Calculate.svc"
          binding="wsHttpBinding" bindingConfiguration="" name="Calculator"
          contract="WCFTwoEndpoints.ICalculate" />
        <endpoint binding="mexHttpBinding" name="mex" contract="IMetadataExchange" />
      </service>
      <service name="WCFTwoEndpoints.Calculate" behaviorConfiguration ="Throttled">
        <endpoint address="http://localhost/WCFTwoEndpoints/ThrottledCalculate.svc"
          binding="wsHttpBinding" bindingConfiguration="" name="ThrottledCalculator"
          contract="WCFTwoEndpoints.ICalculate" />
        <endpoint binding="mexHttpBinding" name="mex" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="NotThrottled">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
        <behavior name="Throttled">
          <serviceMetadata httpGetEnabled="true" />
          <serviceThrottling maxConcurrentCalls="19" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
like image 610
Rob Bowman Avatar asked Jan 15 '12 20:01

Rob Bowman


People also ask

Is it possible for a WCF Service to implement multiple service contracts?

Sometimes in our mind the question arise; can we implement multiple service contract in WCF service? And the answer is, Yes we can.

Can WCF service have multiple endpoints?

As demonstrated in the Multiple Endpoints sample, a service can host multiple endpoints, each with different addresses and possibly also different bindings. This sample shows that it is possible to host multiple endpoints at the same address.

Where are the behaviors and bindings commonly set for WCF?

Windows Communication Foundation (WCF) configures behaviors in two ways: either by referring to behavior configurations -- which are defined in the <behavior> section of a client application configuration file – or programmatically in the calling application.

How does WCF define multiple endpoints?

The first endpoint is defined at the base address using a basicHttpBinding binding, which does not have security enabled. The second endpoint is defined at {baseaddress}/secure using a wsHttpBinding binding, which is secure by default, using WS-Security with Windows authentication.


1 Answers

You are a little stuck, because the name of the key must match exactly the name of your service class.

The only way around this I can think of, would be to create a new class which inherits from WCFTwoEndpoints.Calculate. Then you would have a distinct name. It's not very nice though.

 

I think if you asked the WCF designers why they designed it like this, they would say that a serivce is supposed to be indepent of the client. What you want here is not really a single service; but two different independant services which just happen to have some implementation in common. From the clients point of view they would not behave like one single service.

like image 157
Buh Buh Avatar answered Dec 07 '22 12:12

Buh Buh