ipjohnson / Grace

Grace is a feature rich dependency injection container library
MIT License
336 stars 33 forks source link

Problem in registering IHubContext<,> in Grace IOC #229

Closed shahabganji closed 4 years ago

shahabganji commented 4 years ago

Question on stackoverflow

I have a 2.2 .net core application, using SignalR core, in one of my Controllers I want to inject IHubContext<ChatHub, IChatClient>. Works pretty well with built-in IoC, but when I add Grace IoC I get the following error message:

Unable to resolve service for type 'Microsoft.AspNetCore.SignalR.IHubContext`2[Test.Hubs.ChatHub,Test.Hubs.IChatClient]' while attempting to activate TestController

I tried to add the following line in the ConfigureContainer method:

scope.Configure(c=> c.Export<IHubContext<ChatHub, IChatClient>>());

Also tried:

scope.Configure(c=> { c.Export(typeof(IHubContext<,>)); });

Then the error changed to the following which sounds reasonable since I haven't registered a concrete class for the interface.

Could not find public constructor on type Microsoft.AspNetCore.SignalR.IHubContext`2[[Test.Hubs.ChatHub, Test, Version=0.0.1.0, Culture=neutral, PublicKeyToken=null],[Test.Hubs.IChatClient, Test, Version=0.0.1.0, Culture=neutral, PublicKeyToken=null]]

And BTW, I've tested aforementioned approaches with both .UseGrace((new InjectionScopeConfiguration { AutoRegisterUnknown = true })) and .UseGrace((new InjectionScopeConfiguration { AutoRegisterUnknown = false })).

Grace.AspNetCore.MVC and Grace.AspNetCore.Hosting packages are installed.

Anything that I missed?

silkfire commented 4 years ago

I think you need to add scope.SetupMvc() in order for Grace to correctly perform injection on your controllers.

shahabganji commented 4 years ago

I testes that as well but to no avail.

Program.cs

// omitted code
WebHost.CreateDefaultBuilder(args)
.UseGrace((new InjectionScopeConfiguration { AutoRegisterUnknown = false }));

Startup.cs

 public void ConfigureContainer(IInjectionScope scope)
        {
            scope.SetupMvc();
        }

Got the following error

An unhandled exception has occurred while executing the request.
System.ArgumentNullException: Value cannot be null.
Parameter name: instance
   at Microsoft.Extensions.Internal.PropertyActivator`1.Activate(Object instance, TContext context)
   at Microsoft.AspNetCore.Mvc.Internal.DefaultControllerPropertyActivator.<>c__DisplayClass5_0.<GetActivatorDelegate>g__Activate|0(ControllerContext controllerContext, Object controller)
   at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass5_0.<CreateControllerFactory>g__CreateController|0(ControllerContext controllerContext)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
   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()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

EDIT

And with

.UseGrace((new InjectionScopeConfiguration { AutoRegisterUnknown = true }));

Got this one:

