AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.8k stars 2.23k forks source link

The avalonia android program cannot be called the 'InvokeCoreAsync' method if the connection is not active. #14595

Closed IftikharK closed 8 months ago

IftikharK commented 9 months ago

I have cross-platform application and have tried to communicate with the SignalR server in Avalonia desktop, browser and android program. It works fine with desktop and browser program but with android, it throw an exception "The 'InvokeCoreAsync' method cannot be called if the connection is not active". Don't understand why same lines of code working with desktop and browser but not in android. Any help would be greatly appreciated.

using Avalonia.Threading;
using Avalonia.XPlat.Constants;
using Avalonia.XPlat.Interfaces;
using Avalonia.XPlat.Models;
using Avalonia.XPlat.Services;
using Microsoft.AspNetCore.SignalR.Client;
using ReactiveUI;
using System;
using System.Collections.ObjectModel;
using System.Reactive;
using System.Threading.Tasks;

namespace Avalonia.XPlat.ViewModels
{   
    public class ChatViewModel : ViewModelBase
    {
        private readonly HubConnection? _hubConnection;

        private string? _userName;
        private string? _message;
        private string? _viewTitle;
        private ObservableCollection<string?> _messagesList;

        public string? ViewTitle
        {
            get => _viewTitle;
            set => this.RaiseAndSetIfChanged(ref _viewTitle, value);
        }

        public string? UserName
        {
            get => _userName;
            set => this.RaiseAndSetIfChanged(ref _userName, value);
        }

        public string? Message
        {
            get => _message;
            set => this.RaiseAndSetIfChanged(ref _message, value);
        }

        public ObservableCollection<string?> MessagesList
        {
            get => _messagesList;
            set => this.RaiseAndSetIfChanged(ref _messagesList, value);
        }

        public ReactiveCommand<Unit, Unit> ConnectCommand { get; }
        public ReactiveCommand<Unit, Unit> SendCommand { get; }

        private const string BaseUrl = "https://localhost:7256";
        private const string hub = "/chatHub";

        public ChatViewModel()
        {
            ViewTitle = "Real-Time Chat!";

            _messagesList = new ObservableCollection<string?>();

             _hubConnection = new HubConnectionBuilder()
                 .WithUrl($"{BaseUrl}/{hub}")
                 .Build();

            _hubConnection.On<ChatMessage>(
                "ReceiveMessage",
                (message) =>
                {
                    Dispatcher.UIThread.InvokeAsync(() =>
                    {
                        var newMessage = $"{message.User}: {message.Message}";
                        MessagesList.Add(newMessage);
                    });
                }
            );

            SendCommand = ReactiveCommand.Create(SendMessage);
        }

        private async void SendMessage()
        {
            try
            {
                await _hubConnection!.StartAsync();

                var chatMessage = new ChatMessage(UserName, Message);

                await _hubConnection!.InvokeAsync("SendMessage", chatMessage);
            }
            catch (Exception ex)
            {
                MessagesList.Add(ex.Message);
            }
        }       
    }
}
maxkatz6 commented 9 months ago

I am not sure if it's related to Avalonia. Could be something related to websockets implementation on android. Hard to tell from this description.

IftikharK commented 9 months ago

thanks for response. If it's related to websockets implementation then it should also not work with desktop and browser.

Gillibald commented 9 months ago

This is not related to Avalonia. Please have a look at the debug output.

timunie commented 8 months ago

making this a discussion for now as we think it's not related to Avalonia. If it points out it is, we can still convert it into a bug report again.