The Remoting SDK documentation includes a section on using Host.CreateDefaultBuilder() to configure an ApplicationServer. However, Microsoft has since moved on to using the newer Host.CreateApplicationBuilder() structure in its Visual Studio templates. Is it possible to configure an ApplicationServer using this newer technique?
Hi,
You can add the latest Microsoft.Extensions.Hosting
package to your project and use CreateApplicationBuilder
instead of CreateDefaultBuilder
method.
Thank you for the reply, but I was really wanting to know how we would configure an ApplicationServer and a NetworkServer when the extension methods provided with the SDK are for IHostBuilder.
Specifically, what would replace the following code:
return Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
})
.UseApplicationServer(configuration =>
{
configuration.ApplicationName = "ROServer1";
configuration.RodlNamespace = "ROServer1";
})
.UseNetworkServer(server =>
{
server.Port = 8090;
})
.UseConsoleLifetime();
Thank you.
Hi,
you can use something like
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using RemObjects.SDK.Extensions;
using RemObjects.SDK.Server;
namespace ROServer1
{
internal static class Program
{
public static void Main(string[] args)
{
// Please refer to https://docs.remotingsdk.com/Servers/Technologies/ApplicationHost/
// for more details
var host = CreateHostBuilder(args)
.Build();
host.Run();
}
public static HostApplicationBuilder CreateHostBuilder(string[] args)
{
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
// Configure services
// builder.Services....
builder.UseApplicationServer(configuration =>
{
configuration.ApplicationName = "ROServer1";
configuration.RodlNamespace = "ROServer1";
})
.UseNetworkServer(server =>
{
server.Port = 8090;
});
return builder;
}
}
sealed class ServiceWorker : BackgroundService
{
private readonly IAppServerEngine _engine;
public ServiceWorker(IAppServerEngine engine)
{
this._engine = engine;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
this._engine.Initialize();
this._engine.Start();
try
{
while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(1000, stoppingToken);
}
}
finally
{
this._engine.Stop();
}
}
}
// Extension methods used to configure RO/DA server
static class HostBuilderExtensions
{
public static HostApplicationBuilder UseApplicationServer(this HostApplicationBuilder builder, Action<IAppServerConfiguration> configureDelegate)
{
return HostBuilderExtensions.UseApplicationServer<AppServerEngine>(builder, configureDelegate);
}
public static HostApplicationBuilder UseApplicationServer<TAppServerEngine>(this HostApplicationBuilder builder, Action<IAppServerConfiguration> configureDelegate)
where TAppServerEngine : class, IAppServerEngine
{
var config = HostExtensions.GetAppServerConfiguration(configureDelegate);
// Register worker service
// TODO Make this configurable
builder.Services.AddHostedService<ServiceWorker>();
// Register base server infrastructure
builder.Services.AddInfrastructureServices<TAppServerEngine>(config);
// Register Remoting SDK services
builder.Services.AddApplicationServices();
return builder;
}
public static HostApplicationBuilder UseNetworkServer(this HostApplicationBuilder builder, Action<INetworkServer> configureDelegate)
{
return builder.UseNetworkServer<NetworkServer>(configureDelegate);
}
public static HostApplicationBuilder UseNetworkServer<TNetworkServer>(this HostApplicationBuilder builder, Action<INetworkServer> configureDelegate)
where TNetworkServer : class, INetworkServer
{
if (configureDelegate is null)
{
throw new ArgumentNullException(nameof(configureDelegate));
}
builder.Services.AddNetworkServerConfiguration(configureDelegate);
return builder;
}
}
}
Thank you, that works well.
Are there any plans to integrate this into RemObjects.SDK.Extensions?
Thanks again!
Logged as bugs://D19507.
1 Like