簡體   English   中英

如何在沒有日志記錄和配置文件的情況下運行 ASP.NET Core Kestrel 服務器?

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

我需要在現有的單片應用程序中注入一個 ASP.NET Core web API 服務器。 我的主要問題是關閉所有配置文件、環境配置、日志記錄等等。

我只需要一個帶有 MVC 路由的純代碼控制的 HTTP 服務器。

我怎樣才能做到這一點?

查看我的要點以獲得一個非常小的紅隼實例/asp.net web 應用程序。

要啟用 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."));
    };
  }
}

這也利用了入口點Main(string[] args)的新異步功能,將服務器啟動包裝在 try/catch 中,並注冊一個 catch-all not found 處理程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM