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

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

Closed
shahabganji opened this issue Jul 24, 2019 · 32 comments
Closed

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

shahabganji opened this issue Jul 24, 2019 · 32 comments
Labels

Comments

@shahabganji
Copy link

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
Copy link
Contributor

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

@shahabganji
Copy link
Author

shahabganji commented Jul 24, 2019

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
Copy link
Owner

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
Copy link
Contributor

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

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

@shahabganji
Copy link
Author

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
Copy link
Author

@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
Copy link
Owner

@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
Copy link
Author

shahabganji commented Jul 31, 2019

@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
Copy link
Author

@ipjohnson

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

@ipjohnson ipjohnson added the bug label Aug 1, 2019
@ipjohnson
Copy link
Owner

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.

@shahabganji shahabganji changed the title [QUESTION]: How to register IHubContext<,> in Grace IOC Problem in registering IHubContext<,> in Grace IOC Aug 1, 2019
@ipjohnson
Copy link
Owner

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
Copy link
Author

@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
Copy link
Author

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

@ipjohnson
Copy link
Owner

I've released a new version 7.1.0-beta

@shahabganji
Copy link
Author

@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
Copy link
Owner

Hi @shahabganji

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

@shahabganji
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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 [email protected] and has the above-mentioned problem. I may need to push changes to the repository to get the NuGet dependencies updated there.

@ipjohnson ipjohnson reopened this Aug 26, 2019
@ipjohnson
Copy link
Owner

ipjohnson commented Aug 26, 2019

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
Copy link
Owner

I just pushed a new beta can you try it

@shahabganji
Copy link
Author

shahabganji commented Aug 28, 2019 via email

@ipjohnson
Copy link
Owner

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

@shahabganji
Copy link
Author

shahabganji commented Aug 28, 2019

Hi @ipjohnson

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

  • When scope.SetupMvc() is not called inside ConfigureContainer method, it works fine.
  • When it is called without Action<GraceMVCConfiguration>, it fails with the same error.
  • When it is called with Action<GraceMVCConfiguration> and the UseControllerActivator prop is set to false again works fine, but when set to true it fails again.
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
Copy link

cuteant commented Aug 28, 2019

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

@ipjohnson
Copy link
Owner

@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
Copy link

cuteant commented Aug 28, 2019

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
Copy link
Owner

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

@cuteant
Copy link

cuteant commented Aug 28, 2019

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
Copy link
Author

@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
Copy link
Owner

@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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants