Open Closed

SignalR Hub Service Dependency Error #331


User avatar
0
wazbek created

Hi

I have checked the docs, samples and also the support site but have not found anything documented about the Abp signalR integration on the Angular side.

What do I need to do to get this to work? My code is below.

Thanks,

Warick

  • ABP Framework version: v3.0.4
  • UI type: Angular
  • Tiered (MVC) or Identity Server Seperated (Angular): Identity Server Separated
  • Exception message and stack trace: Access to XMLHttpRequest at 'https://localhost:44336/signalr-hubs/idm/negotiate?negotiateVersion=1' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow- Origin' header is present on the requested resource.
  • Steps to reproduce the issue:
import { Injectable } from '@angular/core';
import * as signalR from '@microsoft/signalr';
import { environment } from '@nexweb/core';
import { IdmManagement } from '../models/idm.models';

@Injectable({
  providedIn: 'root'
})
export class IdmHubService {
  private hubConnection: signalR.HubConnection;
  private idmHubUrl = '/signalr-hubs/idm';
  public hubConnectionEstablished = false;

  constructor() {
    this.buildConnection();
    this.startConnection();
  }

  private buildConnection() {
    this.hubConnection = new signalR.HubConnectionBuilder()
      .withUrl(environment.apis.default.url + this.idmHubUrl)
      .build();
  }

  private startConnection() {
    this.hubConnection
      .start()
      .then(() => {
        this.hubConnectionEstablished = true;
      })
      .catch(err => {
        console.error('Error while starting connection: ' + err);
      });
  }

7 Answer(s)
  • User Avatar
    0
    wazbek created

    Hi

    I managed to find an issue on the server side by looking at the logs. My services are not being resolved. See error below.

    I have placed my Hub class in my Application.csproj. I know the docs recommend either web or api projects but I need to get the IHubContext in my application service to be able to send data back to the hub.

    This is what I would like to achieve. Angular frontend communicates to the hub. The hub then calls to my application service and from there I want to be able to send data back to the Angular frontend using the IHubContext.

    I have had a look at the SignalRTieredDemo application and I see that the hub is in the Web project and there is a ReceivedMessageEventHandler that handles an event that gets published from the ChatAppService.

    Do I need to stick to this architecture or is there another way for me to get this to work as described above?

    Would I also need to use the event bus to publish events from my hub to my application service rather than injecting my application service directly into the hub? I am currently getting this error when injecting the application service into my hub:

