【发布时间】:2021-07-29 14:02:10
【问题描述】:
我在 .Net 5 项目中使用 HotChocolate GraphQL 库实现了一个非常简单的查询。我一直在关注 HotChocolate GitHub 存储库中的 tutorial series,但在我的项目中,我不希望 GraphQL 直接访问上下文,而是希望它访问通过上下文管理数据库访问的存储库。
我在这个项目中与 GraphQL 一起构建了一些 REST 端点,并且这个存储库模式在那里可以正常工作。但是,当我从 GraphQL 调用这些存储库方法时,上下文会在存储库方法使用它之前被释放。
我猜测 HotChocolate 使用上下文的方式导致它比我预期的更早被处理,但我无法弄清楚它何时/何处被处理以及如何防止它被处理,因此我的存储库方法会工作的。
ContentRepository.cs
namespace BLL.Repository
{
public class ContentRepository : IRepository<Content>
{
private readonly CmsContext _dbContext;
public ContentRepository(CmsContext dbContext)
{
_dbContext = dbContext;
}
public virtual List<Content> GetContent()
{
return _dbContext.Content.ToList();
}
}
}
Query.cs
namespace Web.GraphQL
{
public class Query
{
private readonly IContentRepository _contentRepository;
public Query(IContentRepository contentRepository)
{
_contentRepository = contentRepository;
}
public List<Content> GetContent()
{
return _contentRepository.GetContent() as List<Content>;
}
}
}
Startup.cs
namespace Web
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddControllers().AddNewtonsoftJson();
services.AddDbContext<CmsContext>(options =>
options.UseMySql(Configuration.GetConnectionString("DefaultConnection"), ServerVersion.AutoDetect(Configuration.GetConnectionString("DefaultConnection"))));
services
.AddGraphQLServer()
.ModifyRequestOptions(options => options.IncludeExceptionDetails = true)
.AddQueryType<Query>();
services.AddScoped<IRepository<Content>, ContentRepository>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
app.UseHttpsRedirection();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute("default", "/{controller}/{action}",
new {controller = "Default", action = "Index"});
});
app.UseEndpoints(endpoints => endpoints.MapGraphQL());
app.UseGraphQLVoyager(new VoyagerOptions
{
GraphQLEndPoint = "/graphql"
}, "/graphql-voyager");
}
}
}
在调试和单步执行代码时,一旦到达ContentRepository.cs 中的GetContent 方法,它就会抛出ObjectDisposedException。
我需要做些什么来确保CmsContext 在从 GraphQL 查询中调用时仍然可供 ContentRepository 使用?
【问题讨论】:
-
不要使用构造函数注入,使用带有
[Service]属性的参数注入。 github.com/ChilliCream/hotchocolate/blob/develop/templates/… -
你是如何进入 ContentRepository 的呢?此外,这些存储库/查询类过于复杂,没有任何好处。除此之外,Query 还知道它不应该知道的存储库中的实现细节(转换为
List<T>) -
@CamiloTerevinto 我同意 DbContext 的不必要抽象,但
IList/ICollection/IReadonlyList与List并不算太糟糕。 -
@abdusco 哎呀,忽略最后一点,我看到演员表并立即想到
IEnumerable<T>到List<T>(我经常看到) -
@abdusco 解决了我的问题。如果你把它作为一个答案,我会接受它。感谢您的帮助
标签: c# asp.net-core hotchocolate