同一端点上的两个rest-style WCF契约(svc文件)

时间:2015-11-05 08:47:14

标签: wcf wcf-binding wcf-rest

我试图在同一个SVC文件下有两个合同,但它无法正常工作。当我在IE中打开SVC文件时,我收到错误提示' Endpoint error'。 如果我从客户那里调用合同的方法,它就不会打击服务。 服务器托管在IIS上。

请告诉我这里发生了什么。

是否可以在同一SVC文件或Web服务下拥有一个wsHttpBinding和一个webHttpBinding?它们将托管在IIS上。

SVCFile

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

合同

    {
        [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>

1 个答案:

答案 0 :(得分:0)

如果托管在单个端点上,则需要通过提供不同的路径来区分这两种服务。通过在address字段中添加唯一值来分隔这两者。

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

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

现在你可以通过在基地址中为“S1.IServiceREST”附加Rest/V1和为{S1.IServiceREST2“附加Rest/V2来调用它们。