You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Problem in registering IHubContext<,> in Grace IOC #229
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 fortype'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:
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.
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)
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.GroupsController2 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)
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.
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.
@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.'
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.
Reacted by Shahab Ganji and Hamed Fathi
Reacted by Uygar Yilmaz, Shahab Ganji and Hamed Fathi
changed the title [-][QUESTION]: How to register IHubContext<,> in Grace IOC[/-][+]Problem in registering IHubContext<,> in Grace IOC[/+]on Aug 1, 2019
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. 🙏
Am 28.08.2019 um 16:43 schrieb Ian Johnson ***@***.***>:
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.
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.
publicvoidConfigureContainer(IInjectionScopescope){scope.SetupMvc((config)=>{config.UseControllerActivator=false;// Works fine// config.UseControllerActivator = true; // Will fail if commented out});}
@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?
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);
}
@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.
publicclassStartup{publicStartup(IConfigurationconfiguration){Configuration=configuration;}publicIConfigurationConfiguration{get;}// This method gets called by the runtime. Use this method to add services to the container.publicvoidConfigureServices(IServiceCollectionservices){services.AddSignalR();services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);}publicvoidConfigureContainer(IInjectionScopescope){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.publicvoidConfigure(IApplicationBuilderapp,IHostingEnvironmentenv){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
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)
@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.
Activity
silkfire commentedon Jul 24, 2019
I think you need to add
scope.SetupMvc()in order for Grace to correctly perform injection on your controllers.shahabganji commentedon Jul 24, 2019
I testes that as well but to no avail.
Program.cs
Startup.cs
Got the following error
__ EDIT __
And with
Got this one:
ipjohnson commentedon Jul 24, 2019
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 commentedon Jul 24, 2019
I assume you've added this as well to your code to activate SignalR?
app.UseSignalR(routes => routes.MapHub<MyHubType>("/hub-endpoint"));shahabganji commentedon Jul 24, 2019
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 commentedon Jul 24, 2019
@ipjohnson here you could find a sample repo.
Just one thing to note, due to this I thought that version
7.1might be only for.net core 3.0, however, I tested the above-mentioned repo with both versions6.4and7.1.ipjohnson commentedon Jul 31, 2019
@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 commentedon 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 commentedon Jul 31, 2019
@ipjohnson
It should be OK now, just needed to remove
global.jsonfile 🙈ipjohnson commentedon Aug 1, 2019
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.
[-][QUESTION]: How to register IHubContext<,> in Grace IOC[/-][+]Problem in registering IHubContext<,> in Grace IOC[/+]addressing a generic constraint for issue #229
ipjohnson commentedon Aug 5, 2019
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 commentedon Aug 5, 2019
@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. 🙏
9 remaining items
ipjohnson commentedon 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
more constraint changes for issue #229
ipjohnson commentedon Aug 28, 2019
I just pushed a new beta can you try it
shahabganji commentedon Aug 28, 2019
ipjohnson commentedon Aug 28, 2019
I ran it in the example you provided but better to have you test it as well
shahabganji commentedon Aug 28, 2019
Hi @ipjohnson
I got a chance to test the project and here is the result:
scope.SetupMvc()is not called insideConfigureContainermethod, it works fine.Action<GraceMVCConfiguration>, it fails with the same error.Action<GraceMVCConfiguration>and theUseControllerActivatorprop is set tofalseagain works fine, but when set totrueit fails again.cuteant commentedon Aug 28, 2019
Thanks!!!
After this bug is fixed, Grace can run perfectly in OrchardCore
ipjohnson commentedon Aug 28, 2019
@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 commentedon 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 commentedon Aug 28, 2019
@cuteant do you have a simple example of this or something you could share that I can run?
cuteant commentedon 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);
}
`
The test case in OrchardCore.Test cannot pass
My english is not so good, I hope you can understand
shahabganji commentedon Aug 28, 2019
Worth to mention that I am running the app on a Mac.
Program.cs
Startup.cs
Screenshot of the Browser
Raw Exception Details
ipjohnson commentedon Aug 29, 2019
@shahabganji this is caused by a combination of
AutoRegisterUnknown = falseandconfig.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 commentedon Sep 9, 2019
I think this can be closed out and I'll push an RC version here this week