An unhandled exception has occurred while executing the request.
Grace.DependencyInjection.Exceptions.LocateException: Could not locate Type Microsoft.AspNetCore.SignalR.IHubContext`2[Test.Hubs.ChatHub,Test.Hubs.IChatClient]
1 Importing Test.Controllers.GroupsController
2 Importing Microsoft.AspNetCore.SignalR.IHubContext`2[Test.Hubs.ChatHub,Test.Hubs.IChatClient]  for constructor parameter chatHub

   at Grace.DependencyInjection.Impl.InjectionContextValueProvider.GetValueFromInjectionContext[T](IExportLocatorScope locator, StaticInjectionContext staticContext, Object key, IInjectionContext dataProvider, Object defaultValue, Boolean useDefault, Boolean isRequired) in C:\projects\grace\src\Grace\DependencyInjection\Impl\InjectionContextValueProvider.cs:line 236
   at lambda_method(Closure , IExportLocatorScope , IDisposalScope , IInjectionContext )
   at Grace.DependencyInjection.Impl.ActivationStrategyDelegateCache.FallbackExecution(ImmutableHashTree`2 currentNode, Type type, IExportLocatorScope scope, Boolean allowNull, IInjectionContext context) in C:\projects\grace\src\Grace\DependencyInjection\Impl\ActivationStrategyDelegateCache.cs:line 128
   at Grace.DependencyInjection.Impl.ActivationStrategyDelegateCache.ExecuteActivationStrategyDelegateAllowNull(Type type, IExportLocatorScope scope) in C:\projects\grace\src\Grace\DependencyInjection\Impl\ActivationStrategyDelegateCache.cs:line 78
   at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass5_0.<CreateControllerFactory>g__CreateController|0(ControllerContext controllerContext)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
   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()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
ipjohnson commented 4 years ago

Hi @shahabganji

Could you put together a small sample of it not working and I can take a look. What it sounds like is there is a registration that's not being added to Grace but is to the default container (not sure how that happens).

I've not worked with SignalR before so I'm not 100% sure what's wrong.

silkfire commented 4 years ago

I assume you've added this as well to your code to activate SignalR?

app.UseSignalR(routes => routes.MapHub<MyHubType>("/hub-endpoint"));

shahabganji commented 4 years ago

Hi @ipjohnson, yes sure I'll try to reproduce the problem in a repository and I will share the link here.

@silkfire your assumption is correct.

shahabganji commented 4 years ago

@ipjohnson here you could find a sample repo.

Just one thing to note, due to this I thought that version 7.1 might be only for .net core 3.0, however, I tested the above-mentioned repo with both versions 6.4 and 7.1.

ipjohnson commented 4 years ago

@shahabganji sorry it's taken me so long to get to this I've been swamped with life stuff.

When I open this project it fails to load with this exception

'C:\Users\ijohnson\Documents\Visual Studio 2019\Projects\GraceSignalRSample-master\GraceSignalRSample\GraceSignalRSample.csproj : error : One or more errors occurred.'

shahabganji commented 4 years ago

@ipjohnson

Don't be, take your time.

I created the project on my Mac, but I get the same error on windows too; 😩 let me check it first.

shahabganji commented 4 years ago

@ipjohnson

It should be OK now, just needed to remove global.json file 🙈

ipjohnson commented 4 years ago

So I took a look and there is a bug in the way the generic type is being constructed (or not constructed as the case is). I'm going to try and address this issue and a couple others this weekend.

ipjohnson commented 4 years ago

I have one more issue I need to address before I can do a beta release but you can test the fix in the nightly nuget feed

https://ci.appveyor.com/nuget/grace-master

shahabganji commented 4 years ago

@ipjohnson

Thanks for your great support. Unfortunately, my laptop is broken and I cannot test it until I get it repaired. however, as soon as I have my machine I will test and report the results. 🙏

shahabganji commented 4 years ago

I got a chance to test it on another machine, Windows, and everything looks to work just fine.

ipjohnson commented 4 years ago

I've released a new version 7.1.0-beta

shahabganji commented 4 years ago

@ipjohnson

I just figured out one thing which I am not sure how it is affecting the behavior of the Grace IoC, I have added the Grace version 7.1.0-Beta768 and find out that the ConfigureContainer method was commented in my tests, but UseGrace was not, which is weird to me that no exceptions are throwing, since I remember that older versions were throwing exception if that method does not exists. Thus, I am not sure whether Grace is the active dependency resolver in my project; and It fails to resolve the dependencies when I add ConfigureContainer method!!!

Program.cs

public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseGrace(new InjectionScopeConfiguration
                {
                    AutoRegisterUnknown = false
                })
        ;

Startup.cs

public void ConfigureServices(IServiceCollection services)
        {
            services.AddSignalR();

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }

//        public void ConfigureContainer(IInjectionScope scope)
//        {
//            scope.SetupMvc();
//        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseSignalR(routes => { routes.MapHub<ChatHub>("/hubs/chat"); });

            app.UseHttpsRedirection();
            app.UseMvc();
        }
    }

The above-mentioned setting works, but I am not sure whether it is the Grace IoC which is active or the built-in one!?

If you comment out the ConfigureContainer method, the resolution does not work properly!

ipjohnson commented 4 years ago

Hi @shahabganji

This is really a function of asp.net core. 3rd party containers aren't used unless there is a ConfigureContainer method.

shahabganji commented 4 years ago

Hi @ipjohnson

So when I comment that method, I am using the built-in container and not Grace, right? If so, then this issue has not been solved I guess, am I right or I missed something?

ipjohnson commented 4 years ago

If the method is commented out then you are using the built-in container. So it sounds like this is not fixed for you. Is it that same error? Does the example app you put together work with the new version is does it give you problem as well?

shahabganji commented 4 years ago

Is it that same error?

Yes, that is the same error.

Does the example app you put together work with the new version is does it give you problem as well?

The example app uses Grace@7.1.0-Beta768 and has the above-mentioned problem. I may need to push changes to the repository to get the NuGet dependencies updated there.

ipjohnson commented 4 years ago

Ok I can reproduce it. My unit test was slightly different than the use case. It's an easy fix but I won't be able to do it till tomorrow or Wednesday

ipjohnson commented 4 years ago

I just pushed a new beta can you try it

shahabganji commented 4 years ago

Sure, I’ll check it ASAP and let you know if the results.

Am 28.08.2019 um 16:43 schrieb Ian Johnson notifications@github.com:

I just pushed a new beta can you try it

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

ipjohnson commented 4 years ago

I ran it in the example you provided but better to have you test it as well

shahabganji commented 4 years ago

Hi @ipjohnson

I got a chance to test the project and here is the result:

public void ConfigureContainer( IInjectionScope scope ) {
            scope.SetupMvc((config) =>
            {
                config.UseControllerActivator = false; // Works fine
//               config.UseControllerActivator = true; // Will fail if commented out
            } );
        }
<PackageReference Include="Grace" Version="7.1.0-Beta772" />
cuteant commented 4 years ago

Thanks!!! After this bug is fixed, Grace can run perfectly in OrchardCore

ipjohnson commented 4 years ago

@shahabganji when I run it in the example app it's working so I'm a little unsure why it's not working. Can screen shot the error again and send me the program.cs and startup.cs?

@cuteant you're running into this same issue in OrchardCore?

cuteant commented 4 years ago

Yes, but the problem I encountered was that when the GraphQL Type field was registered, I found that IOptions<> could not be resolved correctly.

ipjohnson commented 4 years ago

@cuteant do you have a simple example of this or something you could share that I can run?

cuteant commented 4 years ago

OrchardCore.ShellContainerFactory

Replace with this: `
public static IServiceProvider BuildGraceServiceProvider(this IServiceCollection services) { var configuration = new InjectionScopeConfiguration(); configuration.AutoRegisterUnknown = false; configuration.Behaviors.AllowInstanceAndFactoryToReturnNull = true; return BuildGraceServiceProvider(services, configuration); }

    public static IServiceProvider BuildGraceServiceProvider(this IServiceCollection services, IInjectionScopeConfiguration configuration)
    {
        if (configuration is null) { Eme.EmeThrowHelper.ThrowArgumentNullException(Eme.ExceptionArgument.configuration); }

        var container = new DependencyInjectionContainer(configuration);
        return container.Populate(services);
    }

` The test case in OrchardCore.Test cannot pass

My english is not so good, I hope you can understand

shahabganji commented 4 years ago

@shahabganji when I run it in the example app it's working so I'm a little unsure why it's not working. Can screen shot the error again and send me the program.cs and startup.cs?

@cuteant you're running into this same issue in OrchardCore?

Worth to mention that I am running the app on a Mac.

Program.cs

public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseGrace(new InjectionScopeConfiguration
                {
                    AutoRegisterUnknown = false
                })
        ;
    }

Startup.cs

public class Startup {
        public Startup( IConfiguration configuration ) {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices( IServiceCollection services ) {
            services.AddSignalR( );

            services.AddMvc( ).SetCompatibilityVersion( CompatibilityVersion.Version_2_2 );
        }

        public void ConfigureContainer( IInjectionScope scope ) {
            scope.SetupMvc((config) =>
            {
                config.UseControllerActivator = true; // set this to false, or totally forget scope.SetupMvc
            } );
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure( IApplicationBuilder app, IHostingEnvironment env ) {
            if ( env.IsDevelopment( ) ) {
                app.UseDeveloperExceptionPage( );
            } else {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts( );
            }

            app.UseSignalR( routes => { routes.MapHub<ChatHub>( "/hubs/chat" ); } );

            app.UseHttpsRedirection( );
            app.UseMvc( );
        }
    }

Screenshot of the Browser

Screen Shot 2019-08-28 at 23 42 59

Raw Exception Details

System.ArgumentNullException: Value cannot be null.
Parameter name: instance
   at Microsoft.Extensions.Internal.PropertyActivator`1.Activate(Object instance, TContext context)
   at Microsoft.AspNetCore.Mvc.Internal.DefaultControllerPropertyActivator.<>c__DisplayClass5_0.<GetActivatorDelegate>g__Activate|0(ControllerContext controllerContext, Object controller)
   at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass5_0.<CreateControllerFactory>g__CreateController|0(ControllerContext controllerContext)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
   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()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
ipjohnson commented 4 years ago

@shahabganji this is caused by a combination of AutoRegisterUnknown = false and config.UseControllerActivator = true;

Essentially you're telling the container not to auto register unknown types (controller specifically) and you're enabling resolving controllers from the container. If you register all your controllers and other concrete types then you can use that combination of flags. Otherwise you'll need to change something.

@cuteant your issue looks to be different and I'm planning on addressing it this weekend as I have a couple days off.

ipjohnson commented 4 years ago

I think this can be closed out and I'll push an RC version here this week