gpt4 book ai didi

asp.net-mvc - WCF 服务,从数据库加载数据

转载 作者:太空狗 更新时间:2023-10-30 01:58:23 25 4
gpt4 key购买 nike

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

异常代码及说明:

实体类型 Controller :

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

最佳答案

默认情况下,wcf 配置有很多限制,比如数组的最大大小,异常并没有完全发生,可能在你的情况下是由于默认的 wcf 配置,我建议你使用 svcTraceViewer ,这将有助于您了解显式消息究竟发生了什么。

关于asp.net-mvc - WCF 服务,从数据库加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12768197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com