c# - 为什么 DNX 运行代码两次

标签 c# .net dnx

我想将我的框架移植到新的 .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);
}

最佳答案

猜测:看看网络选项卡,例如Chrome(从您的结果来看,您可能已经在 Chrome 中进行了测试)。您可能会发现您实际上有两个请求:您期望的一个 - 加上一个用于 favicon.ico 的请求。而您的应用正在为这两者提供服务。

关于c# - 为什么 DNX 运行代码两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35639858/

相关文章:

asp.net-core - 在 Nano Server 上运行 ASP.NET 5 会抛出 "Unable to load DLL ' kernel3 2'"

asp.net-core-mvc - 如何考虑请求和响应范围 header 来传输视频或文件?

c# - 如何使用 unity + UnityAutoRegistration 进行开放式通用装饰器链接

c# - 使用 C# 解析 .msg 文件

.net - 使用什么来代替 Visual Studio for Applications?

c# - 在 Visual Studio 中解决 DisconnectedContext

读取注册表项时出现 C# NullReferenceException

c# - 自动化 EF 迁移 "Update-Database -Script"

c# - 基于 Empty XML 示例合并丢失的 XML 记录

c# - Attribute.IsDefined 在 DNX Core 5.0 中去了哪里?