简体   繁体   English

使用自定义绑定时,WCF REST服务未传递POST请求主体流

[英]WCF REST service not passing POST request body stream when using custom binding

Following up from the solution to the previous question I asked here . 从解决方案开始,我在这里问了上一个问题。 It seems I now have a problem where if I use a custom binding in my .NET4 web service, then my methods that implement the web service WebInvoke contracts are no longer getting called when they contain the argument of the request body Stream. 现在似乎出现了一个问题,即如果我在.NET4 Web服务中使用自定义绑定,那么当实现Web服务WebInvoke合同的方法包含请求主体Stream的参数时,它们将不再被调用。

So here's code that runs the webservice as found from the the link above: 因此,这是从上面的链接中找到的运行Web服务的代码:

webserviceHost = new WebServiceHost(typeof(Service1), new Uri("http://localhost:8080));
webserviceHost.AddServiceEndpoint(typeof(IService), getBinding(), "webservice").Behaviors.Add(new WebHttpBehavior());
webserviceHost.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
webserviceHost.Open();

Here's the WebInvoke service contract defined: 这是定义的WebInvoke服务合同:

[WebInvoke(UriTemplate = "import_data?param={value}", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
WebResult importOrder(string value, Stream httpRequestPostBody);

And here's the method implementing the above contract: 这是实现上述合同的方法:

public WebResult importOrder(String value, Stream httpRequestPostBody)
{
    doSomething(value, httpRequestPostBody);
}

So if I try making a HTTP request through Fiddler to the webservice endpoint contract it will return a connection reset HTTP error response. 因此,如果我尝试通过Fiddler向Web服务终结点合同发出HTTP请求,它将返回连接重置HTTP错误响应。 If I remove the Stream httpRequestPostBody argument then it will run the method, but I have no way to get access to the raw JSON data in the request body (I can get the body but its in an XML form). 如果删除Stream httpRequestPostBody参数,它将运行该方法,但是我无法访问请求正文中的原始JSON数据(我可以获取正文,但可以XML形式获取)。 So I can only gather that I'm missing something in my custom binding to ensure that the body stream of the request can be passed. 因此,我只能确定自己的自定义绑定中缺少某些内容,以确保可以传递请求的主体流。 Anybody got any clues? 有人知道吗?

So the only answer I've been able to come up with is to remove the stream from the contract and then use .NET's DataContractJsonSerializer to convert the HTTP request body message from XML back to JSON. 因此,我唯一能想到的答案是从合同中删除流,然后使用.NET的DataContractJsonSerializer将HTTP请求正文消息从XML转换回JSON。 As shown below 如下所示

Contract: 合同:

[WebInvoke(UriTemplate = "import_data?param={value}", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
WebResult importOrder(string value);

Implementation: 实现方式:

public WebResult importOrder(String value){
    MyObject myObject = (MyObject)new DataContractJsonSerializer(typeof(MyObject)).ReadObject(OperationContext.Current.RequestContext.RequestMessage.GetReaderAtBodyContents());

    doSomething(value, myObject);
}

This solution is not ideal since the message is being converted from RAW to JSON to XML to MyObject, and additionally does not allow me to easily use a better 3rd party JSON parser and control how my objects are being initialised from the incoming data. 该解决方案并不理想,因为消息是从RAW转换为JSON,再转换为XML到MyObject,并且不允许我轻松地使用更好的第三方JSON解析器并控制如何根据传入数据初始化对象。 If anyone has got a better solution I'm all ears. 如果有人能找到更好的解决方案,我将不知所措。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM