[英]C# HTTP Server POST request is parsing the body as null
我有我的 ServiceContract 接口
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
UriTemplate = "print")]
[return: MessageParameter(Name = "Data")]
int Print(PrintOrder orden);
}
服务:
public class Service : IService
{
public int Print(PrintOrder orden)
{
try
{
Console.WriteLine("received order",orden.Body,orden.PrinterName);
return 0;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message,ex.StackTrace);
return 1;
}
}
}
和数据合同
[DataContract]
public class PrintOrder
{
[DataMember]
public string PrinterName{ get; set; }
[DataMember]
public string Body { get; set; }
}
问题是每次我发送带有 JSON 正文的 POST 请求时,我的服务方法Print
总是接收 null PrintOrder
作为参数:
{
"PrinterName":"EPSON 1200",
"Body":"test body"
}
将BodyStyle = WebMessageBodyStyle.WrappedRequest,
更改为BodyStyle = WebMessageBodyStyle.Bare,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.