[英]Error receiving JSON (POST) via WCF
因此,我的WCF服務需要能夠接收JSON POST請求,例如:
{
"firstname": "Billy",
"lastname": "Jean"
}
帶標題:
"Content-Type": "application/json"
為此,我提出了以下建議。
我的界面:
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(UriTemplate = "/PostOmnis",
Method = "POST",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
System.Net.HttpStatusCode GetOmnisJson(Stream json);
}
我的課:
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class Service1 : IService1
{
public class MyMapper : WebContentTypeMapper
{
public override WebContentFormat GetMessageFormatForContentType(string contentType)
{
return WebContentFormat.Raw; // always
}
}
static Binding GetBinding()
{
CustomBinding result = new CustomBinding(new WebHttpBinding());
WebMessageEncodingBindingElement webMEBE = result.Elements.Find<WebMessageEncodingBindingElement>();
webMEBE.ContentTypeMapper = new MyMapper();
return result;
}
public System.Net.HttpStatusCode GetOmnisJson(Stream inputJsonStream)
{
StreamReader reader = new StreamReader(inputJsonStream);
string inputJson = reader.ReadToEnd();
// parse JSON string
.....
...
.
// return HTTP 200
return System.Net.HttpStatusCode.OK;
}
但是,通過Postman發送一些JSON,例如:
給我以下錯誤:
操作'GetOmnisJson'的傳入消息(合同'IService1'具有名稱空間' http://tempuri.org/ ')包含無法識別的http正文格式值'Json'。 預期的正文格式值為“ Raw”。 這可能是因為尚未在綁定上配置WebContentTypeMapper。 有關更多詳細信息,請參見WebContentTypeMapper的文檔。
我在這里想念什么?
三種選擇:
GetOmnisJson(Stream json)
更改為GetOmnisJson(YourPeopleClass person)
"Content-Type": "application/json"
(由於其他人正在使用該服務,並且您希望行為正常,因此並非總是這樣)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.