简体   繁体   中英

How to run ASP.NET Core Kestrel server without logging and config files?

I need to inject an ASP.NET Core web API server in the existing monolithic application. The main problem for me is to turn off all config files, environment config, logging and so on stuff.

I need only a pure code-controlled HTTP-server with MVC routing.

How can I achieve this?

Checkout my gist for getting a very minimal kestrel instance/asp.net web app going.

To enable MVC, you change it as follows:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace WebApp 
{
  public class Program
  {
    public static async Task<int> Main(string[] args) {
      try {
        var host = new WebHostBuilder()
          .UseKestrel()
          .UseUrls("http://localhost:5000/;https://localhost:5001")
          .ConfigureServices(_configureServices)
          .Configure(_configure)
          .Build();

        await host.RunAsync();

        return 0;
      }
      catch {
        return -1;
      }
    }

    static Action<IServiceCollection> _configureServices = (services) => {
         services.AddControllersWithViews();
    };

    static Action<IApplicationBuilder> _configure = app => {
      app.UseRouting();

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

      app.Run((ctx) => ctx.Response.WriteAsync("Page not found."));
    };
  }
}

This also takes advantage of the new async capabilities of the entry point Main(string[] args) , wraps the server startup in a try/catch, and registers a catch-all not found handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM