2015-06-20 184 views
0

我創建了一個休息WCF服務。服務本身工作正常。從本地主機瀏覽我可以調用所有功能。以下是合同和功能。REST WCF服務不返回響應

的問題是,當我嘗試使用db.savechanges保存()服務不返回響應。如果我註釋掉該行返回JSON響應。

難道我做錯了什麼在我的代碼?

[DataContract] 
    public class RouteObject{ 
    public List<DetailRouteInfo>routeDetail {get; set;} /// this is Entity Model Class; 
    } 
[OperationContract] 
     [WebGet(UriTemplate="GRDI?route={route}&cloc={cloc}", ResponseFormat=WebMessageFormat.Json)] 
     RouteObject GRDI(string route,string cLoc); 

public RouteObject GRDI(string route, string cLoc) { 

     MapApiClient mClient = new MapApiClient(); 
     Response mData= new Response(); 
     var coord=route.Split('|'); 
     RouteObject rObject = new RouteObject(); 
     rObject.routeDetail = new List<DriverRouteInfo>(); ; 
     //this variable needs to updated later 
     //List<string> dummyCoord= new List<string>(); 
     List<Task> wtask = new List<Task>(); 
     mClient.getRouteData(coord.ToList(), (x) => 
      { 
       mData = x; 
      }, 
      (y) => 
      { 
       wtask.Add(y); 
      } 
      ); 

     if (wtask != null) 
     { 
      Task.WaitAll(wtask.ToArray()); 
      wtask.Clear(); 
     } 
     DriverRouteInfo droute=null; 

// DO SOME MANIPULATION WITH THE DROUTE // 
// THEN ADD LIST OF DROUTE INTO ROUTOBJECT.DETAILROUTE // 

     try 
     { 
      using (var db = new F2PDatabaseEntities()) 
      { 
       UserInfo userinfo = db.UserInfoes.SingleOrDefault(u => u.UserId == 1); 
       //userinfo.DriverRouteInfoes = listRoute; 
       foreach(var dr in rObject.routeDetail){ 
        userinfo.DriverRouteInfoes.Add(dr); 
       } 
       db.SaveChanges(); 
       return rObject; 

      } 
      var check = droute.RouteId; 


     } 

     catch(SqlException ex) { 
      throw new FaultException(ex.Message); ; 
     } 

    } 

回答

0

檢查此鏈接:

http://zamd.net/2008/07/08/error-handling-with-webhttpbinding-for-ajaxjson/

另一個提示:

  • 把[數據成員]在

    public List<DetailRouteInfo>routeDetail {get; set;} 
    

    property屬性。並確保DetailRouteInfo類已經得到了[DataContract]屬性。

  • 不要向客戶端拋出異常。它會導致不良的結果,如果你不能正確處理。構建一個基本響應對象,其中包含一個用於返回響應代碼或消息的屬性,並從此基類繼承您的RouteObject類。