简体   繁体   English

如何在没有日志记录和配置文件的情况下运行 ASP.NET Core Kestrel 服务器?

[英]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.我需要在现有的单片应用程序中注入一个 ASP.NET Core web API 服务器。 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.我只需要一个带有 MVC 路由的纯代码控制的 HTTP 服务器。

How can I achieve this?我怎样才能做到这一点?

Checkout my gist for getting a very minimal kestrel instance/asp.net web app going.查看我的要点以获得一个非常小的红隼实例/asp.net web 应用程序。

To enable MVC, you change it as follows:要启用 MVC,请将其更改如下:

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.这也利用了入口点Main(string[] args)的新异步功能,将服务器启动包装在 try/catch 中,并注册一个 catch-all not found 处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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