2016-09-30 99 views
1

我有一個wcf服務和一個發送插入或更新客戶請求的角度應用程序。 wcf服務接收請求,調用正確的方法,但方法參數值爲空。我試圖將WebMessageBodyStyle改爲Bare,但是這個方法本身甚至沒有被調用。我已經嘗試使用[FromBody]屬性,都使用對象和字符串參數。兩者都輸入方法,但參數值仍爲空。我沒有想法。幫助將不勝感激。反序列化wcf方法中的json參數爲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; 
      } 
     } 
    } 

下面我發送到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":"[email protected]"}

回答

0

您正在使用包裝的請求,包你喜歡JSON:

{"customer":{"Password":"passwordtest","Name":"nametest","NewPassword":"newPasswordTest","Type":1,"Email":"[email protected]", "Addresses":"null", "CustomerLicenses":"null" }}