我有我的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
[DataContract]
public class PrintOrder
{
[DataMember]
public string PrinterName{ get; set; }
[DataMember]
public string Body { get; set; }
}
问题是,每当我发送带有JSON主体的POST请求时,我的服务方法Print
总是接收一个空的PrintOrder
作为参数
{
"PrinterName":"EPSON 1200",
"Body":"test body"
}
将BodyStyle = WebMessageBodyStyle.WrappedRequest,
更改为BodyStyle = WebMessageBodyStyle.Bare,