![](/img/trans.png)
[英]500 Internal Server error while accessing WCF service from http and unable to add service reference for the same reason
[英]WCF rest service 500 internal server error
我在訪問服務方法時遇到了以下異常。所有 GET 方法在我的服務上正常工作,但是當我按照以下方式運行 POST 方法時,我收到 500 內部服務器錯誤。
https://mservice.domain.com/ServicesRestful.svc/json/addorder
{"userId":"30155496","locationId":"10","order":"lorem"}
服務代碼
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "addorder", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[FaultContract(typeof(ServiceException), Name = "ServiceException")]
Standard AddOrder(string personId, int locationId, string order);
錯誤信息
傳入的消息具有意外的消息格式“原始”。 操作的預期消息格式是“Xml”; '傑森'。 這可能是因為尚未在綁定上配置 WebContentTypeMapper。 有關更多詳細信息,請參閱 WebContentTypeMapper 的文檔。
以這種方式更改您的合同:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "addorder", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
[FaultContract(typeof(ServiceException), Name = "ServiceException")]
Standard AddOrder(Order order);
您的Order
類在哪里:
[DataContract]
public class Order
{
[DataMember]
public string personId {get; set;}
[DataMember]
public int locationId {get; set;}
[DataMember]
public string order {get; set;}
}
現在,嘗試運行。 確保您的JSON
格式正確。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.