Skip to content

Commit

Permalink
Fix README location
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Gertenbach committed Nov 20, 2024
1 parent ef96c9d commit 8908ded
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 196 deletions.
38 changes: 31 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# Ringleader
Ringleader includes extensions, handler builder filters, and interfaces that extend the `DefaultHttpClientFactory` implementation to make customizing primary handler and cookie behavior for typed/named HTTP clients easier without losing the pooling and handler pipeline benefits of `IHttpClientFactory`

Expand Down Expand Up @@ -29,17 +30,20 @@ In order to enable the supplied context to provision a handler, a second interfa
```csharp
public interface IContextualHttpClientFactory
{
TClient CreateClient<TClient>(string handlerContext);
TClient CreateClient<TClient>(string handlerContext) where TClient : class;
HttpClient CreateClient(string clientName, string handlerContext);
}

public interface IPrimaryHandlerFactory
{
HttpMessageHandler CreateHandler(string clientName, string handlerContext);
// TypedClientSignature is a convenience wrapper implicitly convertable to and from a string
HttpMessageHandler CreateHandler(TypedClientSignature clientName, string handlerContext);
}
```

In order to register the Ringleader HttpClientFactory interfaces, use the extensions during startup in addition to your normal use of `AddHttpClient()` to set up named or typed clients. You may register the primary handler factory as a singleton implementation, or alternatively supply a function instead that optionally returns a customized handler.
In order to register the Ringleader HttpClientFactory interfaces, use the extensions during startup in addition to your normal use of `AddHttpClient()` to set up named or typed clients. You may register the primary handler factory as a singleton implementation for more advanced logic that requires additional dependency injection, or alternatively supply a function that optionally returns a customized handler directly at registration.

> **New in 3.x**: A convenience type called `TypedClientSignature` wraps the `client` parameter to enable consistent conversions and comparisons with the string format used by `DefaultHttpClientFactory`. This type supports implicit conversion to and from a normal string, but allows explicit matching with `client.IsTypedClient<T>()`
```csharp
using System.Net.Http;
Expand All @@ -50,25 +54,45 @@ builder.Services.AddHttpClient<ExampleTypedClient>();

builder.Services.AddContextualHttpClientFactory((client, context) =>
{
if (client == typeof(ExampleTypedClient).Name)
// This replaces 2.x: if (typeof(ExampleTypedClient).Name == client)
if (client.IsTypedClient<ExampleTypedClient>())
{
var handler = new SocketsHttpHandler();
if (context == "certificate-one")
{
// your customizations here
// Your handler customization here
handler.SslOptions = new System.Net.Security.SslClientAuthenticationOptions()
{
ClientCertificates = new X509Certificate2Collection()
};
}
return handler;
}

// The default implementation will be used if you return null
return null;
});
//...
```

#### New in 3.x: Typed clients with interfaces
Previously, Ringleader was not able to correctly resolve contextual typed clients that were registered and resolved against an interface. Due to the way that `DefaultHttpClientFactory` resolves interface-registered typed client resolution from the service provider, interface-registered typed clients are now supported by Ringleader but require an explicit addition during services registration.

```csharp
using System.Net.Http;

// Program.cs services registration ...
builder.Services.AddHttpClient<IExampleTypedClient, ExampleTypedClient>();

builder.Services.AddContextualHttpClientFactory<MyPrimaryHandlerFactory>()
.WithTypedClientImplementation<IExampleTypedClient, ExampleTypedClient>();

//...
// This now works
var client = _clientFactory.CreateClient<IExampleTypedClient>(context);
```

### Using `IContextualHttpClientFactory` in your application

Inject `IContextualHttpClientFactory` into your controllers and classes. Named and typed clients generated by the factory will have the delegating handler pipeline and policies in place as if they were fetched normally, but handlers will be partitioned by the context you supply and customized based on the primary handler factory behavior you registered.
Expand Down Expand Up @@ -149,4 +173,4 @@ It will (probably) use normal cookie behavior instead. The opt-in customizes ret
Yes. The handler builder filter for the cookies component has been designed to work within the contraints of the Ringleader `IHttpClientFactory` extensions regarding handler builder filter behavior.

#### Couldn't I just use something like Flurl?
You bet! These extensions were designed to enable better control over cookies within the .NET `HttpClient` and `IHttpClientFactory` ecosystem, should you choose (or need) to use them.
You bet! These extensions were designed to enable better control over cookies within the .NET `HttpClient` and `IHttpClientFactory` ecosystem, should you choose (or need) to use them.
6 changes: 6 additions & 0 deletions Ringleader.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RingleaderTests", "Ringlead
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebExample", "WebExample\WebExample.csproj", "{219EDA92-D634-40C0-A71A-7B49275FF086}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{95D2B26C-D658-4663-9121-13604107D090}"
ProjectSection(SolutionItems) = preProject
LICENSE = LICENSE
README.md = README.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
175 changes: 0 additions & 175 deletions Ringleader/README.md

This file was deleted.

14 changes: 0 additions & 14 deletions Ringleader/Ringleader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,4 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<None Include="..\LICENSE">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<None Include="..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

</Project>

0 comments on commit 8908ded

Please sign in to comment.