将对象参数作为JSON对象发送到WCF服务方法时获取空值

本文关键字:对象 服务 WCF 方法 获取 空值 参数 JSON | 更新日期: 2023-09-27 18:16:02

这是我的服务方法签名:

    [OperationContract]
    [System.ServiceModel.Web.WebInvoke(UriTemplate = "/RegisterUser", Method = "POST")]
    void RegisterNewUser(User user);

Type User在类上有DataContract属性,在属性上有DataMember属性

,下面是我如何调用服务方法的:

 String data = "{'"user'":{'"__type'" : '"User:#PingMe'",'"EmailID'": '"something@something.com'",'"RegistrationID'": '"sdfhjklsdgkdfjgklgdjfklg'"}}";  
        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:2443/NotificationService.svc/RegisterUser");
        httpWebRequest.Method = "POST";
        byte[] bytes = Encoding.UTF8.GetBytes(data);
        httpWebRequest.ContentLength = bytes.Length;
        httpWebRequest.ContentType = "text/json; charset=utf-8";
            httpWebRequest.KeepAlive = false;
        Stream requestStream = httpWebRequest.GetRequestStream();
        requestStream.Write(bytes,0,bytes.Length);
        requestStream.Close();

        HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

它成功地调用了服务方法,但是在服务方法的user参数user中。EmailID和用户。RegistrationID总是'NULL'

知道我在这里错过了什么吗?

我需要设置RequestFormat属性为WebMessageFormat.JSON吗?在OperationContract属性?

谢谢

将对象参数作为JSON对象发送到WCF服务方法时获取空值

修改

httpWebRequest.ContentType = "text/json; charset=utf-8";

:

httpWebRequest.ContentType = "application/json; charset=utf-8";

:

[System.ServiceModel.Web.WebInvoke(UriTemplate = "/RegisterUser", Method = "POST")]

:

[System.ServiceModel.Web.WebInvoke(UriTemplate = "/RegisterUser", Method = "POST", BodyStyle=WebMessageBodyStyle.WrappedRequest,
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json)]

在您的服务器增加MaxpostSize属性我对IIS了解不多,但我想它会对你有用的。

 <requestLimits maxAllowedContentLength ="<length>" />