POST to WCF from Fiddler succeeds but passes null values

前端 未结 2 513
栀梦
栀梦 2021-01-03 09:52

I\'ve followed this video series on WCF and have the demo working. It involves building a WCF service that manages student evaluation forms, and implements CRUD operations

相关标签:
2条回答
  • 2021-01-03 10:23

    For POST you need to use BodyStyle = WebMessageBodyStyle.Bare NOT
    WebMessageBodyStyle.Wrapped

        [OperationContract]
        [WebInvoke(RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json,UriTemplate = "eval",BodyStyle = WebMessageBodyStyle.Bare)]
        void SubmitEval(Eval eval);
    
    0 讨论(0)
  • 2021-01-03 10:30

    The default body style in the WebInvoke attribute is Bare, meaning that the object should be sent without a wrapper containing the object name:

    {"Comment":"testComment222",
     "Id":"doesntMatter",
     "Submitter":"Tom",
     "TimeSubmitted":"11/10/2011 4:00 PM"}
    

    Or you can set the request body style to Wrapped, and that would make the input require the wrapping {"eval"...} object:

    [OperationContract]
    [WebInvoke(RequestFormat=WebMessageFormat.Json,
       ResponseFormat=WebMessageFormat.Json,
       UriTemplate = "eval",
       BodyStyle = WebMessageBodyStyle.WrappedRequest)] // or .Wrapped
    void SubmitEval(Eval eval);
    

    Update: there's another problem in your code, since you're using DateTime, and the format expected by the WCF serializer for dates in JSON is something like \/Date(1234567890)\/. You can change your class to support the format you have by following the logic described at MSDN Link (fine-grained control of serialization format for primitives), and shown in the code below.

    public class StackOverflow_8086483
    {
        [DataContract]
        public class Eval //Models an evaluation
        {
            [DataMember]
            public string Id { get; set; }
    
            [DataMember]
            public string Submitter { get; set; }
    
            [DataMember]
            public string Comment { get; set; }
    
            [DataMember(Name = "TimeSubmitted")]
            private string timeSubmitted;
    
            public DateTime TimeSubmitted { get; set; }
    
            public override string ToString()
            {
                return string.Format("Eval[Id={0},Submitter={1},Comment={2},TimeSubmitted={3}]", Id, Submitter, Comment, TimeSubmitted);
            }
    
            [OnSerializing]
            void OnSerializing(StreamingContext context)
            {
                this.timeSubmitted = this.TimeSubmitted.ToString("MM/dd/yyyy h:mm tt", CultureInfo.InvariantCulture);
            }
    
            [OnDeserialized]
            void OnDeserialized(StreamingContext context)
            {
                DateTime value;
                if (DateTime.TryParseExact(this.timeSubmitted, "MM/dd/yyyy h:mm tt", CultureInfo.InvariantCulture, DateTimeStyles.None, out value))
                {
                    this.TimeSubmitted = value;
                }
            }
        }
    
        [ServiceContract]
        public interface IEvalService
        {
            [OperationContract]
            [WebInvoke(RequestFormat = WebMessageFormat.Json,
               ResponseFormat = WebMessageFormat.Json, UriTemplate = "eval",
               BodyStyle = WebMessageBodyStyle.Wrapped)]
            void SubmitEval(Eval eval);
        }
    
        [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
        class EvalService : IEvalService
        {
            public void SubmitEval(Eval eval)
            {
                Console.WriteLine("Received eval: {0}", eval);
            }
        }
    
        public static void Test()
        {
            string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
            WebServiceHost host = new WebServiceHost(typeof(EvalService), new Uri(baseAddress));
            host.Open();
            Console.WriteLine("Host opened");
    
            string data = "{\"eval\":{\"Comment\":\"testComment222\",\"Id\":\"doesntMatter\", \"Submitter\":\"Tom\",\"TimeSubmitted\":\"11/10/2011 4:00 PM\"}}";
            WebClient c = new WebClient();
            c.Headers[HttpRequestHeader.ContentType] = "application/json; charset=utf-8";
            c.UploadData(baseAddress + "/eval", Encoding.UTF8.GetBytes(data));
    
            Console.Write("Press ENTER to close the host");
            Console.ReadLine();
            host.Close();
        }
    }
    
    0 讨论(0)
提交回复
热议问题