Base solution for your next web application
Open Closed

Castle.MicroKernel.Handlers.HandlerException : Can't create component as it has dependencies to be satisfied. #12186


User avatar
0
Astech created

Hi, I have been getting the following error in my dev azure pipeline.

'Congresso.Documents.DocumentManager' is waiting for the following dependencies:

Service 'Microsoft.AspNetCore.SignalR.IHubContext`1[[Congresso.Documents.DocumentHub, Congresso.Core, Version=12.0.0.0, Culture=neutral, PublicKeyToken=null]]' which was not registered.

at Castle.MicroKernel.Handlers.DefaultHandler.AssertNotWaitingForDependency() at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, Burden& burden) at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.ResolveFromKernelByType(CreationContext context, ComponentModel model, DependencyModel dependency) at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency) at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateConstructorArguments(ConstructorCandidate constructor, CreationContext context) at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.Instantiate(CreationContext context) at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context) at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context, Burden burden) at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy) at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, Burden& burden) at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.ResolveFromKernelByType(CreationContext context, ComponentModel model, DependencyModel dependency) at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency) at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateConstructorArguments(ConstructorCandidate constructor, CreationContext context) at Castle.Windsor.MsDependencyInjection.MsScopedLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy) at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, Burden& burden) at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, Arguments additionalArguments, IReleasePolicy policy, Boolean ignoreParentContext) at Castle.Windsor.MsDependencyInjection.ScopedWindsorServiceProvider.GetServiceInternal(Type serviceType, Boolean isOptional) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.AbpZeroIdentityBuilderExtensions.<>c__DisplayClass0_01.&lt;AddAbpTenantManager&gt;b__0(IServiceProvider provider) at Castle.MicroKernel.ComponentActivator.FactoryMethodActivator1.Instantiate(CreationContext context) at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context) at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context, Burden burden) at Castle.Windsor.MsDependencyInjection.MsScopedLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy) at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, Burden& burden) at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, Arguments additionalArguments, IReleasePolicy policy, Boolean ignoreParentContext) at Abp.Events.Bus.EventBus.TriggerHandlingException(IEventHandlerFactory handlerFactory, Type eventType, IEventData eventData, List`1 exceptions) at Abp.Events.Bus.EventBus.Trigger(Type eventType, Object eventSource, IEventData eventData) at Abp.Events.Bus.Entities.EntityChangeEventHelper.TriggerEventWithEntity(Type genericEventType, Object entity, Boolean triggerInCurrentUnitOfWork) at Abp.Events.Bus.Ent

[AbpAuthorize] public class DocumentManager : CongressoDomainServiceBase, IDocumentManager {

private readonly IHubContext&lt;DocumentHub&gt; _documentHub;

public DocumentManager(
    IHubContext&lt;DocumentHub&gt; documentHub,
   )
{
    _documentHub = documentHub;
}


public class DocumentHub : Hub
{
}

}

Any suggestions?

Thanks!


8 Answer(s)
  • User Avatar
    0
    m.aliozkaya created
    Support Team

    Hi @Astech,

    Did you add your hub to endpoints?

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        // Other middleware...
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapHub<DocumentHub>("/documentHub");
        });
    }
    
  • User Avatar
    0
    Astech created

    Hi,

    Yes I already have it added in my startup. The other ones don't seem to be erroring.

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapHub&lt;AbpCommonHub&gt;("/signalr");
                endpoints.MapHub&lt;ChatHub&gt;("/signalr-chat");
                endpoints.MapHub&lt;AgendaHub&gt;("/signalr-agenda");
                endpoints.MapHub&lt;MinuteHub&gt;("/signalr-minute");
                endpoints.MapHub&lt;DocumentHub&gt;("/signalr-document-conversion");
            });
    
  • User Avatar
    0
    m.aliozkaya created
    Support Team

    Hi @Astech,

    Could you share your DocumentHub.cs?

    Also, you can check for this document. https://aspnetboilerplate.com/Pages/Documents/SignalR-Integration

  • User Avatar
    0
    Astech created

    Hi,

    using Microsoft.AspNetCore.SignalR;
    
    namespace Congresso.Documents
    {
        public class DocumentHub : Hub
        {
        }
    }
    

    I did check the link to ensure that the necessary packages are also installed.

  • User Avatar
    0
    m.aliozkaya created
    Support Team

    Hi @Astech,

    You can add dependency with following interface.

    using Microsoft.AspNetCore.SignalR;
    
    namespace Congresso.Documents
    {
        public class DocumentHub : Hub, ITransientDependency
        {
        }
    }
    
  • User Avatar
    0
    Astech created

    Seems to be an issue when trying to write & run tests (in the .Test project), that access services that use a hub

    I'm assuming the startup.cs from the MVC project does not get run when the tests run so how can we ensure these dependencies are registered within the .Test project in the same way they are within the .Mvc project like below:

    app.UseEndpoints(endpoints =>
            {
                endpoints.MapHub<AbpCommonHub>("/signalr");
                endpoints.MapHub<ChatHub>("/signalr-chat");
                endpoints.MapHub<AgendaHub>("/signalr-agenda");
                endpoints.MapHub<MinuteHub>("/signalr-minute");
                endpoints.MapHub<DocumentHub>("/signalr-document-conversion");
            });
    

    There appears to be no startup.cs file in the .Test project to try the same code. Trying to register the Hub in the same way as we register services in the test project:

    public Security_Tests()
    {
        _documentHub = Resolve&lt;IHubContext&lt;DocumentHub&gt;>();
        _documentsAppService = Resolve&lt;IDocumentsAppService&gt;(); // Service that uses the DocumentHub
    }
    

    Results in:

    Abp.AbpException : Can not register IHubContext`1. It should be a non-abstract class. If not, it should be registered before.
    

    Many thanks

  • User Avatar
    0
    m.aliozkaya created
    Support Team

    Hi @Astech,

    You can mock your DocumentHub for unit testing.

    _documentHub = Substitute.For<IHubContext<DocumentHub>>();

  • User Avatar
    0
    Astech created

    Many thanks m.aliozkaya

    This has resolved the issue. To help anyone else, this is the complete code segment for unit testing an app service that utilies SignalR:

    var fakeDocumentHub = Substitute.For&lt;IHubContext&lt;DocumentHub&gt;>();
    LocalIocManager.IocContainer.Register(Component.For&lt;IHubContext&lt;DocumentHub&gt;>().Instance(fakeDocumentHub).IsDefault());
    var _documentsAppService = Resolve&lt;IDocumentsAppService&gt;();
    

    Do this to get the service instance instead of using Dependency Injection in the unit test class itself.

    Thanks again m.aliozkaya