简体   繁体   English

.net 2.0客户端发布到.net 4.0 WCF Restful Service

[英].net 2.0 client POSTing to .net 4.0 WCF Restful Service

I have a .net 4.0 WCF servicing running with the following endpoint: 我有一个使用以下端点运行的.net 4.0 WCF服务:

[OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "UploadReport")]
    bool UploadReport(Report report);

My client is .net 2.0 and thus can't use [DataContract] on my objects. 我的客户端是.net 2.0,因此无法在我的对象上使用[DataContract]。 I'm using Json.net to serialize my data and then send it to the service. 我正在使用Json.net序列化我的数据,然后将其发送到服务。

string json = JsonConvert.SerializeObject(report);
        WebRequest request = HttpWebRequest.Create("http://localhost:51605/QBDHandoffService.svc/UploadReport");
        request.Method = "POST";
        request.ContentType = "application/json";

        using (var streamWriter = new StreamWriter(request.GetRequestStream()))
        {
            streamWriter.Write(json);
            streamWriter.Close();
        }
        var response = (HttpWebResponse)request.GetResponse();

The call successfully reaches the service but the 'report' is always null. 呼叫成功到达服务,但“报告”始终为空。 I suspect a serialization issue since I can't stick a [DataContract] on the object on the .net 2.0 side, but wasn't completely sure. 我怀疑存在序列化问题,因为我无法在.net 2.0端的对象上粘贴[DataContract],但并不确定。 The JSON looks good when I send it off, is there something I am missing here? 当我发送JSON时,JSON看起来不错,这里缺少什么吗? How can I accomplish this? 我该怎么做? Thanks! 谢谢!

Found the issue was JSON.net serializing the DateTime in a way that WCF didn't like. 发现问题在于JSON.net以WCF不喜欢的方式序列化DateTime。 To fix it I used JsonSerializerSettings: 要修复它,我使用了JsonSerializerSettings:

JsonSerializerSettings dateFormatSettings = new JsonSerializerSettings()
{
    DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
};
string json = JsonConvert.SerializeObject(report, dateFormatSettings);

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

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