-->

宁静的WCF POST问题与应用/ JSON内容类型的请求(Restful WCF POST iss

2019-08-03 23:01发布

我已经配置了一个RESTful的WCF具有以下POST“操作”:

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

在我的web.config中,我配置如下:

<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消息的ContentType =“应用/ JSON”它失败,出现以下消息: 远程服务器返回错误:(400)错误的请求

这是我发现的唯一的解决办法是定义工厂类:System.ServiceModel.Activation.WebServiceHostFactory诠释了SVC定义标记。

我发现这个解决方案在以下链接: 使用Ajax JSON发送到WCF 3.5

我的理解,定义WebServiceHostFactory如果你不想编辑web.config才有用。

我怎样才能使它没有确定WebServiceHostFactory工作?

请注意,我成功获得“JSON”内容类型POST消息,但并不是“应用/ JSON的”内容类型。

Answer 1:

问题是,要使用原始的编程模型(使用Stream参数),你需要告诉WCF不要试图了解该请求的JSON,而不仅仅是原始请求主体传递给参数。 你可以做到这一点使用WebContentTypeMapper 。 在后http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx展示了如何可以做到这一点。 与工厂解决方案的工作,因为它确实是,当它创建的端点。



文章来源: Restful WCF POST issue with application/json content type request