MobileBlazorBindings icon indicating copy to clipboard operation
MobileBlazorBindings copied to clipboard

'AttachToIpcChannel must be called before using IJSRuntime.'

Open ktaze1 opened this issue 4 years ago • 0 comments

Hello.

I've been developing a blazor app both for web and mobile and I stumbled upon an error which I believe is about MBB.

So for authentication purposes I've made an AuthService and added as scoped service. So the AuthSerivice looks like this:

public class AuthService : ServiceBase, IAuthService
 {
    public AuthService(HttpClient httpClient, ISessionStorageService sessionStorage) : base(httpClient, sessionStorage)
    {

    }


    public async Task<CurrentUser> CurrentUserInfoAsync()
    {
       // some implementation
    }

   public async Task<Response<LoginResultDto>> AuthenticateAsync(LoginDto loginRequest)
   {
       // some implementation
   }
}

as you can see it inherits the ServiceBase which looks like this:

public class ServiceBase
    {
        protected readonly HttpClient _httpClient;
        protected readonly ISessionStorageService _sessionStorage;

        public ServiceBase(HttpClient httpClient, ISessionStorageService sessionStorage)
        {
            _httpClient = httpClient;
            _sessionStorage = sessionStorage;
            InitService();
        }

        protected async void InitService()
        {
            if (_httpClient.DefaultRequestHeaders.Authorization == null)
            {
                var accesToken = await _sessionStorage.GetItemAsync<string>("accessToken");
                if (!string.IsNullOrEmpty(accesToken))
                {
                    _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accesToken);
                }
            }
        }
    }

So this works well on the web. But whenever I try to open this on mobile, program crashes on the var accesToken = await _sessionStorage.GetItemAsync<string>("accessToken"); line in the ServiceBase class' InitService method, saying System.InvalidOperationException: 'AttachToIpcChannel must be called before using IJSRuntime.'

What might be cause of this?

ktaze1 avatar Aug 10 '21 07:08 ktaze1