Aws-sdk-net: Make IHttpClientFactory available for CORECLR?

Created on 1 Nov 2018  路  5Comments  路  Source: aws/aws-sdk-net

The commit d518dd18ff1e80258cdde24ceef63471e3959efa was brought to my attention by @AnthonySteele for supporting the use of Microsoft's new IHttpClientFactory in .NET Core 2.1 for use with the AWS SDK.

However after looking into it, it's only available to the portable-net45+win8+wpa81+wp8 TFM, and not the netstandard1.3 TFM.

Is there a specific reason for it not being enabled for .NET Core? If not, are you open for me opening a PR to enable it for CORECLR in the source code?

Expected Behavior

The ability to use IClientConfig.HttpClientFactory for .NET Standard-targeting applications.

Current Behavior

The HttpClientFactory property is not available on the IClientConfig interface with the netstandard1.3 TFM assembly.

Possible Solution

Add HttpClientFactory to IClientConfig for the CORECLR define constant. For example this line of code could be changed to:

#if PCL || CORECLR

Context

We'd like to be able to plug IHttpClientFactory into the AWS SDK easily to leverage changes Microsoft have made their, like connection pooling etc. While it is _possible_ to integrate IHttpClientFactory into the AWS SDK today, it is non-trivial.

feature-request

Most helpful comment

Amazon.Runtime.HttpClientFactory is now available in the .NET Standard SDK.
https://www.nuget.org/packages/AWSSDK.Core/3.3.29.4

Follow this pattern to customize how HttpClients are created:

public class MyHttpClientFactory : HttpClientFactory
{
    public override HttpClient CreateHttpClient(IClientConfig clientConfig)
    {
        // return a new instance of an HttpClient, caching is implemented by the SDK.
    }
}

You can also use HttpClientFactory to expose your own pool of cached HttpClients. Here's a rough example of how to implement it:

public class MyHttpClientFactory : HttpClientFactory
{
    public override HttpClient CreateHttpClient(IClientConfig clientConfig)
    {
        // return a cached client from your pool
    }
    public override bool UseSDKHttpClientCaching(IClientConfig clientConfig)
    {
        // return false to indicate that the SDK should not cache clients internally
        return false;
    }
    public override bool DisposeHttpClientsAfterUse(IClientConfig clientConfig)
    {
        // return false to indicate that the SDK shouldn't dispose httpClients because they're cached in your pool
        return false;
    }
    public override string GetConfigUniqueString(IClientConfig clientConfig)
    {
        // has no effect because UseSDKHttpClientCaching returns false
        return null;
    }
}

All 5 comments

We're working on this now.

There's one point I'd like to clarify - we're not using Microsoft's System.Net.Http.IHttpClientFactory. Our factory interface is Amazon.Runtime.IHttpClientFactory. It was an unfortunate coincidence that we named our interface the same as Microsoft's.

The name clash is unfortunate. But although they're not the same thing, I hope that you can use Amazon.Runtime.IHttpClientFactory to wire in Microsoft's System.Net.Http.IHttpClientFactory ?

@vellozzi Yeah I noticed that it's a name clash and it's a different type. That's fine, we can easily create a wrapper that delegates your interface to Microsoft's.

Amazon.Runtime.HttpClientFactory is now available in the .NET Standard SDK.
https://www.nuget.org/packages/AWSSDK.Core/3.3.29.4

Follow this pattern to customize how HttpClients are created:

public class MyHttpClientFactory : HttpClientFactory
{
    public override HttpClient CreateHttpClient(IClientConfig clientConfig)
    {
        // return a new instance of an HttpClient, caching is implemented by the SDK.
    }
}

You can also use HttpClientFactory to expose your own pool of cached HttpClients. Here's a rough example of how to implement it:

public class MyHttpClientFactory : HttpClientFactory
{
    public override HttpClient CreateHttpClient(IClientConfig clientConfig)
    {
        // return a cached client from your pool
    }
    public override bool UseSDKHttpClientCaching(IClientConfig clientConfig)
    {
        // return false to indicate that the SDK should not cache clients internally
        return false;
    }
    public override bool DisposeHttpClientsAfterUse(IClientConfig clientConfig)
    {
        // return false to indicate that the SDK shouldn't dispose httpClients because they're cached in your pool
        return false;
    }
    public override string GetConfigUniqueString(IClientConfig clientConfig)
    {
        // has no effect because UseSDKHttpClientCaching returns false
        return null;
    }
}
Was this page helpful?
0 / 5 - 0 ratings