z4kn4fein / stashbox

A lightweight, fast, and portable dependency injection framework for .NET-based solutions.
https://z4kn4fein.github.io/stashbox
MIT License
140 stars 10 forks source link

Unable to resolve IHubContext<THub, T> #114

Closed adambajguz closed 2 years ago

adambajguz commented 2 years ago

Hi, I have a problem with integration of Stashbox into an offical SignalR Server Example. Strangely a version that uses Microsoft.Extenstions.DependencyInjections works, but as soon as I add Stashbox by adding

    <ItemGroup>
        <PackageReference Include="Stashbox" Version="5.1.0" />
        <PackageReference Include="Stashbox.Extensions.Hosting" Version="4.0.1" />
    </ItemGroup>

to Server.csproj and

                .UseStashbox(container => // Optional configuration options.
                {
                    // This one enables the lifetime validation for production environments too.
                    container.Configure(config => config.WithLifetimeValidation());
                })
                .ConfigureContainer<IStashboxContainer>((context, container) =>
                {
                    // Execute a dependency tree validation.
                    if (context.HostingEnvironment.IsDevelopment())
                        container.Validate();
                })

to Program.cs, the example throws

Container validation failed. See the inner exceptions for details. (Unable to resolve type Server.Worker.
Constructor Void .ctor(Microsoft.Extensions.Logging.ILogger`1[Server.Worker], Microsoft.AspNetCore.SignalR.IHubContext`2[Server.ClockHub,HubServiceInterfaces.IClock]) found with unresolvable parameter: (Microsoft.AspNetCore.SignalR.IHubContext`2[[Server.ClockHub, Server, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[HubServiceInterfaces.IClock, HubServiceInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]])clockHub.
)

After Worker ctor modification

        public Worker(ILogger<Worker> logger, IServiceProvider serviceProvider)
        {
            _logger = logger;
            _clockHub = serviceProvider.GetService<IHubContext<ClockHub, IClock>>();
        }

I've found that serviceProvider.GetService<IHubContext<ClockHub, IClock>>() returns null.

Since IHubContext<THub, T> is an open generic registration, probably open generic type resolver does not function properly.

z4kn4fein commented 2 years ago

Thank you for reporting the issue, I'm going to check what's causing it!

z4kn4fein commented 2 years ago

Hi @adambajguz, I released the fix in Stashbox v5.2.0 / Stashbox.Extensions.Hosting v4.1.0

adambajguz commented 2 years ago

Hi @z4kn4fein, thanks I'll test this later today :)

adambajguz commented 2 years ago

Hi @z4kn4fein, it works! Thanks for super fast fix 🥇