Skip to content

Commit

Permalink
fixed the problems of multiple server hosting with minimum api
Browse files Browse the repository at this point in the history
  • Loading branch information
kerryjiang committed Jan 11, 2025
1 parent 1474647 commit 8b8aff9
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/SuperSocket.Server/Host/HostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ public static MultipleServerHostBuilder AsMultipleServerHostBuilder(this IHostBu
return new MultipleServerHostBuilder(hostBuilder);
}

public static SuperSocketWebApplicationBuilder AsSuperSocketWebApplicationBuilder(this IHostApplicationBuilder hostApplicationBuilder, Action<MultipleServerHostBuilder> configureServerHostBuilder)
{
var applicationBuilder = new SuperSocketWebApplicationBuilder(hostApplicationBuilder);

var hostBuilder = new MultipleServerHostBuilder(applicationBuilder.Host);
configureServerHostBuilder(hostBuilder);
hostBuilder.AsMinimalApiHostBuilder().ConfigureHostBuilder();
return applicationBuilder;
}

public static IMinimalApiHostBuilder AsMinimalApiHostBuilder(this ISuperSocketHostBuilder hostBuilder)
{
return hostBuilder;
Expand Down
59 changes: 59 additions & 0 deletions src/SuperSocket.Server/Host/SuperSocketWebApplicationBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.Metrics;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace SuperSocket.Server.Host
{
public class SuperSocketWebApplicationBuilder : IHostApplicationBuilder
{
private readonly IHostApplicationBuilder _hostApplicationBuilder;

public IDictionary<object, object> Properties => _hostApplicationBuilder.Properties;

public IConfigurationManager Configuration => _hostApplicationBuilder.Configuration;

public IHostEnvironment Environment => _hostApplicationBuilder.Environment;

public ILoggingBuilder Logging => _hostApplicationBuilder.Logging;

public IMetricsBuilder Metrics => _hostApplicationBuilder.Metrics;

public IServiceCollection Services => _hostApplicationBuilder.Services;

internal SuperSocketWebApplicationBuilder(IHostApplicationBuilder hostApplicationBuilder)
{
_hostApplicationBuilder = hostApplicationBuilder;
}

public IHostBuilder Host
{
get
{
return _hostApplicationBuilder.GetType()
.GetProperty("Host", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
.GetValue(_hostApplicationBuilder) as IHostBuilder;
}
}

public IHost Build()
{
var host = _hostApplicationBuilder
.GetType()
.GetMethod(nameof(Build))
.Invoke(_hostApplicationBuilder, Array.Empty<object>()) as IHost;

host.Services.GetService<MultipleServerHostBuilder>()?.AdaptMultipleServerHost(host.Services);

return host;
}

void IHostApplicationBuilder.ConfigureContainer<TContainerBuilder>(IServiceProviderFactory<TContainerBuilder> factory, Action<TContainerBuilder> configure)
{
_hostApplicationBuilder.ConfigureContainer<TContainerBuilder>(factory, configure);
}
}
}
41 changes: 41 additions & 0 deletions test/SuperSocket.Tests/MainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand Down Expand Up @@ -474,6 +475,46 @@ public async Task TestMultipleHostStartup(Type hostConfiguratorType)
}
}

[Theory]
[InlineData(typeof(RegularHostConfigurator))]
[InlineData(typeof(SecureHostConfigurator))]
[InlineData(typeof(UdpHostConfigurator))]
[InlineData(typeof(KestralConnectionHostConfigurator))]
public async Task TestHostStartupMinimalApi(Type hostConfiguratorType)
{
var hostConfigurator = CreateObject<IHostConfigurator>(hostConfiguratorType);

var hostBuilder = WebApplication.CreateBuilder()
.AsSuperSocketWebApplicationBuilder(serverHostBuilder =>
serverHostBuilder
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.Sources.Clear();
config.AddJsonFile("Config/multiple_server.json", optional: false, reloadOnChange: true);
})
.AddServer<TextPackageInfo, LinePipelineFilter>(builder =>
{
hostConfigurator.Configure(builder);

builder
.ConfigureServerOptions((ctx, config) =>
{
return config.GetSection("TestServer1");
})
.UsePackageHandler(async (IAppSession s, TextPackageInfo p) =>
{
await s.SendAsync(Utf8Encoding.GetBytes("Hello World\r\n"));
});
})
);

using (var host = hostBuilder.Build())
{
await host.StartAsync();
await host.StopAsync();
}
}

[Fact]
[Trait("Category", "TestServiceProvider")]
public async Task TestServiceProvider()
Expand Down

0 comments on commit 8b8aff9

Please sign in to comment.