After upgrade to .NET 8 and Microsoft.Identity.Web 2.x: Could not load type 'Microsoft.Identity.Web.ITokenAcquisition'

216 Views Asked by At

I've just upgraded

  • .net 7 blazor server asp.net app to .NET 8
  • Microsoft.Identity.Web nuget from 1.x to 2.x

and I'm getting exception from app.Build():

'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Sylex.ISYS.SharepointPortal.ISharePointClientContextFactory Lifetime: Scoped ImplementationType: Sylex.ISYS.SharepointPortal.SharePointClientContextFactory': Could not load type 'Microsoft.Identity.Web.ITokenAcquisition' from assembly 'Microsoft.Identity.Web, Version=2.16.0.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae'.)

Here is the code that worked before:

builder.services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)                   
    .AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>("Basic", null) 
    .AddMicrosoftIdentityWebApp(configuration.GetSection("AzureAd"))            
    .EnableTokenAcquisitionToCallDownstreamApi(sharepointScopes)
    .AddDistributedTokenCaches();

builder.Services.AddScoped<ISharePointClientContextFactory, SharePointClientContextFactory>(); //this type uses ITokenAquisition

enter image description here

2

There are 2 best solutions below

0
Tiny Wang On

I had a test in my side and it worked for me to use ITokenAcquisition to generate access token. I'm using Microsoft.Identity.Web with version 2.16.0.

I'm afraid the issue is related to how we migrate from .net 7 to .net 8. I have a detailed steps about how I migrate in this answer before testing ITokenAcquisition.

enter image description here

1
Liero On

After I upgraded all other unrelated packages, the problem disappeared.

No sure which one was causing the error:

<PackageReference Include="Microsoft.SharePointOnline.CSOM" Version="16.1.24322.12000" />
<PackageReference Include="Blazor.ContextMenu" Version="1.17.0" />
<PackageReference Include="Blazored.Typeahead" Version="4.7.0" />
<PackageReference Include="DocumentFormat.OpenXml" Version="3.0.0" />
<PackageReference Include="FluentValidation" Version="11.9.0" />
<PackageReference Include="libphonenumber-csharp" Version="8.13.27" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
<PackageReference Include="Microsoft.Extensions.Caching.SqlServer" Version="8.0.0" />  
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="8.0.0" />
<PackageReference Include="Microsoft.Identity.Web" Version="2.16.0" />
<PackageReference Include="Microsoft.Playwright" Version="1.17.0" />
<PackageReference Include="Microsoft.TypeScript.MSBuild" Version="5.3.3" PrivateAssets="all" />
<PackageReference Include="moment.net" Version="1.3.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Syncfusion.Blazor.Calendars" Version="24.1.43" />
<PackageReference Include="Syncfusion.Blazor.DropDowns" Version="24.1.43" />
<PackageReference Include="Syncfusion.Blazor.Themes" Version="24.1.43" />
<PackageReference Include="System.Reactive" Version="6.0.0" />
<PackageReference Include="Thinktecture.EntityFrameworkCore.SqlServer" Version="8.0.0" />
<PackageReference Include="Toolbelt.Blazor.HotKeys2" Version="3.2.1" />