Search code examples
wcfwcf-bindingwcf-rest

Two rest-style WCF contracts on same endpoint(svc file)


I am trying to have two contracts under same SVC file, and it is not working. When I open the SVC file in the IE, I get error saying 'Endpoint error'. If I call the methods of the contract from a client, it wont hit the service. Servers are hosted on IIS.

Please let me know what is going on here.

Also is it possible to have one wsHttpBinding and one webHttpBinding under same SVC file or web service? They will be hosted on IIS.

SVCFile

    <%@ ServiceHost Language="C#" Debug="true" Service="S1.ServiceDual6" CodeBehind="ServiceDual6.cs"   Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

Contracts

    {
        [InspectorBehavior]
        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
        public class ServiceDual6 : IServiceREST, IServiceREST2
        {

            public string RestInDual_Contract(Message mm)
            {
                return "";
            }

            public string RestInDual_Contract2(Message mm)
            {
                return "";
            }
        }
    }

    namespace S1
    {

        [ServiceContract]
        public interface IServiceREST
        {
            [OperationContract]
            [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
            string RestInDual_Contract(Message mm);
        }

        [ServiceContract]
        public interface IServiceREST2
        {
            [OperationContract]
            [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
            string RestInDual_Contract2(Message mm);
        }
    }

Web.config

        <service name="S1.ServiceDual6" behaviorConfiguration="s1bhrWithHttpEnabled">
            <host>

                <baseAddresses>
                    <add baseAddress="http://localhost/S2/Service6.svc"/>
                </baseAddresses>
            </host>
            <endpoint address="" binding="webHttpBinding" contract="S1.IServiceREST" behaviorConfiguration="s1bhr">
            </endpoint>

          <endpoint address="" binding="webHttpBinding" contract="S1.IServiceREST2" behaviorConfiguration="s1bhr">
          </endpoint>

        </service>

Solution

  • You need to differentiate the two services by providing different path if hosted on single endpoint. Separate these two by adding unique value in address field.

    <endpoint address="Rest/v1" binding="webHttpBinding" contract="S1.IServiceREST" behaviorConfiguration="s1bhr">
                </endpoint>
    
    <endpoint address="Rest/v2" binding="webHttpBinding" contract="S1.IServiceREST2" behaviorConfiguration="s1bhr">
              </endpoint>
    

    Now you can invoke them by appending Rest/V1for "S1.IServiceREST" and Rest/V2 for "S1.IServiceREST2" in base address.