代码之家  ›  专栏  ›  技术社区  ›  E. Williams

C#OperationContract POST请求正在分析空正文

  •  0
  • E. Williams  · 技术社区  · 3 年前

    我有我的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; }
        }
    

    问题是我的服务方式 Print 总是接收空值 PrintOrder 作为参数,每次我发送带有JSON正文的POST请求时:

    {
      "PrinterName":"EPSON 1200",
      "Body":"test body"
    }
    
    0 回复  |  直到 3 年前
        1
  •  0
  •   E. Williams    3 年前

    改变 BodyStyle = WebMessageBodyStyle.WrappedRequest, BodyStyle = WebMessageBodyStyle.Bare,