向我的Xamarin应用聊天添加实时功能会引发System.InvalidOperationException

问题描述 投票:0回答:1

[我创建了一个带有聊天和API的Xamarin应用,用于存储数据,移动应用每3分钟向api发送一次聊天消息请求。

我决定按照评论中的建议使用SignalR:

-如图所示将其添加到我的CongifureService中:

public void ConfigureServices(IServiceCollection services)
    {           
        services.AddDbContext<HostelContext>(opt =>
           opt.UseSqlServer(Configuration.GetConnectionString("HostelContext")));

        services.AddCors();
        services.AddControllers();

        services.AddAuthentication("BasicAuthentication")
            .AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>("BasicAuthentication", null);

        services.AddScoped<IUsersService, UsersService>();
        services.AddScoped<IConversationsService, ConversationsService>();
        services.AddScoped<IMessagesService, MessagesService>();

        // Register the Swagger generator, defining 1 or more Swagger documents
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "HostelApi", Version = "v1" });             
        });

        services.AddSignalR();
    }

-添加了要配置的端点,如下所示:

app.UseEndpoints(endpoints =>
        {                
            endpoints.MapControllers();
            endpoints.MapHub<ChatHub>("/chatHub");
        });

-将类添加到名为hub的新文件夹中的api项目中>

-这是Hub类:

//[Authorize]    
public class ChatHub : Hub
{                
    public async Task SendMessage(Message message)
    {
        //await Clients.All.SendAsync("ReceiveMessage", message);
        await Clients.All.SendAsync("ReceiveMessage" + message.ConversationId, message);
        //await Clients.Users(destinationUserIdList).SendAsync("ReceiveMessage" + message.ConversationId, message);
    }
}

在我的Xamarin应用程序中:

-添加了集线器服务:

-这是集线器服务类:

class HubService : IHubService
{
    public HubConnection HubConnection { get; }
    public HubService()
    {
        HubConnection = new HubConnectionBuilder()
            .WithUrl($"https://10.0.2.2:5001/chatHub")
            .Build();
    }

    public async Task Connect()
    {
        await HubConnection.StartAsync().ConfigureAwait(false);
    }

    public async Task Disconnect()
    {
        await HubConnection.StopAsync().ConfigureAwait(false);
    }

    public async Task SendMessage(Message message)
    {            
        await HubConnection.InvokeAsync("SendMessage", message).ConfigureAwait(false);
    }
}

-登录后启动应用程序时连接到集线器:

public MainPage()
    {
        InitializeComponent();                   

        MasterBehavior = MasterBehavior.Popover;

        //should be home page
        MenuPages.Add((int)MenuItemType.Home, (NavigationPage)Detail);

        HubService.Connect().ConfigureAwait(true);            
    }

-注销时关闭连接

-发送消息时,我调用SendMessage方法:

await HubService.SendMessage(message).ConfigureAwait(true);
            OutGoingText = string.Empty;
            Messages.Add(message);

现在的问题是当我发送消息并到达:

public async Task SendMessage(Message message)
    {            
        await HubConnection.InvokeAsync("SendMessage", message).ConfigureAwait(false);
    }

抛出此:

System.InvalidOperationException: 'The 'InvokeCoreAsync' method cannot be called if the connection is not active'

已尝试使用HubService.Connect()。Wait(),但应用程序处于循环状态

有人可以帮我吗? @尼克·科瓦尔斯基...

最好的问候

我创建了一个具有聊天功能和API来存储其数据的Xamarin应用,每3分钟移动应用向api请求一次聊天消息。我决定按照...

c# .net xamarin signalr chat
1个回答
0
投票

我发现了问题!

© www.soinside.com 2019 - 2024. All rights reserved.