在wcf方法参数中反序列化json为null

时间:2016-09-30 09:40:29

标签: c# json wcf parameters null

我有一个wcf服务和一个角度应用程序,它发送插入或更新客户的请求。 wcf服务接收请求,调用正确的方法但方法参数值为null。我已尝试将WebMessageBodyStyle更改为Bare,但后来甚至没有调用方法本身。我尝试了[FromBody]属性,包括对象和字符串参数。两者都输入方法但参数值仍为null。我没有想法。帮助将不胜感激。

        [ServiceContract]
    public interface ICustomerService
    {
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest,
            RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        [OperationContract]
        bool InsertOrUpdateCustomer(Customer customer);

    }
    [DataContract]
    public class Customer
    {
        [DataMember]
        public CustomerType Type { get; set; }
        [DataMember]
        public String Name { get; set; }
        [DataMember]
        public string Email { get; set; }
        [DataMember]
        public String Password { get; set; }
        [DataMember]
        public String NewPassword { get; set; }
        [DataMember]
        public virtual IList<Address> Addresses { get; set; }
        [DataMember]
        public virtual IList<CustomerLicense> CustomerLicenses { get; set; }
    }

    public enum CustomerType
    {
        Organizational = 0,
        Individual = 1
    }
    [DataContract]
    public class Address
    {
    }
    [DataContract]
    public class CustomerLicense
    {
    }

    public class Service : ICustomerService
    {

        public bool InsertOrUpdateCustomer(Customer customer)
        {
            try
            {
                if (customer == null)
                    throw new ArgumentNullException(nameof(customer));
                //do work for insert/update
                return true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                return false;
            }
        }
    }

下面我将请求Angular应用程序发送到wcf服务:

POST http://localhost:49258/CustomerSecurityService.svc/CustomerSecurityService/InsertOrUpdateCustomer HTTP/1.1
Host: localhost:49258
Connection: keep-alive
Content-Length: 109
Accept: text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8
Origin: http://localhost:9342
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36
Authorization: Basic c2FtZXByb2JsZW06bW9yZWNvZGU=
Content-Type: application/json; charset=UTF-8
Referer: http://localhost:9342/index.html/
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6

{"Password":"passwordtest","Name":"nametest","NewPassword":"newPasswordTest","Type":1,"Email":"test@test.be"}

1 个答案:

答案 0 :(得分:0)

您正在使用包装请求,请将您的JSON打包为:

{"customer":{"Password":"passwordtest","Name":"nametest","NewPassword":"newPasswordTest","Type":1,"Email":"test@test.be", "Addresses":"null", "CustomerLicenses":"null" }}
相关问题