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

Incompatiblity with Microsoft.Authentication.WebAssembly.Msal Package when using WebWorkers #7

Closed nantcom closed 1 year ago

nantcom commented 1 year ago

First of all, thank you for all the effort you have put into this library :)

Issue:

If the project uses Microsoft.Authentication.WebAssembly.Msal nuget package, an exception will be thrown at this line:

var webWorker = await workerService.GetWebWorker();

Exception Detail

image

Root Cause

The root cause was MSAL library was trying to read appsettings.json which is not available at the location specified.

image

Workaround

Create appsettings.json file at wwwroot/_content/SpawnDev.BlazorJS.WebWorkers/appsettings.json with empty JSON object as follows:

image

with workaround, the library functions normally.

image

LostBeard commented 1 year ago

@nantcom Thank you for submitting your issue. Unfortunately I cannot reproduce it. The demo site uses appsettings.json also. You can see that the appsettings.json is properly loaded by looking at the console output when you run the worker test. It outputs a message from appsettings.json and says what global context it is in.

Below is the message seen in the browser console after starting a worker on the demo site. "Hello from appsettings.json" is read from the appsettings.json file (see Client/Program.cs)

appsettings test in context DedicatedWorkerGlobalScope: Hello from appsettings.json

Can you share a test project that demonstrates the issue? I will take a look and do my best to figure this out.

Funny note... the issue you are reporting is actually the very first issue that was reported on this project. At that time there was an issue with my code and I fixed it. He was also using or planned to use MSAL.

LostBeard commented 1 year ago

I got caught up verifying that appsettigns was loading correctly with my code I partially ignored "If the project uses Microsoft.Authentication.WebAssembly.Msal nuget package ...".

I will create a test app with that included and let you know.

LostBeard commented 1 year ago

I created a basic test project that has the package "Microsoft.Authentication.WebAssembly.Msal". It is loading the appsettings.json correctly. At this point I need more info or an example project to help as I cannot reproduce the reported issue.

BlazorJSMSALTest

nantcom commented 1 year ago

Sorry, I missed one crucial info .🙏 As soon as I add Msal package and have appsettings.json in a project (since you need to configure MSAL) that exception will surface. If you add Msal Nuget and does not have appsettings.json - everything will work fine. But I can see your project has appsettings.json - so I am not sure why. Maybe it's builder.Services.AddMsalAuthentication

I actually lost a bit of hair (since I went to have a haircut to calm down 😂) trying to figure out why creating an empty project + adding your library works and my almost-production project doesn't.

This is the test project I was testing. BlazorParallelWithMSAL.zip

This is me testing it :

https://github.com/LostBeard/SpawnDev.BlazorJS/assets/7698652/4af02056-4f58-447c-8278-643e0b9c941f

IMO, you don't have to fix anything and I think it is MSAL issue that needs a workaround for this specific use case.🥲

LostBeard commented 1 year ago

I see you are using .Net 7. I have been using .Net 8 mostly since it was released. Checking if that may be it...

LostBeard commented 1 year ago

Yup. If I switch the BlazorJSMSALTest project to .Net 7 it has the issue you are seeing. Narrowed it down a little bit...

LostBeard commented 1 year ago

It looks like the reason it works in .Net 8 and not .Net 7 is that appsettings loading was done differently in .Net 7 and was changed to the .Net 8 way after issue #43700 was reported. Still trying to figure out a usable fix.

LostBeard commented 1 year ago

Fixed it! Required modifying the fetch calls from blazor.webassembly.js in web workers so that the urls are relative to the app base path, not the web worker's script path.

Thank you so much for finding this issue and helping me resolve it.

Updating the repo and building an updated Nuget release now.

LostBeard commented 1 year ago

Just ran your test app with WebWorkers 2.1.14 (Nuget is up) and it no longer has that issue. You may have to clear your browser cache to make sure it loads the updated web workers script.