4

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

异常代码及说明:

实体类型控制器:

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);
}

实体类型服务:

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);
            }
        }
}
4

1 回答 1

0

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

于 2012-10-07T11:08:59.330 回答