어떤 이유로 내 AttributeContract 개체가 내 클라이언트에서 내 서비스 메서드로 올바르게 전달되지 않습니다. 호출을 통해 메서드에 액세스 할 수 있지만 개체가 비어 있습니다. 내가 여기서 뭘 잘못하고 있는지에 대한 제안은?WCF REST 복잡한 개체 게시
클라이언트
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;
}
예 -이 방법은 실수 였으므로 이후 수정되어 동일한 결과가 나타납니다. 같은 개체 이름을 사용해야하는 개체 이름은 무엇입니까? JSON 내의 객체 이름과 서비스 계약, 서비스 메소드 ...? 나는 이것이 가장 명백한 문제 인 것처럼 여러 가지 다른 명명 규칙을 시도했다. – crowemi
객체 이름이나 메소드 변수 이름을 사용하지 마십시오. 속성 만 보내기 : {AttributeId : 1, Attribute : "ss"} – mkysoft
먼저 C# 코드가 아닌 도구로 서비스 테스트를 수행하십시오. 예를 들어 크롬 브라우저에는 이름이 우편 배달부 인 확장자가 있습니다. – mkysoft