In an attempt to launch both a function app and a gRPC server in the same process, I am spawning two builders, one in a non-blocking fashion in the background (fn_host.RunAsync()
) for the FunctionsWebApplication, and the other in a blocking fashion in the foreground (grpcHost.Run()
) for WebApplication (that hosts the gRPC server) to keep the parent process running.
I am aware that the background sub process runs the risk of failing silently, but this can be handled by external health checks such as liveness probes in production.
While this approach works, I would like to know if there are any side effects (especially those specific to .NET) of running a FunctionsWebApplication instance and WebApplication instance within the same .NET process.
IHost fn_host = new HostBuilder()
.ConfigureFunctionsWebApplication(builder =>
{
builder.Services.Configure<JsonSerializerOptions>(options =>
{
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
});
})
.ConfigureAppConfiguration((
hostContext,
configuration) =>
{
configuration.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", true, false)
.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", true, false)
.AddJsonFile("local.settings.json", true, false)
.AddEnvironmentVariables();
})
.ConfigureServices((hostContext, services) =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
IConfiguration configuration = hostContext.Configuration;
services
.AddBindings()
.AddConfig(configuration)
.AddAutoMapper(typeof(Program));
})
.Build();
fn_host.RunAsync();
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGrpc();
builder.WebHost.ConfigureKestrel(options =>
{
options.ListenLocalhost(5000, o => o.Protocols = HttpProtocols.Http2);
});
var grpcHost = builder.Build();
grpcHost.MapGrpcService<GreeterService>();
grpcHost.Run();