Class ConnectionCacheServiceCollectionExtensions
- Namespace
- IceRpc.Extensions.DependencyInjection
- Assembly
- IceRpc.Extensions.DependencyInjection.dll
Provides an extension method for IServiceCollection to add a connection cache.
public static class ConnectionCacheServiceCollectionExtensions
- Inheritance
-
ConnectionCacheServiceCollectionExtensions
- Inherited Members
Methods
AddIceRpcConnectionCache(IServiceCollection)
Adds a ConnectionCache and an IInvoker to this service collection.
public static IServiceCollection AddIceRpcConnectionCache(this IServiceCollection services)
Parameters
servicesIServiceCollectionThe service collection to add services to.
Returns
- IServiceCollection
The service collection.
Examples
The following code adds a ConnectionCache singleton to the service collection.
IHostBuilder builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices(services => services.AddIceRpcConnectionCache());
The resulting singleton is a default connection cache. If you want to customize this connection cache, add an IOptions<TOptions> of ConnectionCacheOptions to your DI container:
IHostBuilder builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices(services =>
{
services
.AddOptions<ConnectionCacheOptions>()
.Configure(options =>
options.ConnectTimeout = TimeSpan.FromSeconds(30));
// options.ClientAuthenticationOptions remains null: this cache uses the system
// Trusted Root CAs to validate the server certificates when establishing secure
// QUIC connections.
services.AddIceRpcConnectionCache();
});
You can also inject a client transport:
- an IDuplexClientTransport for the ice protocol
- an IMultiplexedClientTransport for the icerpc protocol
The following example shows a connection cache that uses Slic over TCP for icerpc connections and keeps the default duplex transport (TCP) for ice connections.
IHostBuilder builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices(services =>
services
// The IMultiplexedClientTransport singleton is implemented by Slic.
.AddSingleton<IMultiplexedClientTransport>(
provider => new SlicClientTransport(
provider.GetRequiredService<IDuplexClientTransport>()))
.AddIceRpcConnectionCache());
If you want to customize the options of the default multiplexed transport (QUIC), you just need to inject an IOptions<TOptions> of QuicClientTransportOptions.
Remarks
This method uses the connection cache options provided by the IOptions<TOptions> of ConnectionCacheOptions.