Giter VIP home page Giter VIP logo

Comments (5)

vyliniem avatar vyliniem commented on August 17, 2024 2

Sorry to abandon my question like this, but I solved the original issue by manually requesting the db-context (via HttpContext):

public Task<IImageResolver> GetAsync(HttpContext context)
{
    ...

    using (var dbContext = context.RequestServices.GetService<ApplicationDbContext>())
    {
        ...
    }
}

from imagesharp.web.

JimBobSquarePants avatar JimBobSquarePants commented on August 17, 2024 1

Hi @vyliniem

Yeah, that's a bit of a pickle for you. The reason we do that is to ensure that we do not want to instantiate complex members on each requests. For example in this Azure Blob Provider we take advantage of the singleton scoping to reuse the CloudBlobClient. This dramatically reduces the overhead for us.

CloudBlobClient client = this.storageAccount.CreateCloudBlobClient();

One workaround you could do for now is to use the service locator (not great but will work) inside your custom provider to return the DbContext.

In the interim I'm going to have a look to see If I can refactor this to scope the providers (cache also) to the request scope to allow more flexibility of injection but keep the performance by injecting singleton configuration types.

@sebastienros Would changing the scope upset things in Orchard or would that be ok?

from imagesharp.web.

sebastienros avatar sebastienros commented on August 17, 2024 1

Going scoped should never be an issue. If you need to hold state across instances the user can always create a separate instance that is registered as Singleton and resolve it in the scoped service.

from imagesharp.web.

ThomasSkyldahl avatar ThomasSkyldahl commented on August 17, 2024

@vyliniem just for reference you could look at this section in the .net core docs: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.2#call-services-from-main

You could inject the IServiceScopeFactory into your IImageProvider and create a scope and resolve your ApplicationDbContext inside that scope as shown in the example in the link above.

from imagesharp.web.

JimBobSquarePants avatar JimBobSquarePants commented on August 17, 2024

@sebastienros Just to clarify, are you saying I should refactor the processors to allow efficient use in a scoped lifetime?

from imagesharp.web.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.