嘗試撰寫處理 Json 請求(和回應)的 WCF 介面。寫了以下
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json)]
MyRequestResponse MyRequest(MyRequestType request);
public partial class MyRequestType
{
[JsonProperty(Required = Required.Always)]
public string id;
}
public partial class MyRequestResponse
{
public string dummyRes = "A response of sorts.";
}
在 web.config 中使用以下內容
<service name="emuse.Chooser.Provide.ChooserWCF.ChooserQueryService" behaviorConfiguration="JsonServiceBehaviour">
<endpoint address="" binding="webHttpBinding" contract="emuse.Chooser.Provide.ChooserWCF.IChoosQueryerQueryService"
behaviorConfiguration="JsonEndpointBehaviour" />
</service>
<serviceBehaviors>
<behavior name="JsonServiceBehaviour">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="JsonEndpointBehaviour">
<enableWebScript />
</behavior>
</endpointBehaviors>
我最初使用以下內容對其進行測驗
curl -X POST http://localhost/Chooser/ChooserQueryService.svc/MyRequest -H 'Content-Type:application/json' -H 'Accept:application/json' -d '{"id":1}'
但是我收到了傳入的訊息具有意外的訊息格式“原始”。該操作的預期訊息格式為“Xml”、“Json”
我已經為此搜索了各種答案,他們通常歸結為確保我正在傳遞“Content-Type:application/json”并且我有 BodyStyle = WebMessageBodyStyle.WrappedRequest。
任何想法我做錯了什么?
uj5u.com熱心網友回復:
事實證明,代碼或配置沒有任何問題——使用 Postman 成功了。
我不知道為什么 curl 失敗了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/523289.html
標籤:jsonwcf
上一篇:如何使彈出視頻回應于移動設備
