RESTful WCF 서비스 기능이 있지만 RestSharp 클라이언트를 통해 제대로 직렬화되지 않습니다.
[ServiceContract]
public interface IRestDragon
{
[OperationContract(Name = "getconfig")]
[WebInvoke(Method = "GET", UriTemplate = "getconfig/{id}", BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
DragonConfig GetConfig(string id);
}
public class RestDragon : IRestDragon
{
public DragonConfig GetConfig(string id)
{
var config = new DragonConfig {Name = "Test", Data = "DAtaaaaaa"};
return config;
}
}
서비스를 사용하는 방법은 다음과 같습니다.
static void Main(string[] args)
{
Console.ReadLine();
var client = new RestClient("http://localhost:5463/RESTDragon.svc");
client.AddDefaultHeader("ContentType", "application/json");
var request = new RestRequest("/getconfig/11123") {Method = Method.GET, RequestFormat = DataFormat.Json};
var response = client.Execute<DragonConfig>(request);
Console.WriteLine("Response: " + response.Content);
Console.ReadLine();
}
그러나 다음을 반환합니다.
Response: {"getconfigResult":{"Data":"DAtaaaaaa","Name":"Test"}}
response.Data. *를 통해 De-Serialized Data에 액세스 할 수 없습니다. null로 반환되고 데이터는 Content에 표시되지만 이상한 getconfigResult 식별자와 함께 JSON 형식으로 표시됩니다.
BodyStyle = WebMessageBodyStyle.Bare를 설정하면 문제가 해결되었습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다