    2020-08-03 08:13:22.994 +02:00 [ERR] Error when dispatching 'OnConnectedAsync' on hub.
    Autofac.Core.DependencyResolutionException: An exception was thrown while activating NexModule.Cmt.IdmHub -> NexModule.Cmt.IdmEngineService -> NexModule.Cmt.Services.IdmBidAndAskDataDomainService -> NexModule.Cmt.Services.IdmTradingIntervalDataDomainService -> NexModule.Tsam.TsamInterconDomainService.
     ---> Autofac.Core.DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'NexModule.Tsam.TsamInterconDomainService' can be invoked with the available services and parameters:
    Cannot resolve parameter 'Volo.Abp.Domain.Repositories.IRepository`2[NexModule.Tsam.Domain.TsamInterconCapacity,System.Int32] interconCapacityRepository' of constructor 'Void .ctor(NexModule.Tsam.IInterconDapperRepository, Volo.Abp.Domain.Repositories.IRepository`2[NexModule.Tsam.Domain.TsamInterconCapacity,System.Int32])'.
       at Autofac.Core.Activators.Reflection.ReflectionActivator.GetValidConstructorBindings(ConstructorInfo[] availableConstructors, IComponentContext context, IEnumerable`1 parameters)
       at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
       at Autofac.Core.Resolving.InstanceLookup.CreateInstance(IEnumerable`1 parameters)
       --- End of inner exception stack trace ---
       at Autofac.Core.Resolving.InstanceLookup.CreateInstance(IEnumerable`1 parameters)
       at Autofac.Core.Resolving.InstanceLookup.Execute()
       at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, ResolveRequest request)
       at Autofac.Core.Resolving.ResolveOperation.ResolveComponent(ResolveRequest request)
       at Autofac.Core.Resolving.ResolveOperation.Execute(ResolveRequest request)
       at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(ResolveRequest request)
       at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance)
       at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable`1 parameters)
       at Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType, IEnumerable`1 parameters)
       at Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType)
       at Autofac.Extensions.DependencyInjection.AutofacServiceProvider.GetService(Type serviceType)
       at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
       at Microsoft.AspNetCore.SignalR.Internal.DefaultHubActivator`1.Create()
       at Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher`1.OnConnectedAsync(HubConnectionContext connection)
       at Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher`1.OnConnectedAsync(HubConnectionContext connection)
       at Microsoft.AspNetCore.SignalR.HubConnectionHandler`1.RunHubAsync(HubConnectionContext connection)
    

    Thanks.

  • User Avatar
    0
    wazbek created

    Managed to sort this out by adding this to the EntityFrameworkCoreModule.cs file

    options.AddDefaultRepositories(includeAllEntities: true);
    
  • User Avatar
    0
    nick.pham-mwp created

    Hi @wazbek,

    How are you?

    I am (brand) new to ABP and have an issue SignalR-Chat-Hub-Dependency-Cannot-Be-Resolved similar to yours above and my EntityFrameworkCoreModule.cs file already has options.AddDefaultRepositories(includeAllEntities: true);. However, Autofac still cannot resolve parameter 'Volo.Abp.Identity.IIdentityUserRepository' and 'Microsoft.AspNetCore.Identity.ILookupNormalizer' of constructor 'Void .ctor(Castle.DynamicProxy.IInterceptor[], Volo.Abp.Identity.IIdentityUserRepository, Microsoft.AspNetCore.Identity.ILookupNormalizer)' in the ChatHub.

    I am just wondering if you could please give me some advice to solve this?

    Besides, my Angular UI has CORS issues as well when it tries to hit the Chat hub on the (Razor) Web project. Could you please kindly share how you could overcome this in your solution (if you do not mind please)?

    • maybe you declared the ChatHub in another project such as HttpApiHost?
    • I did configure CORS in the (Razor) Web project as below:

    Thank you.

    Nick

  • User Avatar
    0
    wazbek created

    Hi Nick

    I'm good thanks.

    This sample helped me a lot to figure out how get my application working: https://github.com/abpframework/abp-samples/tree/master/SignalRTieredDemo.

    Yes I moved my hub into the HttpApi project. I basically followed the same architecture in the sample above and got it working. I am not injecting those 2 parameters into my constructor though so I am not sure I can be of any help to you.

    Hopefully ABP provide you with a solution.

  • User Avatar
    0
    nick.pham-mwp created

    Hi @wazbek,

    thank you for your kind suggestion.

    i have followed the sample app SignalRTieredDemo and that actually helps avoid the error since the Hub's constructor does not use those 2 parameters. however, i can only send messages out (from user A) but cannot receive such messages (to user B).

    i am just wondering how you installed RabbitMQ locally, if you do not mind sharing please?

    I have tried both docker image rabbitmq:latest and windows installation via chocolatey but i still have no luck.

    sorry to bother you again. your kind response is greatly appreciated.

    Nick

  • User Avatar
    0
    wazbek created

    Hi Nick

    We didnt install RabbitMQ. Our system is a modular monolith for the moment.

    However it doesnt mean you cant use the distributed event bus. From the Distributed Event Bus documentation https://docs.abp.io/en/abp/latest/Distributed-Event-Bus: "LocalDistributedEventBus is the default implementation that implements the distributed event bus to work as in-process. Yes! The default implementation works just like the local event bus, if you don't configure a real distributed provider."

    Hope that helps.

  • User Avatar
    0
    nick.pham-mwp created

    Hi @wazbek

    Thank you for your kind help sir. Much appreciated.

    I will try the distributed event bus from your advice.

    Thank you so much again :)

    Nick

Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09