WCF服务,从数据库加载数据

时间:2012-10-07 10:58:05

标签: asp.net-mvc database wcf wcf-data-services

我在asp.net mvc应用程序中遇到了WCF服务的问题。 请帮帮我! 当我运行我的项目时,某些实体的某些数据正在从数据库加载,但某些数据未加载(内部服务器错误)。

例外代码和说明:

EntityTypeController:

public HttpResponseMessage GetAll()
{
       //for debug
       var t = EntityTypeService.GetAll();

       //some exception here!
       /*
    The request channel timed out while waiting for a reply after 00:00:59.9979999. 
    Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. 
    The time allotted to this operation may have been a portion of a longer timeout.
    */
    //SendTimeout is 00:30:00 for all services.

    or

    /*The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.*/

    or

    /*An error occurred while receiving the HTTP response to http://localhost:18822/Services/Department.svc. 
    This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an 
    HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.*/


       return CreateResponse<IEnumerable<EntityType>>(EntityTypeService.GetAll);
}

EntityTypeService:

public class EntityTypeService : BaseService<Base.Entities.EntityType>, IEntityTypeService
{
        public IQueryable<Base.Entities.EntityType> GetAll()
        {
            var t = Repository.Get();
            return t;
        }
}

存储库:

public class Repository<TEntity>: IRepository<TEntity> where TEntity: BaseEntity
{
        [Inject]
        public IDataContext Context { get; set; }

        [Inject]
        public IDatabase DataSource { get; set; }

        /// <summary>
        /// Get entities from repository
        /// </summary>
        /// <param name="filter">Filter</param>
        /// <param name="orderBy">Order by property</param>
        /// <param name="includeProperties"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> Get(
            Expression<Func<TEntity, bool>> filter = null,
            Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
            string includeProperties = "")
        {
            var query = Context.Set<TEntity>().AsQueryable();

            // Apply filter
            filter.Do((s) => { 
                query = query.Where(filter); 
            });


            // Apply includes
            foreach (var includeProperty in includeProperties.Split(new [] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                query = query.Include(includeProperty);
            }

            // Apply order by if need

            var result = orderBy
                .With(x => x(query))
                .Return(x => x, query);


            return result;
        }


        /// <summary>
        /// Find entity by key
        /// </summary>
        /// <param name="id">Identificator</param>
        /// <returns>Entity</returns>
        public virtual TEntity GetById(object id)
        {
            //throw new NotImplementedException();
            return Context.Set<TEntity>().Find(id);
        }

        /// <summary>
        /// Add new entity to repository
        /// </summary>
        /// <param name="entity">New entity</param>
        public virtual void Insert(TEntity entity)
        {
            AttachIsNotAttached(entity);
            Context.Set<TEntity>().Add(entity);
            Context.SaveChanges();
        }

        /// <summary>
        /// Updated entity in repositoy
        /// </summary>
        /// <param name="entity">Entity</param>
        public virtual void Update(TEntity entity)
        {
            AttachIsNotAttached(entity);
            Context.Entry(entity).State = EntityState.Modified;            
            Context.SaveChanges();
        }

        /// <summary>
        /// Remove entity from rpository
        /// </summary>
        /// <param name="entity">Entity</param>
        public virtual void Delete(TEntity entity)
        {
            AttachIsNotAttached(entity);
            Context.Set<TEntity>().Remove(entity);
            Context.Entry(entity).State = EntityState.Deleted;
            Context.SaveChanges();
        }

        /// <summary>
        /// Return models error
        /// </summary>
        /// <returns></returns>
        public IEnumerable<object> GetValidationModelErrors()
        {
            return Context.GetValidationErrors();
        }

        /// <summary>
        /// Attach entity 
        /// </summary>
        /// <param name="entity"></param>
        protected void AttachIsNotAttached(TEntity entity)
        {
            if (Context.Entry(entity).State == EntityState.Detached)
            {
                var attached = Context.Set<TEntity>().Local.Where(x => x.Id == entity.Id).FirstOrDefault();

                attached.Do((s) => {
                    Context.Entry(s).State = EntityState.Detached;
                });

                Context.Set<TEntity>().Attach(entity);
            }
        }
}

1 个答案:

答案 0 :(得分:0)

默认情况下,wcf配置有很多限制,例如数组的最大大小,并且异常并不完全发生了什么,也许在你的情况下它是由于默认的wcf配置,我建议你使用svcTraceViewer,它将帮助您了解明确的消息所发生的事情。