Copied to clipboard

Flag this post as spam?

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


  • ianhoughton 240 posts 488 karma points c-trib
    17 days ago
    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 322 posts 533 karma points
    17 days ago
    Brendan Rice
    0

    How about registering the composition like this?

    composition.Register<QDHDataContext>(factory => new QDHDataContext(), Lifetime.Request);
    
  • ianhoughton 240 posts 488 karma points c-trib
    17 days ago
    ianhoughton
    0

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

  • Gareth Wright 30 posts 98 karma points c-trib
    16 days ago
    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 240 posts 488 karma points c-trib
    14 days ago
    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