Table of Contents

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 IServiceCollection

The 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 IServiceCollection

The service collection to add services to.

dispatcher IDispatcher

The 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.UseContentRoot(AppContext.BaseDirectory).ConfigureServices((hostContext, services) =>
{
    // Load and register the server certificate as a singleton so it stays alive and gets
    // disposed.
    services.AddSingleton<X509Certificate2>(sp =>
        X509CertificateLoader.LoadPkcs12FromFile(
            Path.Combine(hostContext.HostingEnvironment.ContentRootPath, "server.p12"),
            password: null,
            keyStorageFlags: X509KeyStorageFlags.Exportable));

    // Configure the server authentication options using the server certificate.
    services
        .AddOptions<ServerOptions>()
        .Configure<X509Certificate2>((options, serverCertificate) =>
            options.ServerAuthenticationOptions = new SslServerAuthenticationOptions
            {
                ServerCertificateContext = SslStreamCertificateContext.Create(
                    serverCertificate,
                    additionalCertificates: null)
            });

    services.AddIceRpcServer(router);
});

The resulting singleton is a default server: it uses the default server address and the default multiplexed transport (QUIC). 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) =>
    {
        // Load the server certificate from the path specified in the "Certificate"
        // configuration section, and register it as a singleton so it stays alive
        // and gets disposed.
        services.AddSingleton<X509Certificate2>(sp =>
            X509CertificateLoader.LoadPkcs12FromFile(
                Path.Combine(
                    hostContext.HostingEnvironment.ContentRootPath,
                    hostContext.Configuration.GetValue<string>("Certificate:File")!),
                password: null,
                keyStorageFlags: X509KeyStorageFlags.Exportable));

        // Bind the server options to the configuration "Server" section, and add a
        // Configure callback to configure its authentication options.
        services
            .AddOptions<ServerOptions>()
            .Bind(hostContext.Configuration.GetSection("Server"))
            .Configure<X509Certificate2>((options, serverCertificate) =>
                options.ServerAuthenticationOptions = new SslServerAuthenticationOptions
                {
                    ServerCertificateContext = SslStreamCertificateContext.Create(
                        serverCertificate,
                        additionalCertificates: null)
                });

        services.AddIceRpcServer(router);
    });

You can also inject a server transport:

For example, you can add a Slic over TCP server as follows:

var router = new Router(); // the dispatch pipeline

IHostBuilder builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices(services =>
    // Inject an IMultiplexedServerTransport singleton implemented by Slic.
    services
        .AddSingleton<IMultiplexedServerTransport>(
            provider => new SlicServerTransport(
                provider.GetRequiredService<IDuplexServerTransport>()))
        .AddIceRpcServer(router));

If you want to customize the options of the default transport (QUIC), you just need to inject an IOptions<TOptions> of QuicServerTransportOptions.

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 IServiceCollection

The 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
    .UseContentRoot(AppContext.BaseDirectory)
    .ConfigureServices((hostContext, services) =>
    {
        // Load and register the server certificate as a singleton so it stays alive and
        // gets disposed.
        services.AddSingleton<X509Certificate2>(sp =>
            X509CertificateLoader.LoadPkcs12FromFile(
                Path.Combine(hostContext.HostingEnvironment.ContentRootPath, "server.p12"),
                password: null,
                keyStorageFlags: X509KeyStorageFlags.Exportable));

        // Configure the server authentication options using the server certificate.
        services
            .AddOptions<ServerOptions>()
            .Configure<X509Certificate2>((options, serverCertificate) =>
                options.ServerAuthenticationOptions = new SslServerAuthenticationOptions
                {
                    ServerCertificateContext = SslStreamCertificateContext.Create(
                        serverCertificate,
                        additionalCertificates: null)
                });

        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 IServiceCollection

The service collection to add services to.

optionsName string

The name of the options instance. Each Server registered in services must use a unique options name.

Returns

IServiceCollection

The service collection.

Remarks

You need to set at 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 IServiceCollection

The service collection to add services to.

optionsName string

The name of the options instance. Each Server registered in services must use a unique options name.

dispatcher IDispatcher

The 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) =>
    {
        // Load the server certificate from the path specified in the "Certificate"
        // configuration section, and register it as a singleton so it stays alive and
        // gets disposed.
        services.AddSingleton<X509Certificate2>(sp =>
            X509CertificateLoader.LoadPkcs12FromFile(
                Path.Combine(
                    hostContext.HostingEnvironment.ContentRootPath,
                    hostContext.Configuration.GetValue<string>("Certificate:File")!),
                password: null,
                keyStorageFlags: X509KeyStorageFlags.Exportable));

        // The server options for the icerpc server
        services
            .AddOptions<ServerOptions>("IceRpcGreeter") // named option
            .Bind(hostContext.Configuration.GetSection("IceRpcGreeter"))
            .Configure<X509Certificate2>((options, serverCertificate) =>
                options.ServerAuthenticationOptions = new SslServerAuthenticationOptions
                {
                    ServerCertificateContext = SslStreamCertificateContext.Create(
                        serverCertificate,
                        additionalCertificates: null)
                });

        // The server options for the ice server
        services
            .AddOptions<ServerOptions>("IceGreeter")
            .Bind(hostContext.Configuration.GetSection("IceGreeter"))
            .Configure<X509Certificate2>((options, serverCertificate) =>
                options.ServerAuthenticationOptions = new SslServerAuthenticationOptions
                {
                    ServerCertificateContext = SslStreamCertificateContext.Create(
                        serverCertificate,
                        additionalCertificates: null)
                });

        // 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 IServiceCollection

The service collection to add services to.

optionsName string

The name of the options instance. Each Server registered in services must use a unique options name.

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