Class ClientConnectionServiceCollectionExtensions
- Namespace
- IceRpc.Extensions.DependencyInjection
- Assembly
- IceRpc.Extensions.DependencyInjection.dll
Provides extension methods for IServiceCollection to add a client connection.
public static class ClientConnectionServiceCollectionExtensions
- Inheritance
-
ClientConnectionServiceCollectionExtensions
- Inherited Members
Methods
AddIceRpcClientConnection(IServiceCollection)
Adds a ClientConnection singleton to this service collection; this singleton is also registered as the IInvoker singleton.
public static IServiceCollection AddIceRpcClientConnection(this IServiceCollection services)
Parameters
servicesIServiceCollectionThe service collection to add services to.
Returns
- IServiceCollection
The service collection.
Examples
The following code adds a ClientConnection singleton to the service collection.
IHostBuilder builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices(services =>
{
services
.AddOptions<ClientConnectionOptions>()
// We need to set at least ServerAddress in the options.
.Configure(options =>
options.ServerAddress = new ServerAddress(new Uri("icerpc://localhost")));
// options.ClientAuthenticationOptions remains null: this client connection uses
// the system Trusted Root CAs to validate the server certificate when establishing
// the underlying secure QUIC connection.
services.AddIceRpcClientConnection();
});
You can also inject a client transport:
- an IDuplexClientTransport for the ice protocol
- an IMultiplexedClientTransport for the icerpc protocol
For example, you can add a Slic over TCP client connection as follows:
IHostBuilder builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices(services =>
{
services
.AddOptions<ClientConnectionOptions>()
// Since options.ClientAuthenticationOptions is null, we use plain TCP (no TLS).
.Configure(options =>
options.ServerAddress = new ServerAddress(new Uri("icerpc://localhost")));
services
// The IMultiplexedClientTransport singleton is implemented by Slic.
.AddSingleton<IMultiplexedClientTransport>(
provider => new SlicClientTransport(
provider.GetRequiredService<IDuplexClientTransport>()))
.AddIceRpcClientConnection();
});
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 client connection options provided by the IOptions<TOptions> of ClientConnectionOptions. If your application connects to multiple servers, call AddIceRpcConnectionCache(IServiceCollection) instead.
AddIceRpcClientConnection(IServiceCollection, ServerAddress)
Adds a ClientConnection singleton that connects to the specified server address to this service collection; this singleton is also registered as the IInvoker singleton.
public static IServiceCollection AddIceRpcClientConnection(this IServiceCollection services, ServerAddress serverAddress)
Parameters
servicesIServiceCollectionThe service collection to add services to.
serverAddressServerAddressThe server address of the client connection.
Returns
- IServiceCollection
The service collection.
Remarks
This method sets ServerAddress in the client connection options provided by the IOptions<TOptions> of ClientConnectionOptions; the client connection uses all the other options from these injected options. If your application connects to multiple servers, call AddIceRpcConnectionCache(IServiceCollection) instead.
- See Also
AddIceRpcClientConnection(IServiceCollection, Uri)
Adds a ClientConnection singleton that connects to the specified server address URI to this service collection; this singleton is also registered as the IInvoker singleton.
public static IServiceCollection AddIceRpcClientConnection(this IServiceCollection services, Uri serverAddressUri)
Parameters
servicesIServiceCollectionThe service collection to add services to.
serverAddressUriUriThe server address URI of the client connection.
Returns
- IServiceCollection
The service collection.
Remarks
If your application connects to multiple servers, call AddIceRpcConnectionCache(IServiceCollection) instead.
- See Also