【问题标题】:Hosting WCF soap and rest endpoints side by side并排托管 WCF 肥皂和休息端点
【发布时间】:2025-12-31 02:35:06
【问题描述】:

我编写了一个服务,我希望通过 rest 和 soap 来公开它。我读到的关于 WCF 4.0 的所有内容都表明我只需要公开 2 个具有不同行为的端点来执行此操作。但我无法让它工作。

这是我的服务合同:

[ServiceContract]
public interface MyService
{
    [OperationContract]
    [WebGet(UriTemplate="data/{value}")]
    string GetData(string value);
}

这是我的 web.config:

<?xml version="1.0"?>
<configuration>

    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <system.serviceModel>

        <services>
            <service name="MyService">
                <endpoint name="mex" address="mex" binding="mexHttpBinding" contract="MyService"/>
                <endpoint address="rest" behaviorConfiguration="restBehavior" binding="webHttpBinding" contract="MyService" />
                <endpoint address="soap" behaviorConfiguration="soapBehavior" binding="basicHttpBinding" contract="MyService" />
            </service>
        </services>

        <behaviors>

            <serviceBehaviors>
                <behavior>
                    <serviceMetadata httpGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="true"/>
                </behavior>
            </serviceBehaviors>

            <endpointBehaviors>
                <behavior name="restBehavior">
                    <webHttp automaticFormatSelectionEnabled="true" helpEnabled="true" />
                </behavior>
                <behavior name="soapBehavior" />
            </endpointBehaviors>

        </behaviors>

        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>

    </system.serviceModel>

    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
    </system.webServer>

</configuration>

我正在使用路由来定义我的服务 url:

public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.Add(new ServiceRoute("dns", new ServiceHostFactory(), typeof(MyService)));
        }
    }

我在这里做错了什么吗?我真的需要一些帮助。

【问题讨论】:

标签: wcf rest soap


【解决方案1】:

我从未在配置中找到“正确”的方法来执行此操作,但能够使用路由引擎来完成此操作。

我的全局 asax 文件现在如下所示:

public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.Add(new ServiceRoute("my/soap", new ServiceHostFactory(), typeof(MyService)));
            RouteTable.Routes.Add(new ServiceRoute("my/rest", new WebServiceHostFactory(), typeof(MyService)));
        }
    }

我的配置是这样的:(启用其余帮助页面)

<system.serviceModel>

    <standardEndpoints>
        <webHttpEndpoint>
            <standardEndpoint automaticFormatSelectionEnabled="true" helpEnabled="true"/>
        </webHttpEndpoint>
    </standardEndpoints>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>

</system.serviceModel>

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
</system.webServer>

我喜欢这更符合 asp.net MVC 模型并且需要很少的配置。此外,这样做让我可以完全从我的项目中删除 .svc 文件,这也是 IMO 的一个加分项。

【讨论】:

    【解决方案2】:

    您如何托管您的 WCF 服务?在 IIS 中,您需要一个虚拟目录和一个MyService.svc 文件来启用服务激活。

    如果您现在删除 ServiceRoute(为了简化问题),您应该能够在以下位置访问您的 SOAP 服务端点:

    http://YourServer:Port/YourVirtualDirectory/YourService.svc/soap
    

    你的 REST 服务应该在

    http://YourServer:Port/YourVirtualDirectory/YourService.svc/rest/data/{value}
    

    (您可以为{value} 提供一些任意值)。

    在你的情况下究竟是什么不起作用?

    您可以尝试使用WCF Test Client 测试您的 SOAP 端点,同时您应该能够在任何浏览器中点击 REST url。

    【讨论】:

    • 我在 IIS 中托管。我看到的问题是,soap 端点是否只有在根 url 配置时才能访问:YourServer:Port/YourVirtualDirectory/YourService.svcYourServer:Port/YourVirtualDirectory/YourService(取决于我是否使用服务路由)。但是,如果我以这种方式配置它,则由于某种原因无法访问其余端点。或者,如果我按照最初描述的方式进行配置,则只能访问其余端点,并且肥皂端点返回 400 错误。
    • 我尝试将一个全新的服务文件 (.svc) 添加到我的项目中。使用与上面相同的配置它有点工作......休息和肥皂服务都可以从 .svc url 正常工作,但肥皂服务只能从根访问。配置的 /soap 地址未得到遵守。
    【解决方案3】:

    这可以在配置中完成。来自用户 Ladislav Mrnka 的 msdn 论坛帖子:http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/4e95575f-1097-4190-80dd-7a0f96d73f6e

    <system.serviceModel>
     <behaviors>
      <endpointBehaviors>
        <behavior name="REST">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="iSell.Prospects.ProspectBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="iSell.Prospects.ProspectBehavior" name="iSell.Prospects.ProspectService">
        <endpoint address="" behaviorConfiguration="REST" binding="webHttpBinding" contract="iSell.Prospects.ProspectService" />
        <endpoint address="soap" binding="basicHttpBinding" contract="iSell.Prospects.ProspectService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
     </services>
    </system.serviceModel>
    

    【讨论】:

    • 提示任何试图让它工作的人。您的肥皂 URI 将如下所示:http://host/myService.svc/soap/