2014-06-09 55 views
0

我有一個restfull WCF服務,當我通過POST方法發送轉義的JSON數據時工作正常。但是當我發送未轉義的JSON時,會出現錯誤的錯誤請求。任何人都可以告訴我一個解決方案。這是我在WCF中使用的接口代碼。通過POST發送未轉義的JSON數據到WCF

[ServiceContract] 
    public interface IService1 
    { 
    [OperationContract] 
    [WebInvoke(Method = "POST", UriTemplate = "LoginCloudUser", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] 
    DNNLoginResponse LoginCloudUser(string args); 

    [OperationContract] 
    UserCredential GetDataUsingDataContract(UserCredential composite); 

    // TODO: Add your service operations here 
    } 

更新:我使用JSON.Net來序列化JSON。但我已經嘗試刪除所有的代碼,並以字符串形式返回參數。如果JSON沒有被轉義,它仍然會出錯。

public DNNLoginResponse LoginCloudUser(string args) 
    { 
     try 
     { 
      JsonSerializerSettings jss = new JsonSerializerSettings(); 
      jss.StringEscapeHandling = StringEscapeHandling.Default; 
      DNNLogin du = JsonConvert.DeserializeObject<DNNLogin>(args, jss); 
      bool validateresult = DNNLogin.ValidateUser(du); 

      DNNLoginResponse dlogres = new DNNLoginResponse(); 
      dlogres.result = validateresult; 
      dlogres.resulttype = "Success"; 
      dlogres.userid = du.username; 

      return dlogres; //JsonConvert.SerializeObject(dlogres, Formatting.None, jss); 
     } 
     catch 
     { 
      DNNLoginResponse dlogres = new DNNLoginResponse(); 
      dlogres.result = false; 
      dlogres.resulttype = "Internal Error"; 
      dlogres.userid = string.Empty; 

      return dlogres; 
     } 
    } 
+0

明顯的答案是逃避它......但是,你可以顯示一些數據,你試圖連載,所以我們可以看到你的確切問題? – Belogix

+0

是的,我現在已經更新了。 但我試圖從我的函數中刪除所有的代碼,仍然得到這個錯誤.. – Yesudass

回答

0

我已經刪除了JSON.NEt並將代碼更改爲此,其工作非常好。

[ServiceContract] 
public interface IService1 
{ 
    [OperationContract] 
    [WebInvoke(Method = "POST", UriTemplate = "LoginCloudUser", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] 
    DNNLoginResponse LoginCloudUser(DNNLogin args); 

    [OperationContract] 
    UserCredential GetDataUsingDataContract(UserCredential composite); 

    // TODO: Add your service operations here 
} 

和函數代碼。

 public DNNLoginResponse LoginCloudUser(DNNLogin args) 
     { 
     try 
     { 

      bool validateresult = DNNLogin.ValidateUser(args); 

      DNNLoginResponse dlogres = new DNNLoginResponse(); 
      if (validateresult) 
      { 
       dlogres.result = validateresult; 
       dlogres.resulttype = "Success" ; 
       dlogres.userid = args.username; 
      } 
      else 
      { 
       dlogres.result = validateresult; 
       dlogres.resulttype = "Login Error"; 
       dlogres.userid = string.Empty; 

      } 


      return dlogres; //JsonConvert.SerializeObject(dlogres, Formatting.None, jss); 
     } 
     catch 
     { 
      DNNLoginResponse dlogres = new DNNLoginResponse(); 
      dlogres.result = false; 
      dlogres.resulttype = "Internal Error"; 
      dlogres.userid = string.Empty; 

      return dlogres; 
     } 
    }