简体   繁体   中英

.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:

[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. I'm using Json.net to serialize my data and then send it to the service.

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. The JSON looks good when I send it off, is there something I am missing here? How can I accomplish this? Thanks!

Found the issue was JSON.net serializing the DateTime in a way that WCF didn't like. To fix it I used JsonSerializerSettings:

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

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