2015-06-04 16 views
8

WCF (azure bulut) hizmetimde JSON'u desteklemek istiyorum. Her şeyin işe yarayıp yaramadığını görmek için bazı test yöntemleri oluşturuyorum. Ben GET görüşmeleri yapabilmek alabilirsiniz, ama ben basit parametresi olan bir POST yapıyorum zaman hep alacak: Ben bir parametre göndermek yoksaWCF JSON POST isteği, tekil dize parametresi bağlayıcı değil ve geri dönüyor 400

The remote server returned an error: (400) Bad Request. 

, bu yöntem çalıştırır, fakat Elbette parametre olarak bir null değeri. JSON ve WebMessageBodyStyle'ın farklı biçimlerini denedim, ancak hiçbiri işe yaramıyor.

Parametre türünü Stream olarak değiştirirsem verileri alırım, ancak el ile seri hale getirmem gerekiyor. Bu gerekli değil mi?

Arayüz:

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

Impl:

 public string Test(string data) 
     {   
      return "result is " + data; 
     } 

Testi istemcisi:

  WebClient client = new WebClient(); 
      client.Headers["Content-type"] = "application/json"; 
      client.Encoding = System.Text.Encoding.UTF8; 
      string jsonInput = "{'data':'testvalue'}"; 
      string postResponse = client.UploadString(postUrl, jsonInput); 
      Console.WriteLine("post response: " + postResponse); 

cevap

8

altın kombinasyonu WebMessageBodyStyle.WrappedRequest ile kombine JSON kodunda çift tırnak kullanmaktı.

Çalışma JSON: Bare için WebMessageBodyStyle ayarlarken

string jsonInput = "{\"data\":\"testvalue\"}"; 

aşağıdaki JSON çalışır:

string jsonInput = "\"testvalue\"";