【发布时间】:2019-11-20 01:22:08
【问题描述】:
我在尝试将 DbContext 注入 Azure 函数时收到以下错误:
Microsoft.EntityFrameworkCore: Cannot access a disposed object.
这是当前函数
private readonly FundCentreContext _fundCentreContext;
public GetDailyPrices(FundCentreContext fundCentreContext)
{
_fundCentreContext = fundCentreContext;
}
[Produces("application/json")]
[FunctionName(nameof(GetDailyPrices))]
public IActionResult Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "api/dailyprices")] HttpRequest req,
ILogger log)
{
//Parameters
var parameters = req.GetQueryParameterDictionary();
var page = int.Parse(parameters.GetValueOrDefault("page", "0"));
var limit = int.Parse(parameters.GetValueOrDefault("limit", "10"));
var offset = int.Parse(parameters.GetValueOrDefault("offset", "0"));
var sort = parameters.GetValueOrDefault("sort", "asc");
var fundService = new FundService(_fundCentreContext);
var fundDailyPrices = fundService.GetAllDailyPricesByPage(page, limit, offset);
return fundDailyPrices != null
? (ActionResult)new OkObjectResult(fundDailyPrices)
: new BadRequestObjectResult("There was an error with your request");
}
并且使用该服务的启动有以下代码:
services
.AddDbContext<FundCentreContext>(options =>
options.UseSqlServer("*ommited*"));
我不知道为什么会发生这个错误 - 调试过去的 return 语句有效,并且可以在 fundDailyPrices 对象中看到数据,但是 return 语句之后的某些东西意外地结束了整个函数。
【问题讨论】:
-
你的
GetAllDailyPricesByPage函数是async吗? -
如果没有任何等待,为什么要使用
async Task? -
我打算将 await 添加到 GetAllDailyPricesByPage() 函数,但目前它无论如何都不是异步的。我编辑了问题并删除了异步以消除混乱。使用同步函数仍然会发生错误。
-
这可能会有所帮助 - stackoverflow.com/questions/65242457/…
标签: asp.net-core entity-framework-core azure-functions