我有一个 wcf 服务和一个发送插入或更新客户请求的角度应用程序。 wcf 服务收到请求,调用正确的方法,但方法参数值为空。我曾尝试将 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"}
最佳答案
您正在使用包装请求,像这样包装您的 JSON:
{"customer":{"Password":"passwordtest","Name":"nametest","NewPassword":"newPasswordTest","Type":1,"Email":"test@test.be", "Addresses":"null", "CustomerLicenses":"null" }}
关于c# - wcf方法参数中的反序列化json为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39788406/