简体   繁体   English

WCF将不接受具有datetime属性的对象(400错误的请求)

[英]WCF will not accept an object with a datetime property (400 bad request)

This is my first post on SO, so please forgive me if I don't provide enough information. 这是我关于SO的第一篇文章,所以如果我没有提供足够的信息,请原谅我。 I have a WPF app that is trying to send an EmployeeLogin object that contains a DateTime property to my WCF web service and I keep getting a 400 bad request error when calling this EmployeeLogIn() method. 我有一个WPF应用,试图将包含DateTime属性的EmployeeLogin对象发送到WCF Web服务,并且在调用此EmployeeLogIn()方法时,始终收到400错误的请求错误。 I have searched so many places for an answer, but I can't get a direct answer to my specific situation. 我已经在很多地方搜索了答案,但是我无法直接回答我的具体情况。

So my main question is: Does sending an object with a DateTime property that has been set via the method below automatically get converted to the JSON format ( \\/Date(1237951967000)\\/ ) and get accepted by the wcf service as a valid DateTime property, or does it convert it to the above JSON string and get passed to the method as a string? 所以我的主要问题是:发送具有通过以下方法设置的DateTime属性的对象是否自动转换为JSON格式( \\/Date(1237951967000)\\/ ),并被wcf服务接受为有效的DateTime属性,还是将其转换为上述JSON字符串并以字符串形式传递给方法?

EDITv2: My WPF app is not sending this DateTime object in the above correct format and instead sending it as "2017-08-11T13:47:55.2187627-04:00" and I can't figure out a way to get it into the correct DateTime format. EDITv2:我的WPF应用未以上述正确格式发送此DateTime对象,而是将其发送为"2017-08-11T13:47:55.2187627-04:00" ,但我想不出一种方法将其放入正确的DateTime格式。 I can get it into this format as a string, but for the reasons stated at the end of this post, that would be a last-ditch effort. 我可以将它作为字符串形式转换为这种格式,但是由于本文结尾所述的原因,这将是最后的努力。

 EmployeeLogin EmpLogin = new EmployeeLogin { EmployeeID = (int)UserName, Password = Pass, Latitude = 33.45, Longitude = 134.44, TimeIn = DateTime.Now }; HttpResponseMessage response = await Client.PostAsJsonAsync(string.Format(@"{0}{1}", URL, method), EmpLogin); 

Where EmployeeLogin is defined below: EmployeeLogin的定义如下:

 [DataContract] public class EmployeeLogin { [DataMember] public int EmployeeID { get; set; } [DataMember] public string Password { get; set; } [DataMember] public DateTime TimeIn { get; set; } [DataMember] public Nullable<double> Latitude { get; set; } [DataMember] public Nullable<double> Longitude { get; set; } } 

and the EmployeeLogIn method signature is as follows: EmployeeLogIn方法签名如下:

 [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedResponse)] [return: MessageParameter(Name = "Data")] [OperationContract] List<int> EmployeeLogIn(EmployeeLogin e); 

I know the easiest solution is to change the DateTime to a string property and convert it back to a DateTime on the web service side, but I'm trying not to make any changes to the web service unless absolutely necessary. 我知道最简单的解决方案是将DateTime更改为string属性,然后在Web服务端将其转换回DateTime ,但是除非绝对必要,否则我将尝试不对Web Service进行任何更改。 Thank you in advance. 先感谢您。

Ok I finally found the answer to this question after weeks of searching. 好了,经过数周的搜索,我终于找到了该问题的答案。 The problem I was experiencing is explained fully in the post linked to below, and the solution to the problem that is offered in the post worked perfectly for me. 我所遇到的问题在下面链接的文章中得到了充分说明,并且该文章中提供的问题的解决方案对我来说非常有效。 Here is the post with the solution to my problem. 这是解决我的问题的帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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