How do you configure a WCF service with two endpoints to use a different ListenUri for each endpoint?

10,840

Solution 1

Just specify the address attribute with a value for either basic or webhttp endpoint that would distinguish its address. Ex:

<endpoint behaviorConfiguration="web" address="rest" binding="webHttpBinding" bindingConfiguration="general" contract="MyProject.Contracts.IGeneralService" /> 

should resolve your problem

Solution 2

When defining your endpoints for the first one you are specifying address="" and for second you dont have any value(So even for this one we will have address as "")

<endpoint address="" 
            binding="basicHttpBinding" 
            bindingConfiguration="generalBasic"
            contract="MyProject.Contracts.IGeneralService" />
        <endpoint behaviorConfiguration="web" 
            binding="webHttpBinding"
            bindingConfiguration="general" 
            contract="MyProject.Contracts.IGeneralService" />

So in that case when we specify address as empty it will take default base address.

So try to specify some value for anyone of the endpoints. So that we will have different address for these 2 endpoints.

<endpoint address="" 
            binding="basicHttpBinding" 
            bindingConfiguration="generalBasic"
            contract="MyProject.Contracts.IGeneralService" />
        <endpoint behaviorConfiguration="web" address="WP7Service" 
            binding="webHttpBinding"
            bindingConfiguration="general" 
            contract="MyProject.Contracts.IGeneralService" />

So our new endpoints address are:

  1. http://localhost:1726/GeneralService.svc
  2. http://localhost:1726/GeneralService.svc/WP7Service
Share:
10,840
Steve Elmer
Author by

Steve Elmer

I am a developer of thirteen years of well-rounded experience. I have demonstrated strength on both Windows and Web platforms and have worked with both Microsoft and Java technologies. I have experience on the technical side of the house which has repeatedly augmented and informed my software development skills. In addition, I have demonstrated a strong and intuitive knack with user interface and interaction design which has often served to make applications friendlier and easier to use. On the server side, I have worked at all tiers from the database on up and have made extensive use of design patterns and object oriented concepts.While being a strong team member, I have also held a variety of technical/team leadership positions and have worked for several years in an agile environment. I particularly enjoy agile development and embrace the principles thereof. I enjoy working with my colleagues and enjoy being in a position where I can mentor and work with more junior programmers. I take a servant leadership approach to the leadership roles I find myself in and feel that my experience as a ropes course instructor has certainly contributed to my leadership and team skills.I hold a variety of professional certifications, including Microsoft Certified Software Developer (MCSD), Microsoft Certified Technical Specialist (MCTS), Sun Certified Java Programmer (SCJP), and Certified Scrum Master (CSM). I am self directed and capable of working independently with little supervision. And, finally, I am committed to my professional growth and development and maintain a consistent self-study program and am currently on track for the MCPD-EAD 3.5.

Updated on June 04, 2022

Comments

  • Steve Elmer
    Steve Elmer almost 2 years

    I have a WCF Service which exposes an endpoint using the webHttpBinding and is consumed by both WPF and ASP.NET applications. Everything works great.

    I am now attempting to consume the service from Windows Phone (WP7). However, as the .NET Framework hasn't quite caught up to WP7 yet, the System.ServiceModel.Web namespace is unavailable with the result that the webHttpBinding doesn't work in WP7.

    Now, on my service, if I switch the webHttpBinding out for a basicHttpBinding, the phone application works.

    I do not want to have to rework my WPF and ASP.NET applications to use the basicHttpBinding though.

    I understand that WCF is capable of supporting multiple bindings and I have attempted to configure and run the service so that it exposes endpoints for both webHttpBinding and basicHttpBinding. The service appears to start up fine. However, the WPF & ASP.NET applications are unable to access it. And when I attempt to create a Service Reference in the WP7 application I get the following message:

    A binding instance has already been associated to listen URI 'http://localhost:1726/GeneralService.svc'. If two endpoints want to share the same ListenUri, they must also share the same binding object instance. The two conflicting endpoints were either specified in AddServiceEndpoint() calls, in a config file, or a combination of AddServiceEndpoint() and config.

    A colleague and I have played around with a variety of changes to the baseAddress, address, and listenUri attributes without any luck. We are now at the point of just trial and error which isn't proving to be very effective.

    <system.serviceModel>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
        <bindings>
            <basicHttpBinding>
                <binding name="generalBasic" />
            </basicHttpBinding>
            <webHttpBinding>
                <binding name="general" maxReceivedMessageSize="2147483647">
                    <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" />
                    <security mode="None">
                        <transport clientCredentialType="None" />
                    </security>
                </binding>
            </webHttpBinding>
        </bindings>
        <behaviors>
            <serviceBehaviors>
                <behavior>
                    <serviceMetadata httpGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
            <endpointBehaviors>
                <behavior name="web">
                    <webHttp />
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <services>
            <service name="MyProject.GeneralService">
                <endpoint address="mex" 
                    binding="mexHttpBinding"
                    contract="IMetadataExchange" />
                <endpoint address="" 
                    binding="basicHttpBinding" 
                    bindingConfiguration="generalBasic"
                    contract="MyProject.Contracts.IGeneralService" />
                <endpoint behaviorConfiguration="web" 
                    binding="webHttpBinding"
                    bindingConfiguration="general" 
                    contract="MyProject.Contracts.IGeneralService" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:1726/" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
    
  • PositiveGuy
    PositiveGuy over 10 years
    I've done it without putting an address when getting it work for json
  • PositiveGuy
    PositiveGuy over 10 years
    but then yea to distinguish xml vs. a json endpoint you have to freakin put in a stupid address attribute rather than it being able to just figure it out via content-type in the request. So basically if you don't hack WCF to check the content-type in the request and do your own custom code while intercepting the call, you can't hvae both xml and json endpoints for REST out there. Or you have to put a /json/ in the url and specify one with the address="json" for you to be able to provide json and xml endoints in WCF I guesss.
  • Rajesh
    Rajesh over 10 years
    @CoffeeAddict The content type determines if you response is in Json or XML. The address attribute is to distinguish each endpoint on which binding it uses and 2 endpoints cannot have the same address
  • Emil
    Emil about 10 years
    Does that mean when you want to send a request, you have to post your request to the address with .svc/web as suffix or you can still use .svc address?