Vishnu
Vishnu

Reputation: 897

Muliple Service Behaviors to same service in WCF

Can we attach multiple service behaviors to the same service in WCF.If yes how can we do that - via config file or as attributes?

Upvotes: 2

Views: 3371

Answers (3)

Mimas
Mimas

Reputation: 523

Yes, you can.

ServiceEndpoint has a Behaviors collection. So if you create a service in C# code, you may add any behavior to this collection: standard or your one. The example of how to create custom behavior and add it to the endpoint see here. Keep in mind that you can create and add as many behaviors as you need.

If you want to add behaviors in the configuration, you will need to create Behavior configuration extension. Here is an example hot to create it and add it to the endpoint in config file.

EDIT:

Service behaviors can be added in absolute same way

Upvotes: 3

Vinoth
Vinoth

Reputation: 851

Yes We can Create Multiple end points

<services>
  <service name="ReportService.ReportService">       
    <endpoint 
           address="ReportService"  
           binding="netTcpBinding"           
           contract="ReportService.IReportService">
    </endpoint>
     <endpoint 
           address="ReportService"  
           binding="basicHttpBinding"           
           contract="ReportService.IReportService">
    </endpoint>        
  </service>
</services>

we can create multiple end points like this.in client side app.config or webconfig file it show like this

<bindings>
        <netTcpBinding>
            <binding name="netTcpBinding_IReportService" />
        </netTcpBinding>

        <basicHttpBinding>
            <binding name="basicHttpBinding_IReportService" />
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="" binding="netTcpBinding"
            bindingConfiguration="netTcpBinding_IReportService" contract="ServiceReference.IReportService"
            name="netTcpBinding_IReportService">                
        </endpoint>

        <endpoint address="" binding="basicHttpBinding"
            bindingConfiguration="basicHttpBinding_IReportService" contract="ServiceReference.IReportService"
            name="basicHttpBinding_IReportService">                
        </endpoint>
    </client>

Then we should mention the binding name while we are referring

      ServiceReference.ReportServiceClient client = new ServiceReference.ReportServiceClient(netTcpBinding_IReportService); 

Now it will work with netTcpBinding

Upvotes: 0

Ricardo Pontual
Ricardo Pontual

Reputation: 3757

Yes you can. You need to configure your behaviors and, in service tag, configure each behavior, like this:

<service 
    name="Microsoft.ServiceModel.Samples.CalculatorService"
    behaviorConfiguration="CalculatorServiceBehavior">
  <!-- First behavior:
       http://localhost/servicemodelsamples/service.svc  -->
  <endpoint address=""
            binding="basicHttpBinding"
            contract="Microsoft.ServiceModel.Samples.ICalculator" />
  <!-- Second behavior, with secure endpoint exposed at {base address}/secure:
       http://localhost/servicemodelsamples/service.svc/secure -->
  <endpoint address="secure"
            binding="wsHttpBinding"
            contract="Microsoft.ServiceModel.Samples.ICalculator" />
</service>

The same service for ICalcular, for two different behaviors.

Read more here: https://msdn.microsoft.com/en-us/library/ms751515.aspx

Upvotes: 1

Related Questions