Cannot access a disposed object when injecting DbContext through Azure Function

asp.net-core azure-functions entity-framework-core

Question

I am receiving the following error when trying to inject the DbContext into an Azure Function:

Microsoft.EntityFrameworkCore: Cannot access a disposed object.

Here is the current function

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

And also the StartUp to use the service has the following code:

services
    .AddDbContext<FundCentreContext>(options =>
        options.UseSqlServer("*ommited*"));

I can't work out why this error is occuring - debugging past the return statement works, and data can be seen in the fundDailyPrices object, however something after the return statement ends the whole function unexpectedly.

1
1
7/10/2019 12:31:06 PM

Accepted Answer

It's hard to tell with the code you've provided, but the most likely culprit is that you aren't materializing the result set until the context has gone out of scope. Again, we can't see all the code, but this happens when you do things like return an IQueryable directly. Any type of service call should return a materialized list (i.e. call ToList() or ToListAsync() on the result set before returning). It might also be caused by lazy-loading, if you've enabled that feature. If so, yo should make sure that all necessary relationships are eagerly loaded.

It's also weird that you're newing up your service with an injected context. You should simply inject your service, and since it has a constructor dependency on your context, that will be automatically injected into it. That ensures that both objects are operating in the same, or at least compatible, lifetime.

Also, don't use IDisposable with injected dependencies. We can't tell anything about your service class, but if it does implement IDisposable, remove that.

3
7/10/2019 1:14:05 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow