~ruther/NosSmooth.Comms

ref: ecfc8cd7dc9143e9550044f3f5971bf8631f51b7 NosSmooth.Comms/src/Core/NosSmooth.Comms.NamedPipes/Extensions/ServiceCollectionExtensions.cs -rw-r--r-- 1.9 KiB
ecfc8cd7 — Rutherther feat: add local injectable and injector to allow making connections to nostale processes 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//
//  ServiceCollectionExtensions.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.IO.Pipes;
using Microsoft.Extensions.DependencyInjection;
using NosSmooth.Comms.Data;

namespace NosSmooth.Comms.NamedPipes.Extensions;

/// <summary>
/// Extension methods for <see cref="IServiceCollection"/>.
/// </summary>
public static class ServiceCollectionExtensions
{
    /// <summary>
    /// Adds a named pipe client as a <see cref="IClient"/>.
    /// </summary>
    /// <param name="serviceCollection">The service collection.</param>
    /// <param name="pipeNameResolver">A function for resolving the name of the pipe using service provider.</param>
    /// <returns>The same service collection.</returns>
    public static IServiceCollection AddNamedPipeClient
        (this IServiceCollection serviceCollection, Func<IServiceProvider, string> pipeNameResolver)
        => serviceCollection
            .AddSingleton<NamedPipeClient>(p => new NamedPipeClient(pipeNameResolver(p)))
            .AddSingleton<IClient>(p => p.GetRequiredService<NamedPipeClient>());

    /// <summary>
    /// Adds a named pipe server as a <see cref="IServer"/>.
    /// </summary>
    /// <param name="serviceCollection">The service collection.</param>
    /// <param name="pipeNameResolver">A function for resolving the name of the pipe using service provider.</param>
    /// <returns>The same service collection.</returns>
    public static IServiceCollection AddNamedPipeServer
        (this IServiceCollection serviceCollection, Func<IServiceProvider, string> pipeNameResolver)
        => serviceCollection
            .AddSingleton<NamedPipeServer>(p => new NamedPipeServer(pipeNameResolver(p)))
            .AddSingleton<IServer>(p => p.GetRequiredService<NamedPipeServer>());
}
Do not follow this link