与应用程序/json内容类型请求有关的宁静WCF POST问题 [英] Restful WCF POST issue with application/json content type request

查看:102
本文介绍了与应用程序/json内容类型请求有关的宁静WCF POST问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用以下POST操作"配置了RESTful WCF:

I've configured a RESTful WCF with the following POST "operation":

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/Test", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json)]
void PostTest(Stream stream);

在我的web.config中,我配置了以下内容:

In my web.config, I've configured the following:

<service name="MyTest.TestSvc" behaviorConfiguration="MyTest.TestBehavior" >
    <endpoint address="" behaviorConfiguration="MyBehavior" binding="webHttpBinding" contract="MyTest.ITestSvc"/>
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>

<endpointBehaviors>
    <behavior name="MyBehavior">
        <webHttp />
    </behavior>
</endpointBehaviors>

<serviceBehaviors>
    <behavior name="MyTest.TestBehavior">
        <serviceMetadata httpGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
</serviceBehaviors>

当我使用"text/plain" "json" 发送POST消息时,一切正常. 但是,当我尝试发送POST消息时 ContentType ="application/json" 它失败,并显示以下消息: 远程服务器返回错误:(400)错误的请求

When I sent a POST message using "text/plain" or "json" everything works fine. However, when I try to send a POST message with ContentType = "application/json" It fails with the following message: The remote server returned an error: (400) Bad Request

我发现的唯一解决方案是定义Factory类: System.ServiceModel.Activation.WebServiceHostFactory int Svc定义标记.

The only solution which I found was to define the Factory class : System.ServiceModel.Activation.WebServiceHostFactory int the Svc definition markup.

我在以下链接中找到了此解决方案: 使用Ajax将JSON发送到WCF 3.5

I found this solution in the following link: Send JSON to WCF 3.5 using Ajax

据我了解,仅当您不想编辑web.config时,定义WebServiceHostFactory才有用.

As I understood , defining the WebServiceHostFactory is only useful if you don't want to edit the web.config.

如何在不定义WebServiceHostFactory的情况下使其工作?

How can I make it work without defining the WebServiceHostFactory?

请注意,我成功获取了"json"内容类型的POST消息,但未获取"application/json"内容类型的消息.

Notice that I succeed to get "json" content type POST message but not "application/json" content type.

推荐答案

问题是要使用原始编程模型(使用Stream参数),您需要告诉WCF不要尝试理解请求作为JSON,而是仅将原始请求正文传递给参数.您可以使用WebContentTypeMapper来实现.

The problem is that to use the raw programming model (using a Stream parameter), you'll need to tell WCF to not try to understand the request as JSON, instead just pass the raw request body to the parameter. You can do that using a WebContentTypeMapper. The post at http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx shows how this can be done. The solution with the factory works because it does that when it creates the endpoint.

这篇关于与应用程序/json内容类型请求有关的宁静WCF POST问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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