aspnetcore: SetCookieHeaderValue - System.TypeLoadException: Could not load type Microsoft.Extensions.Primitives.InplaceStringBuilder'

Description

This API is raising an exception because it cannot find type Microsoft.Extensions.Primitives.InplaceStringBuilder.

This happens both on .NET Framework 4.7.2 as well as .NET 6.0. I tried reference Microsoft.Net.Http.Headers 2.2.8, but the same exception is raised.

#region Assembly Microsoft.Net.Http.Headers, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60 // D:\Git\AD-AggregatorService-Common\packages\microsoft.net.http.headers\2.2.0\lib\netstandard2.0\Microsoft.Net.Http.Headers.dll // Decompiled with ICSharpCode.Decompiler 6.1.0.5902 #endregion

Test method Microsoft.Identity.Services.Http.UnitTest.CookieTest.Cookie_HeaderRoundtripping_Test threw exception: System.TypeLoadException: Could not load type ‘Microsoft.Extensions.Primitives.InplaceStringBuilder’ from assembly ‘Microsoft.Extensions.Primitives, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60’.

Stack Trace:  SetCookieHeaderValue.ToString() Cookie.ToSetCookieHeader() line 115 CookieTest.Cookie_HeaderRoundtripping_Test(String originalHeader, String expectedHeader) line 33

Reproduction Steps

        internal string ToSetCookieHeader()
        {
            SetCookieHeaderValue header = new SetCookieHeaderValue(this.Name, this.Value);
            header.Domain = this.Domain;
            header.Path = this.Path;
            header.MaxAge = this.MaxAge;
            header.Expires = this.Expires;
            header.HttpOnly = this.HttpOnly;
            header.Secure = this.Secure;
            header.SameSite = ConvertSameSiteModeAppToFramework(this.SameSite);

            return header.ToString();
        }

Expected behavior

SetCookieHeaderValue.ToString() should not throw.

Actual behavior

SetCookieHeaderValue.ToString() raises TypeLoadException.

Regression?

No response

Known Workarounds

No response

Configuration

.NET Framework 4.7.2 and .NET 6

Windows 11, x64

Other information

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 23 (15 by maintainers)

Most upvoted comments

That’s exactly the case.

@davidfowl any chances we could retain InPlaceStringBuilder, mark it as Obsolete instead? A quick search online finds us many threads about typeloadexception on InPlaceStringBuilder type … That would solve a lot of problems to the community.

This issue is pretty annoying because it means you can run into problems whenever you pair one of the older packages with a newer version of Microsoft.Extensions.Primitives.

This situation is relatively easy to get yourself into if you have libraries that need to support .NET Framework and .NET Core, since .NET Framework only works with the older ASP.NET Core 2.x packages which rely on InPlaceStringBuilder.

If the InPlaceStringBuilder API had been retained or could be restored it would have (and still could) save us a lot of pain. Seems like it could just be marked as Obsolete.

Is Microsoft.Net.Http.Headers even a supported package anymore? I don’t see any updates to it in over 2 years.

I’m not inclined to add this API back in order to support an EOL ASP.NET package. If something needs to change here, Microsoft.Net.Http.Headers should be updated to the latest Microsoft.Extensions.Primitives version. I can transfer this issue to the dotnet/aspnetcore repo if that is the route we want to go. Or if that package is EOL, we can just close this and say “don’t use that package anymore”.

Is there an equivalent class to SetCookieHeaderValue with same ToString() logic elsewhere?

See also https://github.com/dotnet/extensions/issues/3800 which is a duplicate and resolved as “Won’t fix”