【问题标题】:WCF REST Service: Method parameter (object) is always nullWCF REST 服务:方法参数(对象)始终为空
【发布时间】:2018-05-05 01:41:33
【问题描述】:

我在这里看到了一些类似的问题,但没有一个适用于这个特定问题。

我有一个 WCF Rest Web 服务,它对 GET 工作正常。一种方法是 POST,每当我通过 json 参数调用它时,当该方法在 Web 服务器上运行时,传递的参数为 null。

代码如下:

客户电话:

   public async Task Test()
    {
        HttpClient client;
        client = new HttpClient();
        client.MaxResponseContentBufferSize = 2147483646;
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        ContactParameter cp = new ContactParameter();
        cp.ApptDateFrom = DateTime.Now;
        cp.ApptDateTo = DateTime.Now.AddDays(1);
        cp.Code = "00";
        cp.Type = Enums.ContactType.Person;
        cp.Status = string.Empty;

        string RestUrl = "http://localhost:61919/data.svc/GetBooked";
        var uri = new Uri(string.Format(RestUrl, string.Empty));

        JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
        {
            DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
        };

        //json string = {"Code":"00","ApptDateTo":"2017-11-22T14:02:01.8758558+00:00","ApptDateFrom":"2017-11-21T14:02:01.8718458+00:00","Type":67,"Status":"A"}
        var json = JsonConvert.SerializeObject(cp);

        var content = new StringContent(json, Encoding.UTF8, "application/json");
        var response = await client.PostAsync(uri, content);
    }

WCF Web 服务合同:

    [OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetBooked")]

    List<Contact> GetBooked(ContactParameter contactParameter);

联系人参数类型

namespace CatService.Types
    {
        [DataContract (Name = "contactParameter")]
        public class ContactParameter
            {
                [DataMember(Name = "Code")]
                public string Code { get; set; }
                [DataMember(Name = "ApptDateTo")]
                public DateTime ApptDateTo { get; set; }
                [DataMember(Name = "ApptDateFrom")]
                public DateTime ApptDateFrom { get; set; }
                [DataMember(Name = "Type")]
                public Enums.Enums.Type Type { get; set; }
                [DataMember(Name = "Status")]
                public string Status { get; set; }

            }
   }

我尝试更改 WebMessageBodyStyle.Bare,但随后我得到一个 StatusCode:400,ReasonPhrase:'Bad Request'。 如果我尝试一个只接受一个字符串的测试方法,它就可以工作。

【问题讨论】:

    标签: rest wcf datacontract datamember


    【解决方案1】:

    我也遇到了同样的问题。我建议,您应该在另一个控制台应用程序上尝试您的 Post 方法。因为 POST 方法不在 URL 上。

    【讨论】:

      【解决方案2】:

      这是一个日期时间问题,我没有在之前的帖子中实现答案。基本上:

      JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
              {
                  DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
              };
      

      那么当你序列化时......

       var json = JsonConvert.SerializeObject(jsonString, microsoftDateFormatSettings);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-15
        • 2011-08-28
        • 1970-01-01
        • 1970-01-01
        • 2016-08-13
        • 2012-03-18
        • 2016-06-27
        • 1970-01-01
        相关资源
        最近更新 更多