LostBeard / SpawnDev.BlazorJS

Full Blazor WebAssembly and Javascript Interop with multithreading via WebWorkers
https://blazorjs.spawndev.com
MIT License
78 stars 6 forks source link

Webworkers gives error with msal #1

Closed danielpastoor closed 1 year ago

danielpastoor commented 1 year ago

When I use the webworkers package with MSAL.

Then I get this error: "There seems to be a problem. Try is later again!

Message:

Error: could not resolve endpoints. Please check network and try again. Detail: TypeError: Cannot read properties of null (reading 'filter') "

LostBeard commented 1 year ago

That is not an error from WebWorkers. Without more info it has hard to tell where or what the error's cause is. If you can create a minimal working example repo that shows the failure I might be able to help.

danielpastoor commented 1 year ago

I have found that it is not working when I have this in my program.cs:

var workerService = host.Services.GetRequiredService(); await workerService.InitAsync();

I will create a example project tommorrow

LostBeard commented 1 year ago

I have updated the BlazorJS and WebWorkers initialization code needed in the Program.cs file (see the README) and fixed some bugs. The latest version may solve your issue.

danielpastoor commented 1 year ago

Msal bug seems to be fixed thank you.

But when I try to run the example in my project then I get this error:



       System.AggregateException: One or more errors occurred. (The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. LineNumber: 0 | BytePositionInLine: 0.)
 ---> System.Text.Json.JsonReaderException: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. LineNumber: 0 | BytePositionInLine: 0.
   at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan`1 bytes)
   at System.Text.Json.Utf8JsonReader.Read()
   at System.Text.Json.JsonDocument.Parse(ReadOnlySpan`1 utf8JsonSpan, JsonReaderOptions readerOptions, MetadataDb& database, StackRowStack& stack)
   at System.Text.Json.JsonDocument.Parse(ReadOnlyMemory`1 utf8Json, JsonReaderOptions readerOptions, Byte[] extraRentedArrayPoolBytes, PooledByteBufferWriter extraPooledByteBufferWriter)
   at System.Text.Json.JsonDocument.Parse(ReadOnlyMemory`1 json, JsonDocumentOptions options)
   at System.Text.Json.JsonDocument.Parse(String json, JsonDocumentOptions options)
   at Microsoft.Extensions.Configuration.Json.JsonConfigurationFileParser.ParseStream(Stream input)
   at Microsoft.Extensions.Configuration.Json.JsonConfigurationFileParser.Parse(Stream input)
   at Microsoft.Extensions.Configuration.Json.JsonStreamConfigurationProvider.Load(Stream stream)
   at Microsoft.Extensions.Configuration.StreamConfigurationProvider.Load()
   at Microsoft.AspNetCore.Components.WebAssembly.Hosting.WebAssemblyHostConfiguration.Add(IConfigurationSource source)
   at Microsoft.Extensions.Configuration.ConfigurationExtensions.Add[JsonStreamConfigurationSource](IConfigurationBuilder builder, Action`1 configureSource)
   at Microsoft.AspNetCore.Components.WebAssembly.Hosting.WebAssemblyHostBuilder.InitializeEnvironment(IJSUnmarshalledRuntime jsRuntime)
   at Microsoft.AspNetCore.Components.WebAssembly.Hosting.WebAssemblyHostBuilder..ctor(IJSUnmarshalledRuntime jsRuntime, JsonSerializerOptions jsonOptions)
   at Microsoft.AspNetCore.Components.WebAssembly.Hosting.WebAssemblyHostBuilder.CreateDefault(String[] args)
   at Program.<Main>$(String[] args) in C:\Users\DanielPastoor\source\repos\nuget\Blazor.Test.Webworkers\Blazor.Test.Webworkers\Program.cs:line 8
   --- End of inner exception stack trace ---```
LostBeard commented 1 year ago

Can you post your Program.cs file?

danielpastoor commented 1 year ago
using Blazor.Test.Webworkers;
using Blazor.Test.Webworkers.Services;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using SpawnDev.BlazorJS;
using SpawnDev.BlazorJS.WebWorkers;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

//builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

builder.Services.AddSingleton((sp) => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
// Add SpawnDev.BlazorJS.BlazorJSRuntime
builder.Services.AddBlazorJSRuntime();
// Add SpawnDev.BlazorJS.WebWorkers.WebWorkerService
builder.Services.AddWebWorkerService();

builder.Services.AddSingleton<WebWorkerPool>();

builder.Services.AddSingleton<IMathsService, MathsService>();

//builder.Services.AddSingleton(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

builder.Services.AddMsalAuthentication(options =>
{
    builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
    options.ProviderOptions.LoginMode = "redirect";
});

await builder.Build().BlazorJSRunAsync();
danielpastoor commented 1 year ago

Here I have also a test project.

My end goal is to use the Webworkers to do heavy stuff outside the ui threat and also call api's outside the ui threat Blazor.Test.Webworkers.zip

LostBeard commented 1 year ago

I may not have time to find a fix ATM, but it appears to be that in the Worker thread it is trying to load your appsettings.json file from the wrong place (_framework/appsettings.json) I know I had to modify the baseUri in the worker threads to allow HttpClient to work correctly from worker services, so I probably missed something related. I will get to it ASAP. Thank you for helping!

LostBeard commented 1 year ago

I fixed the issue and updated the Nuget packages and repo. I also noticed you are using a script tag in your index.html to load "AuthenticationService.js".

Previously WebWorkers did not read the index.html file because they didn't need to. They just used the blazor.webassembly.js file to load Blazor. Because you may need it and I can imagine others might need it... I added the ability to have the WebWorkers load those index.html script tags if they have the attribute "webworker-enabled".

Example for loading AuthenticationService.js in the WebWorkers context when they load.

<script src="_content/Microsoft.Authentication.WebAssembly.Msal/AuthenticationService.js" webworker-enabled></script>

Good luck. Let me know if you have any other issues.