Skip to content

Replace Microsoft.AspNet.WebApi.Client with built-in abstractions #133

Description

@jmbryan4

Problem

The Pathoschild.Http.FluentClient package depends on Microsoft.AspNet.WebApi.Client, which originates from the aspnet/AspNetWebStack repository. That is the legacy ASP.NET stack for MVC 5.x, Web API 2.x, and Web Pages 3.x.

This dependency is problematic for several reasons:

  1. End of support: ASP.NET Web API 2 reached end of support on July 1, 2019.
  2. No active development: No stable source release since October 2020 (5+ years).
  3. System.Net.Http.Formatting and the MediaTypeFormatter abstraction were deliberately not brought forward to ASP.NET Core
  4. Newtonsoft.Json: The package pulls in a transitive dependency on Newtonsoft.Json. .NET moved to System.Text.Json as the platform default since .NET Core 3.0.
  5. System.Net.Http.Json (actively maintained in dotnet/runtime) is the official successor for HTTP content serialization.

Prior discussion

This has come up several times:

From #128: MediaTypeFormatter concept is a legacy part that the .NET team chose not to bring forward.

  1. System.Net.Http.Json design doc explains why they created a new assembly instead of extending System.Net.Http.Formatting: "The existing assembly (which ships in Microsoft.AspNet.WebApi.Client package) provides the integration between HttpClient and JSON.NET. Mixing this with the new System.Text.Json would create a mess."

  2. .NET runtime code in dotnet/runtime's HttpMessageHandlerBuilder.cs explicitly avoids the old package: "This is similar to [System.Net.Http.Formatting/HttpClientFactory.cs] but we don't want to take that package as a dependency."

  3. David Fowler explained in dotnet/aspnetcore#3313 why the shared HTTP model that MediaTypeFormatter depended on was abandoned.

The modern approach is System.Net.Http.Json with simple extension methods (GetFromJsonAsync<T>(), etc.) no formatters, no content negotiation. A rewrite toward that model would be cleaner but would break the entire
public API surface (Formatters on IClient/IRequest/IResponse, IBodyBuilder.Model(), MediaTypeFormatterBase subclasses, etc.). That felt too disruptive for this change.

Proposed solution

Replace Microsoft.AspNet.WebApi.Client with lightweight, built-in abstractions:

  1. Define a new IMediaTypeFormatter interface (replacing System.Net.Http.Formatting.MediaTypeFormatter)
  2. Provide JsonMediaTypeFormatter (using Newtonsoft.Json, preserving existing behavior) and XmlMediaTypeFormatter (using DataContractSerializer)
  3. Create a MediaTypeFormatterCollection (same class name to minimize consumer breakage)
  4. Update target frameworks to netstandard2.0;net8.0

This removes the legacy dependency while keeping Newtonsoft.Json for backward compatibility (consumers can still configure JSON serialization the same way). A future version could add System.Text.Json support as an alternative formatter.

What could be added later (non-breaking):

  • A SystemTextJsonMediaTypeFormatter so consumers can choose between Newtonsoft.Json and System.Text.Json
  • Make System.Text.Json the default, with Newtonsoft.Json as opt-in

PR #134

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions