Open Closed

Integrate Modules to Blazor Web App (v8.2) #7430


User avatar
0
hanntd created

Hi ABP Team, I have a problem when upgrading to v8.2 and using new template of Blazor Web App. My current solution using version 8.1.4 as following:

  1. I developed some business function in separated module using ABP Suite Module Application Template. In each module I developed UI in Blazor Project, I didn't use Blazor Server but using Blazor Web Assembly.
  2. Above modules after completed development I have built into nuget packages
  3. I used ABP Suite to generate a Blazor Web Assembly solution as the Main Application then I integrated above built modules through nuget package. Everything are ok with v8.1.4 or earlier versions.

Now I'd like to use the new application template Blazor Web App as the Main Application replace for the above Main Application (Blazor Web Assembly) and I don't know how to integrate these modules with the new Main Application structure as now we have 2 Blazor projects (Blazor and Blazor.Client). Is there any document or guidline for this case? I have tried to do as earlier versions but it seems having error with authentication when my component in module is using signalR and error happened with code of getting access_token In razor page I have injected: @inject IAccessTokenProvider TokenProvider and here code of SignalR:

#region SignalR 
        public async Task GetConnectSignalR()
        {
            var tokenResult = await TokenProvider.RequestAccessToken();
            var apiURL = Configuration.GetValue<string>("SignalR:Url");
            Console.WriteLine("Getting SignalR-Common from appsettings.json: " + apiURL);
            try
            {
                if (tokenResult.TryGetToken(out var token))
                {
                    _hubConnection = new HubConnectionBuilder()
                                    .WithUrl($"{apiURL}", options =>
                                    {
                                        options.AccessTokenProvider = async () => await Task.FromResult(token.Value);
                                    }).Build();
                        _hubConnection.On<NotificationCreateDto>("ReceiveMessage",
                        (message) =>
                        {
                            _messages.Add(message);
                            Console.WriteLine("Received message from server: " + message);
                            _ = GetNotificationListAsync();
                        });

                    await _hubConnection.StartAsync();
                    Console.WriteLine("SignalR-Common connected");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error starting HubConnection: " + ex.Message);
            }
        } 
        public async Task SendMessage()
        {
            try
            {
                await _hubConnection.SendAsync("SendMessage", _message);
                _message = new NotificationCreateDto();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error sending message: " + ex.Message);
            }
        }
        public void Dispose()
        {
            _ = _hubConnection?.DisposeAsync();
        } 
        #endregion

Error: blazor.web.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Cannot provide a value for property 'TokenProvider' on type 'HQSOFT.Common.Blazor.Pages.Component.HQSOFTNotifications'. There is no registered service of type 'Microsoft.AspNetCore.Components.WebAssembly.Authentication.IAccessTokenProvider'. System.InvalidOperationException: Cannot provide a value for property 'TokenProvider' on type 'HQSOFT.Common.Blazor.Pages.Component.HQSOFTNotifications'. There is no registered service of type 'Microsoft.AspNetCore.Components.WebAssembly.Authentication.IAccessTokenProvider'. Thanks Dharma Han Nguyen


4 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can inject the IAbpAccessTokenProvider to get access token

  • User Avatar
    0
    hanntd created

    Hi, Is there any document about IAbpAccessTokenProvider, I search in ABP standard document but didn't see? Thanks

  • User Avatar
    0
    hanntd created

    I can use GetTokenAsync() of IAbpAccessTokenProvider in my module and now already resolved my issue. Thank you so much!

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Blazor web app has two runtimes(Blazor server/ webassembly). The TokenProvider is only working for webassembly, that's why it's not working.

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11