Processing when startup fails(Asp.net Grpc Service)

Hiromu Matsubara (松原 拡) 21 Reputation points
2022-03-24T07:26:39.683+00:00

<Problem>
1 Create a project with the C # Asp.net grpc service template.
2 Modify Program.cs as follows.
Register the function in app.Lifetime.ApplicationStarted.Register for the process you want to execute after starting the server.
Program.cs

using Asp.net.GrpcService1;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGrpc();

var app = InitHelper.InitApp(builder);

app.Run();

InitHelper.cs
using Asp.net.GrpcService1.Services;

namespace Asp.net.GrpcService1
{
    public class InitHelper
    {
        public static WebApplication InitApp(WebApplicationBuilder builder)
        {
            var app = builder.Build();

            app.Lifetime.ApplicationStarted.Register(
                () =Console.WriteLine("started!")
                );

            // Configure the HTTP request pipeline.
            app.MapGrpcService<GreeterService>();
            app.MapGet("/", () ="Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
            return app;
        }
    }
}

3 Run the project.
4 If the port of the server to be started is already used by another application, an exception will occur in app.Run ().
Exception) System.IO.IOException:'Failed to bind to address http: // localhost: xxxx.'

<What you want to hear>
How should I code to execute the process registered in app.Lifetime.ApplicationStarted.Register even if app.Run () fails?
However, I don't want to enclose app.Run () in Program.cs with try catch.

×
try {
app.Run ();
} catch {
// Console.WriteLine("started!")
}

Because InitHelper is a common module and InitHelper is referenced from multiple Program.cs, I don't want to change Program.cs.
Please let me know if you have an alternative.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,236 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,307 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,369 questions
0 comments No comments
{count} votes

Accepted answer
  1. Brando Zhang-MSFT 2,966 Reputation points Microsoft Vendor
    2022-03-25T05:33:43.723+00:00

    Hi @Hiromu Matsubara (松原 拡) ,

    How should I code to execute the process registered in app.Lifetime.ApplicationStarted.Register even if app.Run () fails?

    Short answer: It's impossible.

    According to the sources codes, you could find, when the webhost call the startasync method, it will use the Server.StartAsync method to start the server.

    During the Server.StartAsync method, it will firstly check the server support http, address is ok and port is OK a lot of things.

    After checking these things, then it will fired the _applicationLifetime?.NotifyStarted(); method. So the exception is fired before the webhost call the `_applicationLifetime?.NotifyStarted() method.


0 additional answers

Sort by: Most helpful