繁体   English   中英

并排托管WCF肥皂和休息端点

[英]Hosting WCF soap and rest endpoints side by side

我写了一个服务,我想通过休息和肥皂暴露。 我读到的关于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)));
        }
    }

有什么我在这里做错了吗? 我真的可以使用一些帮助。

我从来没有找到在配置中执行此操作的“正确”方法,但是能够使用路由引擎来实现此目的。

我的全局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。

你是如何托管你的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测试客户端尝试测试SOAP端点,同时您应该能够在任何浏览器中访问REST URL。

这可以在配置中进行。 来自msdn论坛帖子的用户Ladislav Mrnka: 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM