ipjohnson / Grace.DependencyInjection.Extensions

Grace Extensions for ASP.Net Core
19 stars 7 forks source link

.Net Core - Grace container.resolve finds implementation, can't find it to inject in Controller #6

Closed scovel closed 6 years ago

scovel commented 6 years ago

Simple test project. I'm sure I'm missing some configuration somewhere...

program.cs
        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseGrace()  // add grace

                .UseStartup<Startup>()

                .Build();
startup.cs
        public void ConfigureContainer(IInjectionScope scope)
        {
            scope.SetupMvc();
        }
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<KestrelServerOptions>(options => {
                options.Listen(new System.Net.IPAddress(new byte[] { 127, 0, 0, 1 }), 8088);
            });

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
            });

            services.AddMvc();

            var container = new DependencyInjectionContainer();

            container.Configure(c => {
                c.Export<BusinessServiceA>().As<IBusinessServiceA>();
                c.Export<BusinessServiceB>().As<IBusinessServiceB>();
                c.Export<Dal>().As<IDal>();

            });

            var basicService = container.Locate<IBusinessServiceA>();

            // RegisterStuff(container);
        }
container.Locate<IBusinessServiceA>(); works fine.

When running the service, i get the following exception:

      Executed action TestUnity.Controllers.ValuesControllerA.Stuff (TestGrace) in 11.7578ms
fail: Microsoft.AspNetCore.Server.Kestrel[13]
      Connection id "0HLDND95UEMNM", Request id "0HLDND95UEMNM:00000001": An unhandled exception was thrown by the application.
Grace.DependencyInjection.Exceptions.LocateException: Could not locate Type TestUnity.Implementations.IBusinessServiceA
1 Importing TestUnity.Controllers.ValuesControllerA
2 Importing TestUnity.Implementations.IBusinessServiceA  for constructor parameter businessService

   at Grace.DependencyInjection.Impl.InjectionContextValueProvider.GetValueFromInjectionContext[T](IExportLocatorScope locator, StaticInject
ionContext staticContext, Object key, IInjectionContext dataProvider, Object defaultValue, Boolean useDefault, Boolean isRequired)
   at lambda_method(Closure , IExportLocatorScope , IDisposalScope , IInjectionContext )
   at Grace.DependencyInjection.Impl.LifetimeScope.LocateFromParent(Type type, Object extraData, ActivationStrategyFilter consider, Object k
ey, Boolean allowNull, Boolean isDynamic)
   at Grace.DependencyInjection.Impl.LifetimeScope.LocateOrDefault(Type type, Object defaultValue)
   at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass5_0.<CreateControllerFactory>g__CreateController|0(Con
trollerContext controllerContext)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeInnerFilterAsync>d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeNextResourceFilter>d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeFilterPipelineAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeAsync>d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIIndexMiddleware.<Invoke>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.<Invoke>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.<Invoke>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame`1.<ProcessRequestsAsync>d__2.MoveNext()
dbug: Microsoft.AspNetCore.Server.Kestrel[9]
      Connection id "0HLDND95UEMNM" completed keep alive response.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 321.8685ms 500
ipjohnson commented 6 years ago

Hi @scovel

When you call .UseGrace() it's actually creating the container behind the scenes for you and it's passed back to you in the ConfigureContainer(IInjectionScope scope) method.

If you change your code to call Configure on the scope parameter I think it will work.

scovel commented 6 years ago

First, WOW that was a fast response!!!

I knew it had to be something simple. You are correct sir, that fixed the problem.

Thanks!

Sean