Post JSON Object to WCF Service Oject always null

自作多情 提交于 2019-12-03 17:30:59

your JSON request should be something like this

[OperationContract]
        [WebInvoke(UriTemplate = "AccountAdd", Method = "POST")]
        SMProcessResponse<bool> AccountAdd(SMProcess<User> request);

and your JSOn Data

var userinfo = { "request": { "Action": { "Address": Adress, "Children": [], "CityId": 0, "Email": "waheed@gmail.com", "HomeUser": true, "ImagePath": "C:/Waheed.jpg", "IpAdress": "192.168.1.3", "IsActive": true, "LastLogin": "\/Date(1358852299323+0500)\/", "Name": "Waheed Iqbal", "Password": "111111", "PhoneNumber": "+923226270150", "ProfileHit": 10, "ShowEmail": true, "ShowPhoneNumber": true, "SubscribeNews": true, "UserID": 1}} }

after editing Web.config

<behavior name="Services.webHttpBehavior">
          <webHttp defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true" defaultBodyStyle="WrappedRequest" />
        </behavior>

cheers

You need to have correct WebInvoke attribute applied to WCF operation. Looking at your JSON request, I believe it should be something like

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json))]
SMProcessResponse<bool> AccountAdd(SMProcess<User> request)

Note a wrapped body style (or WrappedRequest) is needed so that input JSON would be treated as wrapped one (i.e. parameter name would be the property on incoming JSON).

Apart from this, you might get into issues with your SMProcess<T> de-serialization because it does not have any parameter less constructor.

Anyway, I would advise you start with a simple service declaration (and implementation) such as

public SMProcessResponse<bool> AccountAdd(User request)
{
   return new SMProcessResponse<bool>(MembershipController.AccountAdd(request));
}

And then start adding extra nesting etc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!