Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • ianhoughton 281 posts 605 karma points c-trib
    Jan 08, 2021 @ 15:32
    ianhoughton
    0

    Injecting dbcontext with LightInject

    I'm trying to inject an EF dbcontext into a controller and have tried the following ways:

    public class ServiceComposer : IUserComposer
        {
            public void Compose(Composition composition)
            {
                composition.Register<QDHDataContext>(factory => new QDHDataContext(), Lifetime.Scope);
    
                //var container = composition.Concrete as ServiceContainer;
                //container.EnablePerWebRequestScope();
                //container.Register<QDHDataContext>(new PerScopeLifetime());
            }
        }
    

    This works the first time the controller is hit, but on subsequent attempts it throws this error: The operation cannot be completed because the DbContext has been disposed

    So i tried this way:

    var container = composition.Concrete as ServiceContainer;
    container.EnablePerWebRequestScope();
    container.Register<QDHDataContext>(new PerScopeLifetime());
    

    but this just throws this error on startup: Container.ScopeManagerProvider is not MixedLightInjectScopeManagerProvider

  • Brendan Rice 538 posts 1102 karma points
    Jan 08, 2021 @ 17:19
    Brendan Rice
    0

    How about registering the composition like this?

    composition.Register<QDHDataContext>(factory => new QDHDataContext(), Lifetime.Request);
    
  • ianhoughton 281 posts 605 karma points c-trib
    Jan 08, 2021 @ 17:25
    ianhoughton
    0

    No, I get the same error: The operation cannot be completed because the DbContext has been disposed

  • Gareth Wright 32 posts 101 karma points c-trib
    Jan 09, 2021 @ 18:29
    Gareth Wright
    0

    Does this work?

    composition.Register<QDHDataContext, DbContext>(Lifetime.Request);
    

    if you need to inject parameters you can do:

    composition.Register<QDHDataContext>(x=> new DbContext(para1, para2)), Lifetime.Request);
    

    Similarly using npoco:

       composition.Register<IDatabase>(x =>
                {
                    return new NPoco.Database(
                        ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString,
                        DatabaseType.SqlServer2012,
                        SqlClientFactory.Instance);
                });
    
  • ianhoughton 281 posts 605 karma points c-trib
    Jan 11, 2021 @ 10:20
    ianhoughton
    0

    Thanks Gareth, none of those work either :(

    I'm thinking it must be the way the dbcontext is setup, or being injected into the service. (I've inherited that part of the code, and am trying to incorporate it without any changes)

    If I remove the need to inject into the service, and just new up a dbcontext when needed, then I don't have a problem i.e:

    public ServiceRecordService(QDHDataContext mainRepo)
    {
        _dbContext = mainRepo;
    }
    
    using (var db = _dbContext)
    {
        // this does NOT work
    }
    
    //public ServiceRecordService(QDHDataContext mainRepo)
    //{
        //_dbContext = mainRepo;
    //}
    
    using (var db = new QDHDataContext())
    {
        // this works ok
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft