2012-03-15 80 views
2

我有一個wcf服務,我想測試發佈數據到它。但是我的函數的參數從來沒有得到任何值。WCF WebInvoke方法POST

[OperationContract] 
[WebInvoke(UriTemplate = "TestPost", Method = "POST", 
     ResponseFormat = WebMessageFormat.Json, 
     RequestFormat = WebMessageFormat.Json, 
     BodyStyle = WebMessageBodyStyle.WrappedRequest)] 
int Test(string value); 

public int Test(string value) //Value stays null 
{ 
    return 0; 
} 

的JSON請求我送,建立使用Fiddler2

http://localhost:49633/Service1.svc/TestPost 

User-Agent: Fiddler 
Host: localhost:49633 
Content-Length: 42 
Content-type: application/json 

{"value":{"name":"value","name1":"value"}} 

我想要的參數包含一個JSON字符串,所以基本上我創建一個包含JSON對象JSON請求,因爲我想稍後將JSON對象反序列化爲我的自定義對象之一。任何想法爲什麼值參數保持爲空?

感謝

+0

的參數是一個字符串,所以它的值應該是一個字符串了。嘗試傳遞'{「值」:「{\」name \「:\」value \「,\」name1 \「:\」value \「}」}'代替。雖然我很驚訝,但服務不會給你一個錯誤,而不是一個空的參數。 – shambulator 2012-03-15 09:22:04

+0

您可以嘗試將BodyStyle移除爲WrappedRequest的默認值 – Rajesh 2012-03-15 10:06:01

回答

4

我用下面的方法來發布JSON字符串上面定義的服務,它爲我工作:

我的服務:

[WebInvoke(UriTemplate = "TestPost", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] 
     public int Test(string value) 
     { 
      Console.Write(value); 
      return 1; 
     } 

我的客戶:

private string UseHttpWebApproach(string serviceUrl, string resourceUrl, string method, string requestBody) 
     { 
      string responseMessage = null;     
      var request = WebRequest.Create(string.Concat(serviceUrl, resourceUrl)) as HttpWebRequest; 
      if (request != null) 
      {      
       request.ContentType = "application/json"; 
       request.Method = method; 
      } 

      //var objContent = HttpContentExtensions.CreateDataContract(requestBody); 
      if(method == "POST" && requestBody != null) 
      {     
       byte[] requestBodyBytes = ToByteArrayUsingJsonContractSer(requestBody);     
       request.ContentLength = requestBodyBytes.Length; 
       using (Stream postStream = request.GetRequestStream()) 
        postStream.Write(requestBodyBytes, 0, requestBodyBytes.Length); 

      } 

      if (request != null) 
      { 
       var response = request.GetResponse() as HttpWebResponse; 
       if(response.StatusCode == HttpStatusCode.OK) 
       { 
        Stream responseStream = response.GetResponseStream(); 
        if (responseStream != null) 
        { 
         var reader = new StreamReader(responseStream); 

         responseMessage = reader.ReadToEnd(); 
        } 
       } 
       else 
       { 
        responseMessage = response.StatusDescription; 
       } 
      } 
      return responseMessage; 
     } 

private static byte[] ToByteArrayUsingJsonContractSer(string requestBody) 
     { 
      byte[] bytes = null; 
      var serializer1 = new DataContractJsonSerializer(typeof(string)); 
      var ms1 = new MemoryStream(); 
      serializer1.WriteObject(ms1, requestBody); 
      ms1.Position = 0; 
      var reader = new StreamReader(ms1); 
      bytes = ms1.ToArray(); 
      return bytes; 
     } 

我在客戶端使用UseHttpWebApproach的呼叫如下:

string serviceBaseUrl = <<your service base url>>; 
string resourceUrl = "/TestPost"; 
string method = "POST"; 
string jsonText = "{\"value\":{\"name\":\"value\",\"name1\":\"value\"}}"; 
UseHttpWebApproach(serviceBaseUrl, resourceUrl, method, jsonText); 

下面是我的小提琴手請求:

POST http://localhost/VDName/AppName/TestPost HTTP/1.1 
Content-Type: application/json 
Content-Length: 54 
Connection: Keep-Alive 

"{\"value\":{\"name\":\"value\",\"name1\":\"value\"}}" 
相關問題