Giter Club home page Giter Club logo

Comments (4)

aL3891 avatar aL3891 commented on July 2, 2024 1

Cool np, thanks for the info!
I ended up with this as a poc, perhaps it will be of use to someone :)

public class Program
{
    public static Task Main(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
        .UseKestrel()
        .UseMultiServer<KestrelServer, TestServer>()
        .UseStartup<Startup>()
        .Build()
        .RunAsync();
}

public class MultiServer<TServer1, TServer2> : MultiServer, IServer where TServer1 : IServer where TServer2 : IServer
{
    public MultiServer(TServer1 server1, TServer2 server2) : base(new IServer[] { server1, server2 }) { }
}

public class MultiServer : IServer
{
    public IServer[] Servers { get; }
    public IFeatureCollection Features { get; set; } = new FeatureCollection();

    public MultiServer(IServer[] servers)
    {
        Servers = servers;
        foreach (var f in servers.SelectMany(s => s.Features))
            Features[f.Key] = f.Value;
    }

    public Task StartAsync<TContext>(IHttpApplication<TContext> application, CancellationToken cancellationToken) =>
        Task.WhenAll(Servers.Select(s => s.StartAsync(application, cancellationToken)));

    public Task StopAsync(CancellationToken cancellationToken) =>
        Task.WhenAll(Servers.Select(s => s.StopAsync(cancellationToken)));

    public void Dispose()
    {
        foreach (var s in Servers)
            s.Dispose();
    }
}

public static class ServerExtensions
{
    public static IWebHostBuilder UseMultiServer<TServer1, TServer2>(this IWebHostBuilder hostBuilder) where TServer1 : class, IServer where TServer2 : class, IServer =>
        hostBuilder.ConfigureServices(services =>
        {
            services.AddSingleton<TServer1, TServer1>();
            services.AddSingleton<TServer2, TServer2>();
            services.AddSingleton<IServer, MultiServer<TServer1, TServer2>>();
        });
}

public class TestServer : IServer
{
    public Task StartAsync<TContext>(IHttpApplication<TContext> application, CancellationToken cancellationToken) => Task.CompletedTask;
    public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
    public IFeatureCollection Features { get; } = new FeatureCollection();
    public void Dispose() { }
}

from hosting.

Tratcher avatar Tratcher commented on July 2, 2024

You're right on all counts.

Don't worry about using UseServer, it takes an instance. Directly registering with DI will make it easier for you to resolve services.

from hosting.

aL3891 avatar aL3891 commented on July 2, 2024

Great :)
Is there anything else to think about with the move to generic host in .net core 3? Following some prs and digging though the code I've gathered that a web app is just a HostedService in the new model(?), is it still the case that IServer is a Singleton or is it scoped?

(Or actually, maybe you can already register IServer as scoped today and just create a scope for each server? I don't remember if you can override registrations in a child scope)

from hosting.

Tratcher avatar Tratcher commented on July 2, 2024

No, IServer is still a singleton in the new generic host scenario.

from hosting.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.