2014-04-25 2 views
3

요청 본문을 구문 분석하기 위해 WCF REST와 어려움을 겪고 있습니다. 내 문제 :WCF REST가 요청 본문을 구문 분석 할 수 없습니다.

  • POST 메서드 https : // {Service}/X/Y? Z = 0000000
  • 바디 요청 : { "ID": "ID가"}

내 코드 :

[ServiceContract] 
public interface IService 
{ 
    [OperationContract] 
    [WebInvoke(Method = "POST", 
       BodyStyle = WebMessageBodyStyle.Bare, 
       RequestFormat = WebMessageFormat.Json, 
       ResponseFormat = WebMessageFormat.Json, 
       UriTemplate = "/X/{y}?Z={z}") 
       ] 
    string GetRequest(string y, string z, Request request); 
} 
public class Service : IService 
{ 
    public string GetRequest(string y, string z, Request request) 
    { 
     //Do sth 
    } 


[DataContract] 
public class Request 
{ 
    [DataMember] 
    [JsonProperty("id")] 
    public String Id { get; set; } 
} 

내가 가진 문제가 있음을 y를 z는 데이터를 가지고 있으며 그들은 정확합니다. 그러나 요청의 ID는 null입니다. 나는 "id"가 될 것으로 예상했다.

인터넷에서 많은 검색을했는데이 경우 쉽게 따라갈 수없는 스트림 솔루션을 발견했습니다. 누구든지이 일을하는 영리한 생각이 있는지 궁금합니다.

+0

이상한 ... 비슷한 일이 저에게는 효과적이지만 BodyStyle, RequestFormat 및 ResponseFormat도 설정하지 않았습니다. – PierrOz

+0

당신이 그들을 필요로하지 않고 작동한다는 것은 더욱 이상합니다. 특별하다고 할 수있는 특별한 언급이나 특별한 언급이 있습니까? – ShrnPrmshr

+0

web.config가 어떤지 알려주실 수 있습니까? – ShrnPrmshr

답변

5

이 버전

본 서비스에 봐

namespace ServiceTest 
{ 
    internal class Program 
    { 
     private static WebServiceHost _service; 

     private static void Main() 
     { 
      _service = new WebServiceHost(typeof (Service)); 
      Console.WriteLine("The service has started"); 
      _service.Open(); 
      Console.ReadKey(); 
     } 
    } 

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 
    public sealed class Service : IService 
    { 
     public string GetRequest(string y, string z, Request request) 
     { 
      Console.WriteLine("Y: {0}, Z: {1}, request: {2}", y, z, request); 
      return "Ok"; 
     } 
    } 

    [ServiceContract] 
    public interface IService 
    { 
     [OperationContract] 
     [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, 
      ResponseFormat = WebMessageFormat.Json, UriTemplate = "/X/{y}?Z={z}")] 
     string GetRequest(string y, string z, Request request); 
    } 

    [DataContract] 
    public sealed class Request 
    { 
     [DataMember] 
     public string Id { get; set; } 

     public override string ToString() 
     { 
      return string.Format("Id: {0}", Id); 
     } 
    } 
} 

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 

    <system.serviceModel> 
     <services> 
      <service name="ServiceTest.Service"> 
       <host> 
        <baseAddresses> 
         <add baseAddress="http://localhost:9090/webhost" /> 
        </baseAddresses> 
       </host> 
       <endpoint binding="webHttpBinding" contract="ServiceTest.IService" /> 
      </service> 
     </services> 
    </system.serviceModel> 

    <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
    </startup> 
</configuration> 

클라이언트

internal class Program 
{ 
    private static void Main(string[] args) 
    { 
     var client = new HttpClient(); 
     var request = new Request {Id = "Nelibur"}; 
     var result = client.PostAsync("http://localhost:9090/webhost/X/Y?Z=2000", CreateContent(request)).Result; 
     Console.WriteLine(result.Content.ReadAsStringAsync().Result); 
     Console.ReadKey(); 
    } 

    private static StringContent CreateContent<T>(T value) 
    { 
     using (var stream = new MemoryStream()) 
     { 
      var serializer = new DataContractJsonSerializer(typeof (T)); 
      serializer.WriteObject(stream, value); 
      string content = Encoding.UTF8.GetString(stream.ToArray()); 
      return new StringContent(content, Encoding.UTF8, "application/json"); 
     } 
    } 
} 

원시 요청 :

POST http://localhost:9090/webhost/X/Y?Z=2000 HTTP/1.1 
Content-Type: application/json; charset=utf-8 
Host: localhost:9090 
Content-Length: 16 
Expect: 100-continue 
Connection: Keep-Alive 

{"Id":"Nelibur"} 

당신은에서 요청을 사용하려고 할 수 있습니다 예 : Fiddler

관련 문제