【问题标题】:Two rest-style WCF contracts on same endpoint(svc file)同一端点(svc 文件)上的两个休息式 WCF 合同
【发布时间】:2016-02-06 00:27:49
【问题描述】:

我试图在同一个 SVC 文件下有两个合同,但它不起作用。当我在 IE 中打开 SVC 文件时,我收到错误消息“端点错误”。 如果我从客户端调用合同的方法,它不会影响服务。 服务器托管在 IIS 上。

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

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

SVC 文件

    <%@ 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>

【问题讨论】:

    标签: wcf wcf-binding wcf-rest


    【解决方案1】:

    如果托管在单个端点上,您需要通过提供不同的路径来区分这两种服务。通过在 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>
    

    现在您可以通过在基地址中附加 Rest/V1for "S1.IServiceREST" 和 Rest/V2 for "S1.IServiceREST2" 来调用它们。

    【讨论】:

      猜你喜欢
      • 2020-07-15
      • 1970-01-01
      • 2011-12-24
      • 2012-08-21
      • 1970-01-01
      • 1970-01-01
      • 2011-02-04
      • 2017-11-13
      • 1970-01-01
      相关资源
      最近更新 更多