并排托管 WCF 肥皂和休息端点 [英] Hosting WCF soap and rest endpoints side by side

查看:29
本文介绍了并排托管 WCF 肥皂和休息端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个服务,我想通过休息和肥皂公开它.我读到的关于 WCF 4.0 的所有内容都表明我只需要公开 2 个具有不同行为的端点即可执行此操作.但我无法让它工作.

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. But I cannot get it to work.

这是我的服务合同:

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

这是我的 web.config:

Here is my 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:

I am using routing to define my service 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.

我的全局 asax 文件现在看起来像这样:

My global asax file now looks like this:

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>

我喜欢这更符合asp.net MVC模型并且需要很少的配置.此外,通过这种方式,我可以完全从我的项目中删除 .svc 文件,这也是一个额外的 IMO.

I like that this is in line with the asp.net MVC model more and requires little config. Additionally doing it this way allowed me to remove the .svc files from my project entirely which is also a plus IMO.

这篇关于并排托管 WCF 肥皂和休息端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