REST WCF服务不会返回响应

时间:2015-06-20 14:52:31

标签: c# sql-server entity-framework wcf rest

我创建了一个休息wcf服务。服务本身工作正常。从localhost浏览我可以调用所有函数。以下是合同和功能。

问题是当我尝试使用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); ;
        }

    }

1 个答案:

答案 0 :(得分:0)

检查此链接:

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

另一个提示:

  • 将[DataMember]属性放在

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

    属性。并确保DetailRouteInfo类具有[DataContract]属性。

  • 不要向客户端抛出异常。如果您无法正确处理,会导致不良后果。构建一个基本响应对象,其中包含一个用于返回响应代码或消息的属性,并从该基类继承RouteObject类。