简体   繁体   English

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

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

I have written a service that I would like expose both via rest and soap. 我写了一个服务,我想通过休息和肥皂暴露。 Everything I read about WCF 4.0 says that I just need to expose 2 endpoints with differing behaviors to do this. 我读到的关于WCF 4.0的一切都说我只需要暴露2个具有不同行为的端点来完成这项工作。 But I cannot get it to work. 但我无法让它发挥作用。

Here is my service contract: 这是我的服务合同:

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

Here is my web.config: 这是我的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>

I am using routing to define my service url: 我正在使用路由来定义我的服务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)));
        }
    }

Is there something that I am doing wrong here? 有什么我在这里做错了吗? I could really use some help. 我真的可以使用一些帮助。

I never found the "right" way to do this in configuration but was able to use the routing engine to accomplish this. 我从来没有找到在配置中执行此操作的“正确”方法,但是能够使用路由引擎来实现此目的。

My global asax file now looks like this: 我的全局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)));
        }
    }

and my config like this: (to enable the rest help pages) 和我的配置像这样:(启用其余的帮助页面)

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

I like that this is in line with the asp.net MVC model more and requires little config. 我喜欢这更符合asp.net MVC模型,并且需要很少的配置。 Additionally doing it this way allowed me to remove the .svc files from my project entirely which is also a plus IMO. 另外这样做允许我从我的项目中完全删除.svc文件,这也是一个加IMO。

How are you hosting your WCF service?? 你是如何托管你的WCF服务的? In IIS, you need a virtual directory and a MyService.svc file somewhere to enable service activation. 在IIS中,您需要一个虚拟目录和一个MyService.svc文件来启用服务激活。

If you remove the ServiceRoute for now (to simplify matters), you should be able to reach your SOAP service endpoint at: 如果您暂时删除ServiceRoute(为了简化问题),您应该能够通过以下方式访问您的SOAP服务端点:

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

and your REST service should be at 你的REST服务应该在

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

(where you supply some arbitrary value for {value} ). (您为{value}提供了一些任意值)。

What exactly is not working in your case?? 究竟什么不适合你的情况?

You can try and test your SOAP endpoints using the WCF Test Client , while you should be able to hit the REST url in any browser. 您可以使用WCF测试客户端尝试测试SOAP端点,同时您应该能够在任何浏览器中访问REST URL。

This is possible to do in configuration. 这可以在配置中进行。 From msdn forum thread by user Ladislav Mrnka: http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/4e95575f-1097-4190-80dd-7a0f96d73f6e 来自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