WCF REST后复杂对象

时间:2016-09-14 20:27:17

标签: c# json rest wcf

由于某种原因,我的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;    
}

3 个答案:

答案 0 :(得分:0)

您的方法不匹配:在服务合同上,它被输入为PUT,但在客户端上它是POST。你的对象名称也不匹配:attribute =! AttributeContract

答案 1 :(得分:0)

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
    };

                    var request =
                        (HttpWebRequest)
                            WebRequest.Create(new Uri(serviceCall );
                    request.ContentType = "application/json";
                    request.Method = "PUT";
                    var itemToSend = JsonConvert.SerializeObject(
                        attributeContract 
                        );
                    using (var streamWriter = new StreamWriter(await request.GetRequestStreamAsync()))
                    {
                        streamWriter.Write(itemToSend);
                        streamWriter.Flush();
                        streamWriter.Dispose();
                    }

试试此代码

答案 2 :(得分:0)

原来你不需要JSON中的对象包装器(每个mkysoft)。在fiddler中进行了一些挖掘,发现我在数据合同上反序列化datetime属性时遇到了问题。我将属性从DateTime更改为字符串,同时确定最佳路径。

相关问题