WebDispatchFormatter忽略“接受”HTTP标头

时间:2013-01-31 19:06:29

标签: .net wcf rest .net-4.0

我在VS2010,.NET 4中有一个WCF项目。我正在使用WcfRestContrib来支持多个无缝格式化程序用于我的服务调用。这不起作用。我能够提交带有JSON有效负载的查询,但我无法让它在响应中自动返回JSON数据。我正在使用“接受”HTTP标头并将其设置为“application / json” - 就像我在下面的配置中映射了mime类型一样。

我能让JSON回到响应中的唯一方法是将system.serviceModel/behaviors/serviceeBehaviors/behavior[@name='REST']/webFormatter/formatters/@defaultMimeType属性设置为“application / json”。这证明WebDispatchFormatter肯定涉及输出,它只是不使用Accept HTTP标头来确定使用哪个格式化程序。

如何让WebDispatchFormatter查看Accept HTTP Header?我认为这是默认行为。

我已将服务配置为在我的Web.config文件中使用WcfRestContrib的WebDispatchFormatter,如下所示:

<system.serviceModel>
    <extensions>
        <behaviorExtensions>
            <add name="webFormatter" type="WcfRestContrib.ServiceModel.Configuration.WebDispatchFormatter.ConfigurationBehaviorElement, WcfRestContrib" />
        </behaviorExtensions>
    </extensions>

    <bindings>
        <webHttpBinding>
            <binding name="REST"/>
        </webHttpBinding>
    </bindings>

    <behaviors>
        <endpointBehaviors>
            <behavior name="REST">
                <webHttp automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Xml"/> <!-- Note: I also tried it with automaticFormatSelectionEnabled set to "false". -->
            </behavior>
        </endpointBehaviors>

        <serviceBehaviors>
            <behavior name="REST">
                <webFormatter>
                    <formatters defaultMimeType="application/xml">
                        <formatter mimeTypes="application/xml,text/xml" type="WcfRestContrib.ServiceModel.Dispatcher.Formatters.PoxDataContract, WcfRestContrib" />
                        <formatter mimeTypes="application/json" type="WcfRestContrib.ServiceModel.Dispatcher.Formatters.DataContractJson, WcfRestContrib" />
                        <formatter mimeTypes="application/x-www-form-urlencoded" type="WcfRestContrib.ServiceModel.Dispatcher.Formatters.FormUrlEncoded, WcfRestContrib" />
                    </formatters>
                </webFormatter>
                <serviceDebug includeExceptionDetailInFaults="True"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>

    <services>
        <service name="MyService.REST" behaviorConfiguration="REST">
            <endpoint name="REST" contract="MyService.IREST" bindingConfiguration="REST" behaviorConfiguration="REST" binding="webHttpBinding" />
        </service>
    </services>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>

我的服务界面/合约看起来像这样。

namespace MyService
{
    [ServiceContract]
    public partial interface IREST
    {
        [OperationContract]
        [WebInvoke(Method="POST")]
        [WebDispatchFormatter(WcfRestContrib.ServiceModel.Dispatcher.WebDispatchFormatter.FormatterDirection.Outgoing)]
        List<MyData> GetData(SearchQuery searchQuery);
    }
}

我的服务实现如下:

namespace MyService
{
    [AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
    public partial class REST : IREST
    {
        public List<MyData> GetData(SearchQuery searchQuery)
        {
            return GetTheData(searchQuery);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

原来我使用Accepts代替Accept作为HTTP标头密钥!糟糕!