Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EF doesn't work with NInject #1241

Closed
victorhurdugaci opened this issue Dec 6, 2014 · 4 comments
Closed

EF doesn't work with NInject #1241

victorhurdugaci opened this issue Dec 6, 2014 · 4 comments

Comments

@victorhurdugaci
Copy link

Repro:

  1. Change MusicStore to use NInject instead of the default DI container.
  2. Run

Result:

System.AggregateException was unhandled by user code
  HResult=-2146233088
  Message=One or more errors occurred.
  Source=mscorlib
  StackTrace:
       at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
       at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
       at System.Threading.Tasks.Task.Wait()
       at MusicStore.Startup.Configure(IApplicationBuilder app) in D:\aspnet\musicstore\src\MusicStore\Startup.cs:line 187
       at MusicStore.Startup.ConfigureDevelopment(IApplicationBuilder app) in D:\aspnet\musicstore\src\MusicStore\Startup.cs:line 115
  InnerException: System.NullReferenceException
       HResult=-2147467261
       Message=Object reference not set to an instance of an object.
       Source=EntityFramework.Core
       StackTrace:
            at Microsoft.Data.Entity.Infrastructure.DbContextServices.get_DataStoreServices()
            at Microsoft.Data.Entity.Storage.DataStoreServices.GetStoreServices(IServiceProvider serviceProvider)
            at Microsoft.Data.Entity.Storage.DataStoreServices.<>c__DisplayClass9.<get_DatabaseFactory>b__11()
            at Microsoft.Data.Entity.Utilities.LazyRef`1.get_Value()
            at Microsoft.Data.Entity.Infrastructure.DbContextService`1.get_Service()
            at Microsoft.Data.Entity.DbContext.get_Database()
            at MusicStore.Models.SampleData.<InitializeMusicStoreDatabaseAsync>d__1.MoveNext() in D:\aspnet\musicstore\src\MusicStore\Models\SampleData.cs:line 24
       InnerException: 
@ajcvickers
Copy link
Contributor

@victorhurdugaci Can you provide a link with instructions on using MusicStore with NInject?

@victorhurdugaci
Copy link
Author

Instructions for NInject:

  1. Use the MusicStore from the dev branch.
  2. Add this dependency to project.json (it works only for desktop CLR):
"Microsoft.Framework.DependencyInjection.Ninject": "1.0.0-*",
  1. Modifiy the ConfigureServices method as described below
// Change method return type from void to IServiceProvider
public IServiceProvider ConfigureServices(IServiceCollection services)
{
    ...

    // Add this just before method end
    var kernel = new StandardKernel();
    kernel.Populate(services);
    return kernel.Get<IServiceProvider>();
}

@ajcvickers
Copy link
Contributor

This appears to be showing up in EF because the first thing to try to resolve dependencies is EF in the following code in Startup:

SampleData.InitializeMusicStoreDatabaseAsync(app.ApplicationServices).Wait();

This is only needed when the database doesn't exist and so can be removed if the database does exist. The result is then:

An unhandled exception occurred while processing the request.

ActivationException: Error activating INestedProviderManager{FilterProviderContext} using binding from INestedProviderManager{T} to NestedProviderManager{T}
A cyclical dependency was detected between the constructors of two services.

Activation path:
4) Injection of dependency INestedProviderManager{FilterProviderContext} into parameter filterProvider of constructor of type ControllerActionInvokerProvider
3) Injection of dependency INestedProvider{ActionInvokerProviderContext} into parameter providers of constructor of type NestedProviderManager{ActionInvokerProviderContext}
2) Injection of dependency INestedProviderManager{ActionInvokerProviderContext} into parameter actionInvokerProvider of constructor of type ActionInvokerFactory
1) Request for IActionInvokerFactory

Suggestions:
1) Ensure that you have not declared a dependency for INestedProviderManager{FilterProviderContext} on any implementations of the service.
2) Consider combining the services into a single one to remove the cycle.
3) Use property injection instead of constructor injection, and implement IInitializable
if you need initialization logic to be run after property values have been injected.
Ninject.Activation.Context.Resolve() in Context.cs, line 148

Stack Query Cookies Headers Environment
ActivationException: Error activating INestedProviderManager{FilterProviderContext} using binding from INestedProviderManager{T} to NestedProviderManager{T} A cyclical dependency was detected between the constructors of two services. Activation path: 4) Injection of dependency INestedProviderManager{FilterProviderContext} into parameter filterProvider of constructor of type ControllerActionInvokerProvider 3) Injection of dependency INestedProvider{ActionInvokerProviderContext} into parameter providers of constructor of type NestedProviderManager{ActionInvokerProviderContext} 2) Injection of dependency INestedProviderManager{ActionInvokerProviderContext} into parameter actionInvokerProvider of constructor of type ActionInvokerFactory 1) Request for IActionInvokerFactory Suggestions: 1) Ensure that you have not declared a dependency for INestedProviderManager{FilterProviderContext} on any implementations of the service. 2) Consider combining the services into a single one to remove the cycle. 3) Use property injection instead of constructor injection, and implement IInitializable if you need initialization logic to be run after property values have been injected.
Ninject.Activation.Context.Resolve() in Context.cs
Ninject.KernelBase.<>c__DisplayClass10.<Resolve>b__c(IBinding binding) in KernelBase.cs
System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent) in Target.cs
Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent) in Target.cs
Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target) in StandardProvider.cs
Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target) in StandardProvider.cs
System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
System.Linq.Buffer`1..ctor(IEnumerable`1 source)
System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
Ninject.Activation.Providers.StandardProvider.Create(IContext context) in StandardProvider.cs
Ninject.Activation.Context.Resolve() in Context.cs
Ninject.KernelBase.<>c__DisplayClass10.<Resolve>b__c(IBinding binding) in KernelBase.cs
System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
System.Linq.Enumerable.<CastIterator>d__b1`1.MoveNext()
System.Linq.Buffer`1..ctor(IEnumerable`1 source)
System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext()
System.Linq.Buffer`1..ctor(IEnumerable`1 source)
System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
Microsoft.Framework.DependencyInjection.NestedProviders.NestedProviderManager`1..ctor(IEnumerable`1 providers)
DynamicInjector2649713f7ca34ec59941a8ccba28b9b4(Object[] )
Ninject.Activation.Providers.StandardProvider.Create(IContext context) in StandardProvider.cs
Ninject.Activation.Context.Resolve() in Context.cs
Ninject.KernelBase.<>c__DisplayClass10.<Resolve>b__c(IBinding binding) in KernelBase.cs
System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent) in Target.cs
Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent) in Target.cs
Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target) in StandardProvider.cs
Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target) in StandardProvider.cs
System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
System.Linq.Buffer`1..ctor(IEnumerable`1 source)
System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
Ninject.Activation.Providers.StandardProvider.Create(IContext context) in StandardProvider.cs

INestedProviderManager is a type from the DI repro so opening a bug on DI to investigate this since it doesn't, at least from what I can see, relate to EF.

@ajcvickers
Copy link
Contributor

Opened aspnet/DependencyInjection#153. Closing this issue for now; it can be re-opened if further investigation reveals that it is an EF issue.

@ajcvickers ajcvickers removed this from the 7.0.0 milestone Dec 11, 2014
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants