question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot send dependency to Configure that depends on IHubContext

See original GitHub issue

An error is thrown when passing a custom service to the Configure method of the Startup class when using AddAzureSignalR service. This is in ASP.NET Core 2.1. My code:

public interface IDatabaseChangeNotificationService
    {
        void Config();
    }
public class SqlDependencyService : IDatabaseChangeNotificationService
{
public SqlDependencyService( IHubContext<ChatHub> chatHub)
        {
        }

        public void Config()
        {
        }
}

// in the ConfigureServices method:

services.AddScoped<IDatabaseChangeNotificationService, SqlDependencyService>();

            services.AddSignalR().AddAzureSignalR("My Secret key");

// Configure method:

public void Configure(IApplicationBuilder app,
            IHostingEnvironment env,
            IDatabaseChangeNotificationService notificationService)
 {
if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseAzureSignalR(x =>
            {
                x.MapHub<ChatHub>("/chatHub");
            });

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

            notificationService.Config();
}

Where ChatHub is a class the inherits from Hub (no constructor on this class). If I run the app, I get the following error:

System.InvalidOperationException: ‘AddAzureSignalR(…)’ was called without a matching call to ‘IApplicationBuilder.UseAzureSignalR(…)’. at Microsoft.Azure.SignalR.ServiceLifetimeManager1..ctor(IServiceConnectionManager1 serviceConnectionManager, IClientConnectionManager clientConnectionManager, IHubProtocolResolver protocolResolver, ILogger1 logger, AzureSignalRMarkerService marker) --- End of stack trace from previous location where exception was thrown --- at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitSingleton(SingletonCallSite singletonCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitSingleton(SingletonCallSite singletonCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.AspNetCore.Hosting.Internal.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)

The issue goes away if I remove the IHubContext dependency from the SqlDependencyService class, or if I remove the use of the functions AddAzureSignalR and UseAzureSignalR.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
xscriptcommented, Sep 11, 2018

Ah, I see. notificationService is a parameter of Configure method. This exception is intentionally put here. Before calling UseAzureSignalR, the use of HubLifetimeManager<> is forbidden. Because connections between your app server and Azure SignalR Service are not set up yet.

As a workaround, you can instantiate notificationService by app.ApplicationServices.GetRequiredService<IDatabaseChangeNotificationService>() after calling UseAzureSignalR.

0reactions
Olusheenorcommented, Jun 27, 2019

I have similar issue but on AspNetcore UseSignalR

Read more comments on GitHub >

github_iconTop Results From Across the Web

asp.net core - .NET 6 IHubContext Dependency Injection
NET 5 before, but I'm running across a DI issue that's got me stumped. In 5, all hubs that were mapped automatically have...
Read more >
SignalR HubContext
Learn how to use the ASP.NET Core SignalR HubContext service for sending notifications to clients from outside a hub.
Read more >
Use hubs in SignalR for ASP.NET Core
Don't instantiate a hub directly via dependency injection. To send messages to a client from elsewhere in your application use an IHubContext ....
Read more >
Build Real-time Applications with ASP.NET Core SignalR
Hubs in ASP.NET Core SignalR now support constructor dependency injection without extra configuration, just like ASP.NET Core controllers or ...
Read more >
Signalr Hub Dependency Injection
Setting up dependency injection with SignalR is pretty straightforward. ... IHubContext from the IoC container and using its methods to send messages to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found