简体   繁体   中英

C# HTTP Server POST request is parsing the body as null

I have my ServiceContract interface

[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);
}

The Service:

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;
            }
        }
    }

And the DataContract

[DataContract]
    public class PrintOrder
    {
        [DataMember]
        public string PrinterName{ get; set; }
        [DataMember]
        public string Body { get; set; }
    }

the problem is that my service method Print is always receiving a null PrintOrder as parameter every time I send a POST request with JSON body:

{
  "PrinterName":"EPSON 1200",
  "Body":"test body"
}

Changed BodyStyle = WebMessageBodyStyle.WrappedRequest, to BodyStyle = WebMessageBodyStyle.Bare,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM