Search code examples
c#.netdnx

Why is DNX running code twice


I'm wanting to port my framework to the new .net core, so I created an empty app, added a static member to get a feel for the behavior, increment an int per request, but it runs twice, once for the request and once right after the request, so instead of getting 1,2,3,4... i get 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);
}

Solution

  • At a guess: Have a look in the Network tab of e.g. Chrome (judging from your result, you're likely already testing in Chrome). You'll probably see that you actually do have two requests: The one you expect - plus one for favicon.ico. And your app is serving both of those.