C# 将对象参数作为JSON对象发送到WCF服务方法时获取空值
这是我的服务方法签名:C# 将对象参数作为JSON对象发送到WCF服务方法时获取空值,c#,wcf,json,rest,C#,Wcf,Json,Rest,这是我的服务方法签名: [OperationContract] [System.ServiceModel.Web.WebInvoke(UriTemplate = "/RegisterUser", Method = "POST")] void RegisterNewUser(User user); 另外,类型User在类上具有DataContract属性,在其属性上具有DataMember属性 下面是我调用服务方法的方式: String data = "{\"user\
[OperationContract]
[System.ServiceModel.Web.WebInvoke(UriTemplate = "/RegisterUser", Method = "POST")]
void RegisterNewUser(User user);
另外,类型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.EmailID和user.RegistrationID中总是出现“NULL”
你知道我在这里遗漏了什么吗
是否需要将RequestFormat属性设置为WebMessageFormat.JSON?操作中的契约属性
谢谢你改变这个
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>" />
您可以发布用户类的定义吗?您是否已将[DataMember]属性设置为您的用户属性?