Class MessageAppService 不能有多个基 类 'Hub' 和 'AsyncCrudAppService'

Class MessageAppService cannot have multiple base classes 'Hub' and 'AsyncCrudAppService'

我正在使用 ASP.NET .NET Core 3.1 样板。

我正在尝试将 SignalR 聊天记录保存到数据库中。问题是当我想创建 AsyncCrudAppServiceHub 的子 class 时,出现以下文本错误:

Class MessageAppService cannot have multiple base classes 'Hub' and 'AsyncCrudAppService'

这是我的代码:

namespace MyProject.ChatAppService
{
    public class MessageAppService : Hub, AsyncCrudAppService<Message, MessageDto, int, PagedAndSortedResultRequestDto, CreateMessageDto, UpdateMessageDto, ReadMessageDto>
    {
        private readonly IRepository<Message> _repository;

        private readonly IDbContextProvider<MyProjectDbContext> _dbContextProvider;
        private MyProjectPanelDbContext db => _dbContextProvider.GetDbContext();

        public MessageAppService(
            IDbContextProvider<MyProjectDbContext> dbContextProvider,
            IRepository<Message> repository)
            : base(repository)
        {
            _repository = repository;
            _dbContextProvider = dbContextProvider;
        }

        public  List<Dictionary<long, Tuple<string, string>>> InboxChat()
        {
            // The result will be List<userid, Tuple<username, latest message>>();
            List<Dictionary<long, Tuple<string, string>>> result = new List<Dictionary<long, Tuple<string, string>>>();

            List<User> listOfAllUsers = db.Set<User>().ToList();

            listOfAllUsers.ForEach((user) =>
            {
                try
                {
                    var dict = new Dictionary<long, Tuple<string, string>>();

                    var latestMessage = (from msg in db.Set<Message>() select msg)
                        .Where(msg => msg.CreatorUserId == user.Id && msg.receiverID == AbpSession.UserId)
                        .OrderByDescending(x => x.CreationTime)
                        .FirstOrDefault()
                        .Text.ToString();

                    dict.Add(user.Id, Tuple.Create(user.UserName, latestMessage));
                    result.Add(dict);
                }
                catch (Exception ex)
                {
                    new UserFriendlyException(ex.Message.ToString());
                }
            });

            return result;
        }

        public List<Message> getMessageHistory(int senderId)
        {
            return _repository.GetAll()
                .Where(x => x.CreatorUserId == senderId && x.receiverID == AbpSession.UserId )
                .ToList();
        }
    }
}

我怎样才能避免这个错误?

更新

这里是MyChatHub代码,我想和AsyncCrudAppService子class合并成一个class(不知道这样对不对但这就是我想到的!)。

public class MyChatHub : Hub, ITransientDependency
{
    public IAbpSession AbpSession { get; set; }

    public ILogger Logger { get; set; }

    public MyChatHub()
    {
        AbpSession = NullAbpSession.Instance;
        Logger = NullLogger.Instance;
    }

    public async Task SendMessage(string message)
    {
        await Clients.All.SendAsync("getMessage", string.Format("User {0}: {1}", AbpSession.UserId, "the message that has been sent from client is "+message));
    }

    public async Task ReceiveMessage(string msg, long userId)
    {
        if (this.Clients != null)
        {
            await Clients.User(userId.ToString())
                .SendAsync("ReceiveMessage", msg, "From Server by userID ", Context.ConnectionId, Clock.Now);
        }
        else
        {
            throw new UserFriendlyException("something wrong");
        }
    }

    public override async Task OnConnectedAsync()
    {
        await base.OnConnectedAsync();
        Logger.Debug("A client connected to MyChatHub: " + Context.ConnectionId);
    }

    public override async Task OnDisconnectedAsync(Exception exception)
    {
        await base.OnDisconnectedAsync(exception);
        Logger.Debug("A client disconnected from MyChatHub: " + Context.ConnectionId);
    }
}    

您的 AsyncCrudAppService 子类不能也不应该继承 Hub

相反,注入并使用类似于 ABP 的 SignalRRealTimeNotifierIHubContext<MyChatHub>

public MessageAppService(
    IHubContext<MyChatHub> hubContext,
    IDbContextProvider<MyProjectDbContext> dbContextProvider,
    IRepository<Message> repository)
    : base(repository)
{
    _dbContextProvider = dbContextProvider;
    _hubContext = hubContext;
    _repository = repository;
}

要向所有客户端发送消息,请调用 _hubContext.Clients.All.SendAsync(...)

参考文献: