microsoft-authentication-library-for-dotnet
microsoft-authentication-library-for-dotnet copied to clipboard
Port to System.Text.Json + Source Generators for JSON parsing + enable ILTrimming
Fixes #3407 Replaces #3428
Changes proposed in this request Removes Newtonsoft.Json, and replaces it with System.Text.Json.
Testing All tests are passing, so it should be an exact match of how the existing APIs work. No new API, no new nothing, aside from a MUCH smaller binary footprint, as well as being IL-trimmable. To test this, create a net-6.0 console app and call something from MSAL, like:
var app = PublicClientApplicationBuilder
.Create(clientId)
.WithTenantId(tenantId)
.WithRedirectUri("http://localhost")
.Build();
var accounts = await app.GetAccountsAsync();
AuthenticationResult result;
try
{
result = await app.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
.ExecuteAsync();
}
catch (MsalUiRequiredException)
{
result = await app.AcquireTokenInteractive(scopes)
.ExecuteAsync();
}
(Just an example, as any API should work).
And in your csproj set these:
<PublishTrimmed>true</PublishTrimmed>
<PublishSingleFile>true</PublishSingleFile>
Then run:
dotnet restore MyApp -r win-x64 /p:Configuration=Release
dotnet build MyApp --no-restore --self-contained -r win-x64 /p:Configuration=Release
dotnet publish MyApp --no-build --self-contained -r win-x64 /p:Configuration=Release /p:TrimmerSingleWarn=false
This will generate a single file in a path like MyApp\bin\Release\net6.0\win-x64\publish\MyApp.exe
.
Run it and it should work.
Performance impact Performance should greatly improve after the source generators are implemented since no reflection will be used anymore for the Json parsing.
Really no idea what is going on with the generators for macos... @pmaytak / @bgavrilMS any idea?
Really no idea what is going on with the generators for macos... @pmaytak / @bgavrilMS any idea?
No idea either. xamarin should be compatible with netstandard 2.0 version of system.text ...
Mentioned offline - maybe we just target older unsupported versions. We target iOS10 but System.Text.Json supports 10.14+ via netstandard2. @trwalke also had similar issues with NetStandard2 logger and Mac target.
Issues remaining:
- [ ] MacOs build for ios automation is failing. Seems to be a problem with Mono, used by the Xamarin.iOS task to build an app for test (i.e. not the product itself). @SameerK-MSFT is looking as this, as part of the MAUI work he's doing. If fix is taking too long, we are ok with temporarily disabling iOS automation tests to be able to ship MAUI and System.Text.Json
- [ ] ok to drop net45 (I will probably add more cleanup for this), but please keep netcoreapp 2.1 and net5-win* target as they are for now. Let's upgrade only when they block something.
Turns out that STJ doesn't work with netcoreapp2.1, so I had to keep 3.1. I did some additional net45 cleanup, and reverted the net5-windows->net6-windows changes. Only outstanding issue now is the MacOS build.
Hey @bgavrilMS / @pmaytak , I've rebased this on main and things are looking good now. I've also removed the Net5WamInterop class, since that is not needed anymore since it is provided by C#/WinRT: https://github.com/microsoft/CsWinRT/blob/b8c50f3288dc08be3682493df24498ad1c0d16ae/src/cswinrt/strings/ComInteropHelpers.cs
@azchohfi Based on the offline discussions, we'll be trying to do side-by-side Newtonsoft + STJ implementation. STJ will be for new net6 target, Newtonsoft for the others. Probably a bit less cleaner implementation wise but safer from the MSAL package implementation. We can't really deprecate netcoreapp2.1 target since people seem to be still using it. We also can't use STJ 6x for lower targets since it has some breaking changes plus will mess up some dependencies (like for R9).
Ok @pmaytak Any ETA for when this might land? I'm trying to at least keep the branch synced with main.
@azchohfi - looks like @pmaytak is finalizing the compromise work where we'll keep Newtonsoft side by side with SJT. SJT will be used on the new net6 target. Closing this PR in favor of the one from Peter, which is a superset of your work.
https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/pull/3605