Class ServerServiceCollectionExtensions
- Namespace
- IceRpc.Extensions.DependencyInjection
- Assembly
- IceRpc.Extensions.DependencyInjection.dll
Provides extension methods for IServiceCollection to add a Server.
public static class ServerServiceCollectionExtensions
- Inheritance
-
ServerServiceCollectionExtensions
- Inherited Members
Methods
AddIceRpcServer(IServiceCollection)
Adds a Server to this service collection; you specify the server's options by injecting an IOptions<TOptions> of ServerOptions.
public static IServiceCollection AddIceRpcServer(this IServiceCollection services)
Parameters
services
IServiceCollectionThe service collection to add services to.
Returns
- IServiceCollection
The service collection.
Remarks
You need to set a least the dispatcher in the injected options.
AddIceRpcServer(IServiceCollection, IDispatcher)
Adds a Server with the specified dispatch pipeline to this service collection; you can specify the server's options by injecting an IOptions<TOptions> of ServerOptions.
public static IServiceCollection AddIceRpcServer(this IServiceCollection services, IDispatcher dispatcher)
Parameters
services
IServiceCollectionThe service collection to add services to.
dispatcher
IDispatcherThe dispatch pipeline.
Returns
- IServiceCollection
The service collection.
Examples
The following code adds a Server singleton to the service collection.
var router = new Router(); // the dispatch pipeline
IHostBuilder builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices(services => services.AddIceRpcServer(router));
The resulting singleton is a default server: it uses the default server address, the default multiplexed
transport (tcp) and null
for its authentication options (so no TLS). If you want to customize this
server, add an IOptions<TOptions> of ServerOptions to your DI container:
var router = new Router(); // the dispatch pipeline
IHostBuilder builder = Host.CreateDefaultBuilder(args);
builder.UseContentRoot(AppContext.BaseDirectory).ConfigureServices((hostContext, services) =>
{
services
.AddOptions<ServerOptions>()
// Read the server options from configuration.
.Bind(hostContext.Configuration.GetSection("Server"));
services.AddIceRpcServer(router);
});
You can also inject a server transport:
- an IDuplexServerTransport for the ice protocol
- an IMultiplexedServerTransport for the icerpc protocol
For example, you can add a QUIC server as follows:
var router = new Router(); // the dispatch pipeline
IHostBuilder builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices(services =>
// Inject an IMultiplexedServerTransport singleton implemented by QUIC.
services
.AddSingleton<IMultiplexedServerTransport>(provider => new QuicServerTransport())
.AddIceRpcServer(router));
If you want to customize the options of the default transport (tcp), you just need to inject an IOptions<TOptions> of TcpServerTransportOptions.
AddIceRpcServer(IServiceCollection, Action<IDispatcherBuilder>)
Adds a Server to this service collection and configures the dispatch pipeline of this server; you can specify the server's options by injecting an IOptions<TOptions> of ServerOptions.
public static IServiceCollection AddIceRpcServer(this IServiceCollection services, Action<IDispatcherBuilder> configure)
Parameters
services
IServiceCollectionThe service collection to add services to.
configure
Action<IDispatcherBuilder>The action to configure the dispatch pipeline using an IDispatcherBuilder.
Returns
- IServiceCollection
The service collection.
Examples
The following code builds a dispatch pipeline and adds a server with this dispatch pipeline to the service collection.
IHostBuilder hostBuilder = Host.CreateDefaultBuilder(args);
hostBuilder.ConfigureServices(services =>
services
.AddIceRpcServer(builder =>
// Configure the dispatch pipeline:
builder
.UseTelemetry()
.UseLogger()
.Map<IGreeterService>()));
See also AddIceRpcServer(IServiceCollection, IDispatcher).
Remarks
The dispatch pipeline built by this method is not registered in the DI container.
AddIceRpcServer(IServiceCollection, string)
Adds a Server to this service collection; you specify the server's options by injecting
an IOptionsMonitor<TOptions> of ServerOptions named optionsName
.
public static IServiceCollection AddIceRpcServer(this IServiceCollection services, string optionsName)
Parameters
services
IServiceCollectionThe service collection to add services to.
optionsName
stringThe name of the options instance.
Returns
- IServiceCollection
The service collection.
Remarks
You need to set a least the dispatcher in the injected options.
- See Also
AddIceRpcServer(IServiceCollection, string, IDispatcher)
Adds a Server with the specified dispatch pipeline to this service collection; you can
specify the server's options by injecting an IOptionsMonitor<TOptions> of ServerOptions
named optionsName
.
public static IServiceCollection AddIceRpcServer(this IServiceCollection services, string optionsName, IDispatcher dispatcher)
Parameters
services
IServiceCollectionThe service collection to add services to.
optionsName
stringThe name of the options instance.
dispatcher
IDispatcherThe dispatch pipeline of the server.
Returns
- IServiceCollection
The service collection.
Examples
A server application may need to host multiple Server instances, each with its own options. A typical example is when you want to accept requests from clients over both the icerpc protocol and the ice protocol. This overload allows you to add two (or more) server singletons, each with its own options:
var router = new Router(); // the dispatch pipeline
IHostBuilder builder = Host.CreateDefaultBuilder(args);
builder.UseContentRoot(AppContext.BaseDirectory).ConfigureServices((hostContext, services) =>
{
// The server options for the icerpc server
services
.AddOptions<ServerOptions>("IceRpcGreeter") // named option
.Bind(hostContext.Configuration.GetSection("IceRpcGreeter"));
// The server options for the ice server
services
.AddOptions<ServerOptions>("IceGreeter")
.Bind(hostContext.Configuration.GetSection("IceGreeter"));
// We pass the named server options to get the correct server options for each server.
services.AddIceRpcServer("IceRpcGreeter", router);
services.AddIceRpcServer("IceGreeter", router);
});
See also AddIceRpcServer(IServiceCollection, IDispatcher).
AddIceRpcServer(IServiceCollection, string, Action<IDispatcherBuilder>)
Adds a Server to this service collection and configures the dispatch pipeline of this
server; you can specify the server's options by injecting an IOptionsMonitor<TOptions> of
ServerOptions named optionsName
.
public static IServiceCollection AddIceRpcServer(this IServiceCollection services, string optionsName, Action<IDispatcherBuilder> configure)
Parameters
services
IServiceCollectionThe service collection to add services to.
optionsName
stringThe name of the options instance.
configure
Action<IDispatcherBuilder>The action to configure the dispatch pipeline using an IDispatcherBuilder.
Returns
- IServiceCollection
The service collection.
Remarks
The dispatch pipeline built by this method is not registered in the DI container.
- See Also