4

I have a WCF service which needs to meet the following requirement:

  1. Endpoint1 : It should use netTCP binding with windows authentication.
  2. Endpoint2 : It should use netTCP binding with Custom User name and password validation.

I was able to do both of these individually by creating two service behaviors, one for Windows authentication and one for user name and password, but this way I have to expose 2 service instead of 1 for the above functionality. I am looking for a way by which I could expose only one service and by different end point configuration, I am able to fulfill the requirement.

Code snippet and configuration would be helpful.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Rishabh Verma
  • 117
  • 2
  • 9
  • 1
    I don't think you can - while you are using the same binding (NetTcp), you're implementing them differently, so in effect you have 2 bindings. Therefore you have two services. You might be able to do it in code by switching the bindings, but once the service is instantiated, the binding is set. – Tim Sep 09 '12 at 17:34
  • Second @Tim. You should write that as an answer I think. – Jeroen Sep 09 '12 at 18:28
  • Ok... If there isn't a way to do this with a single service, is there a way to get this done with a single svc.cs file. The configuration that will enable to have a single service exposed with different service behavior on different endpoints. – Rishabh Verma Sep 09 '12 at 19:52
  • @Tim don't get confused between endpoints and services. An endpoint can only have one binding, but a service can have multiple endpoints each with their own binding. – Kirk Broadhurst Sep 10 '12 at 01:09
  • possible duplicate of [WCF service with two binding types for two different clients](http://stackoverflow.com/questions/3929847/wcf-service-with-two-binding-types-for-two-different-clients) – Kirk Broadhurst Sep 10 '12 at 01:09
  • @KirkBroadhurst - You are correct. I misunderstood the question - I thought OP was trying to expose a single service through a single endpoint with 2 different bindings, hence my comment. That's what I get for reading a question when I'm only half-awake :) – Tim Sep 10 '12 at 02:43

1 Answers1

2

This is one of the scenarios that WCF supports, a single interface exposed as 2 different endpoints.

They will have two different addresses, but will point to the same code.

<service 
    name="Microsoft.ServiceModel.Samples.CalculatorService"
    behaviorConfiguration="CalculatorServiceBehavior">
    <!-- This endpoint is exposed at the base address provided by host:       http://localhost/servicemodelsamples/service.svc  -->
   <endpoint address=""
        binding="basicHttpBinding"
        contract="Microsoft.ServiceModel.Samples.ICalculator" />
   <!-- secure endpoint exposed at {base address}/secure:       http://localhost/servicemodelsamples/service.svc/secure -->
  <endpoint address="secure"
        binding="wsHttpBinding"
        contract="Microsoft.ServiceModel.Samples.ICalculator" />
  ...
</service>

See: http://msdn.microsoft.com/en-us/library/ms751515.aspx

Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
  • I am afraid, this would not work..I have two different service behaviors. Both uses TCP binding , one for Windows authentication, one for custom authentication. What I am looking for is the configuration by which I can use the same svc.cs file with different service behaviors on different endpoints. – Rishabh Verma Sep 09 '12 at 20:29
  • 1
    You can override the behaviour configuration at the endpoint level and have 2 endpoints with the same binding see: http://stackoverflow.com/questions/7036209/endpoint-behavior-configuration-wcf-with-protobuf-net – Shiraz Bhaiji Sep 09 '12 at 20:33
  • Could you post the snippet which meets the above requirement; I have the binding in place already. Say Service behavior 1 = "WindowsAuthenticatedBehavior" using netTCPWindowsAuthenticated Binding and service behavior 2 = "CustomUserNameBehavior" using netTCPUserName binding. I have my service in service.svc.cs file which implements IService.cs. Could you post the snippet which will accomplish my ask ? – Rishabh Verma Sep 09 '12 at 20:46