Skip to content

"Microsoft.Extensions.Http.Resilience 8.0.1" adds 1.2 MBytes of packages #4937

Closed

Description

@barruka

This is just my two cents and I might be talking out of my hat, so please, no one get offended.

I was using "Microsoft.Extensions.Http.Resilience 8.0.1" to simply add standard resilience to HttpClient calls, then I've realized that the package is adding 1.2 MBytes of dlls to the project.
image

I think this is totally unaceptable to simpy add a standard resilience to the HttpClient calls. I think the key problem is: We need to add resilience to HttpClient calls: N retries with some delay, then OK or FAIL, that's it.

My believe is that the ASP.NET team is doing a big effort to achieve NativeAOT but I don't think this package is helping to achieve that goal.

Activity

  1. martintmk commented on Feb 14, 2024

    @martintmk
    Contributor

    Hey @barruka, you are right that Microsoft.Extensions.Http.Resilience adds a bunch of dependencies. The good thing is that all these (up to the Polly itself) are trimming-friendly. So if you care about the publish size, I recommend to enable native AOT.

    I did some tests just to see how some HTTP features affect the package size:

    Hello World Console - 1.2MB

    Console.WriteLine("Hello World");

    HTTP Client - 5.1MB

    var client = new HttpClient();
    var result = await client.GetStringAsync("https://jsonplaceholder.typicode.com/todos");

    HTTP Client with ResilienceHandler - 5.8MB

    var resilienceHandler = new ResilienceHandler(
        new ResiliencePipelineBuilder<HttpResponseMessage>()
            .AddRetry(new HttpRetryStrategyOptions())
            .Build());
    
    resilienceHandler.InnerHandler = new HttpClientHandler();
    
    var client = new HttpClient(resilienceHandler);
    var result = await client.GetStringAsync("https://jsonplaceholder.typicode.com/todos");

    HTTP Client with ResilienceHandler and standard resilience - 6.32 MB

    This sample illustrates how to create "standard resilience" without the DI.

    var standardOptions = new HttpStandardResilienceOptions();
    
    var resilienceHandler = new ResilienceHandler(new ResiliencePipelineBuilder<HttpResponseMessage>()
        .AddRateLimiter(standardOptions.RateLimiter)
        .AddTimeout(standardOptions.TotalRequestTimeout)
        .AddRetry(standardOptions.Retry)
        .AddCircuitBreaker(standardOptions.CircuitBreaker)
        .AddTimeout(standardOptions.AttemptTimeout)
        .Build());
    
    resilienceHandler.InnerHandler = new HttpClientHandler();
    
    var client = new HttpClient(resilienceHandler);
    var result = await client.GetStringAsync("https://jsonplaceholder.typicode.com/todos");

    HTTP Client with raw resilience - 5.745 MB

    var pipeline = new ResiliencePipelineBuilder<HttpResponseMessage>()
        .AddRetry(new Polly.Retry.RetryStrategyOptions<HttpResponseMessage>())
        .Build();
    
    using var message = new HttpRequestMessage(HttpMethod.Get, "https://jsonplaceholder.typicode.com/todos");
    
    var client = new HttpClient();
    await pipeline.ExecuteAsync(async token => await client.SendAsync(message, CancellationToken.None));

    HTTP Client with standard resilience - 7.2 MB

    var services = new ServiceCollection();
    services.AddHttpClient("my-endpoint").AddStandardResilienceHandler();
    
    var client = services
        .BuildServiceProvider()
        .GetRequiredService<IHttpClientFactory>()
        .CreateClient("my-endpoint");
    
    var result = await client.GetStringAsync("https://jsonplaceholder.typicode.com/todos");

    My csproj is:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <PublishAot>true</PublishAot>
        <InvariantGlobalization>true</InvariantGlobalization>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.2.0" />
      </ItemGroup>
    
    </Project>

    And output executable was generated by calling dotnet publish command. So if you want to cut the assembly size to minimum, while still keeping the resilience, you might want to use the ResilienceHandler and define your own resilience pipeline with retries. Over raw HttpClient this adds around 700KB of size. You can also use raw Polly without ResilienceHandler, but that only saves about 50KB, so I would go with ResilienceHandler. All other types are trimmed out.

    FYI, we recommend AddStandardResilienceHandler as a go-to resilience solution, because it has everything set-up by default (logging, telemetry, pipeline of strategies), but as you see you have plenty of options to setup-the resilience.

  2. locked and limited conversation to collaborators on Feb 14, 2024
  3. converted this issue into a discussion #4944 on Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions