【问题标题】:WCF services, loading data from databaseWCF 服务,从数据库加载数据
【发布时间】:2012-10-07 10:58:04
【问题描述】:

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

【问题讨论】:

  • 调试服务或开启diagnostics获取具体错误详情。 500 内部服务器错误无用。

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多