【发布时间】:2016-09-14 20:27:16
【问题描述】:
由于某种原因,我的 AttributeContract 对象没有正确地从我的客户端传递到我的服务方法。我可以通过调用成功访问该方法,但对象为空。关于我在这里做错了什么有什么建议吗?
客户
using (var client = new HttpClient())
{
string serviceCall = string.Format("{0}AttributeService.svc/AttributeDefinition/", _serviceLocation);
int attributeIdInt = Convert.ToInt32(attributeId);
int objectIdInt = Convert.ToInt32(objectId);
AttributeContract attributeContract = new AttributeContract()
{
AttributeId = attributeIdInt,
AttributeValue = attributeValue,
ObjectId = objectIdInt,
ObjectType = objectType
};
string attributeString = JsonConvert.SerializeObject(attributeContract);
string requestJsonString = "{ \"attribute\" : " + attributeString + " }";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, serviceCall);
request.Content = new StringContent(requestJsonString, Encoding.UTF8, "application/json");
HttpResponseMessage response = client.SendAsync(request).Result;
}
数据合同
[DataContract(Name = "AttributeContract")]
public class AttributeContract
{
[DataMember(Name = "AttributeId")]
public int AttributeId { get; set; }
[DataMember(Name = "Attribute")]
public string Attribute { get; set; }
[DataMember(Name = "AttributeValue")]
public string AttributeValue { get; set; }
[DataMember(Name = "ObjectId")]
public int ObjectId { get; set; }
[DataMember(Name = "ObjectType")]
public string ObjectType { get; set; }
[DataMember(Name = "LastModifiedDate")]
public DateTime LastModifiedDate { get; set; }
[DataMember(Name = "LastModifiedUser")]
public string LastModifiedUser { get; set; }
}
服务合同
[OperationContract]
[WebInvoke(Method = "PUT",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "AttributeDefinition/")]
void UpdateAttributes(AttributeContract attribute);
服务方式
[OperationBehavior(TransactionScopeRequired = true)]
public void UpdateAttributes(AttributeContract attribute)
{
attribute.LastModifiedDate = DateTime.Now;
}
【问题讨论】: