为什么 DNX 运行代码两次

本文关键字:两次 DNX 运行 代码 为什么 | 更新日期: 2023-09-27 18:36:27

我想将我的框架移植到新的 .net 核心,所以我创建了一个空应用程序,添加了一个静态成员来感受行为,每个请求增加一个 int,但它运行两次,一次用于请求,一次在请求之后,所以而不是得到 1,2,3,4...我得到 1,3,5,7...

public class Startup
{
    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {      
    }
    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app)
    {
        app.UseIISPlatformHandler();
        app.Run(async (context) =>
        {
            state++;
            await  context.Response.WriteAsync(state.ToString()); 
        }); 
    }
    public static int state;
    // Entry point for the application.
    public static void Main(string[] args) => Microsoft.AspNet.Hosting.WebApplication.Run<Startup>(args);
}

为什么 DNX 运行代码两次

猜测一下:看看例如Chrome的网络标签(从您的结果来看,您可能已经在Chrome中进行测试)。您可能会看到您实际上有两个请求:您期望的请求 - 加上一个用于favicon.ico。您的应用程序正在同时满足这两种需求。