jeudi 13 mai 2021

Error executing .NET Core Web API as a service in Ubuntu with Nginx

I'm having trouble when running my web core API in Ubuntu with Nginx as a service. Manually is working fine, I don't know how to solve the error it throws. I'm pretty new to Web API and if this could help other people I would need your help.

When I run the API manually in my Nginx server and works fine:

ubuntu@vps-1806252c:/var/www/html/communityforamongus$ dotnet community-for-among-us.dll
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: https://127.0.0.1:2000
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /var/www/html/communityforamongus

Then when I start it as a service:

ubuntu@vps-1806252c:~$ sudo service kestrel-communityforamongus start
ubuntu@vps-1806252c:~$ sudo service kestrel-communityforamongus status
● kestrel-communityforamongus.service - MyService in .NET
     Loaded: loaded (/etc/systemd/system/kestrel-communityforamongus.service; enabled; vendor preset: enabled)
     Active: failed (Result: core-dump) since Thu 2021-05-13 14:54:40 UTC; 914ms ago
    Process: 7276 ExecStart=/usr/bin/dotnet /var/www/html/communityforamongus/community-for-among-us.dll (code=dumped, signal=ABRT)
   Main PID: 7276 (code=dumped, signal=ABRT)

May 13 14:54:40 vps-1806252c dotnet[7276]:    at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.BindAsync(CancellationToken cancellationToken)
May 13 14:54:40 vps-1806252c dotnet[7276]:    at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)
May 13 14:54:40 vps-1806252c dotnet[7276]:    at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
May 13 14:54:40 vps-1806252c dotnet[7276]:    at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
May 13 14:54:40 vps-1806252c dotnet[7276]:    at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
May 13 14:54:40 vps-1806252c dotnet[7276]:    at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
May 13 14:54:40 vps-1806252c dotnet[7276]:    at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
May 13 14:54:40 vps-1806252c dotnet[7276]:    at community_for_among_us.Program.Main(String[] args) in C:\Users\alexm\Documents\Projects\.NET\community-for-among-us-api\community-for-among-us\Program.cs:line 17
May 13 14:54:40 vps-1806252c systemd[1]: kestrel-communityforamongus.service: Main process exited, code=dumped, status=6/ABRT
May 13 14:54:40 vps-1806252c systemd[1]: kestrel-communityforamongus.service: Failed with result 'core-dump'.
ubuntu@vps-1806252c:~$

I leave here the files I think that are important if somebody sees something is wrong:

Nginx configuration file location for the API:

    location / {
            proxy_pass         https://127.0.0.1:2000;
            proxy_http_version 1.1;
            proxy_set_header   Upgrade $http_upgrade;
            proxy_set_header   Connection keep-alive;
            proxy_set_header   Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto $scheme;
    }

The file that apparently is crashing when starting it as a service:

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseContentRoot(Directory.GetCurrentDirectory());
                webBuilder.UseUrls("https://127.0.0.1:2000");
                webBuilder.UseStartup<Startup>();
            });
}

And finally the Startup.cs:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {

        //services.AddControllers();
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "community_for_among_us", Version = "v1" });
        });
        services.AddEntityFrameworkNpgsql().AddDbContext<ApplicationContext>(options =>
        options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));
        //services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Latest);
        services.AddMvc();
        services.AddScoped<IAppInformationInterface, AppInformationService>();

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseSwagger();
            app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "community_for_among_us v1"));
        }

        app.UseForwardedHeaders(new ForwardedHeadersOptions
        {
            ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
        });

        app.UseAuthentication();

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

If somebody can fix the error please answer and we can help me and the community if they are involved in a situation like this. Thanks!




Aucun commentaire:

Enregistrer un commentaire