여기에 몇 가지 비슷한 질문이 있지만이 특정 문제에 대해서는 아무도 연구하지 않았습니다.WCF REST 서비스 : 메서드 매개 변수 (개체)가 항상 null입니다.
나는 WCF Rest 웹 서비스를 가지고 있으며 GET을 위해 잘 작동한다. 한 가지 방법은 POST이며 json 매개 변수를 전달할 때마다 메소드가 웹 서버에서 실행될 때 전달되는 매개 변수는 null입니다.
클라이언트 전화 :
public async Task Test()
{
HttpClient client;
client = new HttpClient();
client.MaxResponseContentBufferSize = 2147483646;
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
ContactParameter cp = new ContactParameter();
cp.ApptDateFrom = DateTime.Now;
cp.ApptDateTo = DateTime.Now.AddDays(1);
cp.Code = "00";
cp.Type = Enums.ContactType.Person;
cp.Status = string.Empty;
string RestUrl = "http://localhost:61919/data.svc/GetBooked";
var uri = new Uri(string.Format(RestUrl, string.Empty));
JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
{
DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
};
//json string = {"Code":"00","ApptDateTo":"2017-11-22T14:02:01.8758558+00:00","ApptDateFrom":"2017-11-21T14:02:01.8718458+00:00","Type":67,"Status":"A"}
var json = JsonConvert.SerializeObject(cp);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync(uri, content);
}
WCF 웹 서비스 계약 :
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetBooked")]
List<Contact> GetBooked(ContactParameter contactParameter);
연락 매개 변수 유형
01 여기 코드입니다namespace CatService.Types
{
[DataContract (Name = "contactParameter")]
public class ContactParameter
{
[DataMember(Name = "Code")]
public string Code { get; set; }
[DataMember(Name = "ApptDateTo")]
public DateTime ApptDateTo { get; set; }
[DataMember(Name = "ApptDateFrom")]
public DateTime ApptDateFrom { get; set; }
[DataMember(Name = "Type")]
public Enums.Enums.Type Type { get; set; }
[DataMember(Name = "Status")]
public string Status { get; set; }
}
}
WebMessageBodyStyle.Bare를 변경하려고 시도했지만 그 다음 StatusCode : 400, ReasonPhrase : '잘못된 요청'이 표시됩니다. 그냥 문자열을 받아들이는 테스트 메서드를 시도하면 작동합니다.