Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 6, HomeControler Index() called twice

Tags:

asp.net-mvc

Just updated to MVC 6 RC 1 and created a new MVC app.

Found that Index action method is called twice.

Index() in HomeControler:

public string Index()
{
    System.Diagnostics.Debug.WriteLine("############################");
    return string.Empty;
}

I removed everything from Startup.cs Configure except the default routing.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}

The global layout is removed. _ViewStart.cshtml

@{
   // Layout = "_Layout";
}

The output in the browser is empty. No scripts.

Console output:

'dnx.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\Miroslav\.dnx\packages\Microsoft.Extensions.Localization.Abstractions\1.0.0-rc1-final\lib\net451\Microsoft.Extensions.Localization.Abstractions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'dnx.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\Miroslav\.dnx\packages\Microsoft.VisualStudio.Web.BrowserLink.Loader\14.0.0-rc1-final\lib\dnx451\Microsoft.VisualStudio.Web.BrowserLink.Loader.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'dnx.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\Miroslav\.dnx\packages\Microsoft.AspNet.WebUtilities\1.0.0-rc1-final\lib\net451\Microsoft.AspNet.WebUtilities.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
############################
############################
'dnx.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\Miroslav\.dnx\packages\Microsoft.AspNet.Http.Extensions\1.0.0-rc1-final\lib\net451\Microsoft.AspNet.Http.Extensions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The thread 0x70a4 has exited with code 0 (0x0).

Call Stack:

MvcTest1!MvcTest1.Controllers.HomeController.Index() Line 15    C#
[Native to Managed Transition]  
[Managed to Native Transition]  
Microsoft.AspNet.Mvc.Core.dll!Microsoft.AspNet.Mvc.Controllers.ControllerActionExecutor.ExecuteAsync(System.Reflection.MethodInfo actionMethodInfo, object instance, object[] orderedActionArguments)   Unknown
Microsoft.AspNet.Mvc.Core.dll!Microsoft.AspNet.Mvc.Controllers.ControllerActionInvoker.InvokeActionAsync(Microsoft.AspNet.Mvc.Filters.ActionExecutingContext actionExecutingContext)    Unknown
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder<System.__Canon>.Start<Microsoft.AspNet.Mvc.Controllers.ControllerActionInvoker.<InvokeActionAsync>d__6>(ref Microsoft.AspNet.Mvc.Controllers.ControllerActionInvoker.<InvokeActionAsync>d__6 stateMachine)  Unknown
Microsoft.AspNet.Mvc.Core.dll!Microsoft.AspNet.Mvc.Controllers.ControllerActionInvoker.InvokeActionAsync(Microsoft.AspNet.Mvc.Filters.ActionExecutingContext actionExecutingContext)    Unknown
Microsoft.AspNet.Mvc.Core.dll!Microsoft.AspNet.Mvc.Controllers.FilterActionInvoker.InvokeActionFilterAsync()    Unknown
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder<System.__Canon>.Start<Microsoft.AspNet.Mvc.Controllers.FilterActionInvoker.<InvokeActionFilterAsync>d__53>(ref Microsoft.AspNet.Mvc.Controllers.FilterActionInvoker.<InvokeActionFilterAsync>d__53 stateMachine)    Unknown
Microsoft.AspNet.Mvc.Core.dll!Microsoft.AspNet.Mvc.Controllers.FilterActionInvoker.InvokeActionFilterAsync()    Unknown

EDIT

Found that the HomeControler constructor is called twice???

public class HomeController : Controller
{
    public HomeController()
    {
        System.Diagnostics.Debug.WriteLine("CCCCCCCCCCCCCCCCCCCCCC");
    }

    public string Index()
    {
        return string.Empty;
    }

Console output:

'dnx.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\Miroslav\.dnx\packages\Microsoft.Extensions.Localization.Abstractions\1.0.0-rc1-final\lib\net451\Microsoft.Extensions.Localization.Abstractions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'dnx.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\Miroslav\.dnx\packages\Microsoft.VisualStudio.Web.BrowserLink.Loader\14.0.0-rc1-final\lib\dnx451\Microsoft.VisualStudio.Web.BrowserLink.Loader.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'dnx.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\Miroslav\.dnx\packages\Microsoft.AspNet.WebUtilities\1.0.0-rc1-final\lib\net451\Microsoft.AspNet.WebUtilities.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
CCCCCCCCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCCCCCCCC
'dnx.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\Miroslav\.dnx\packages\Microsoft.AspNet.Http.Extensions\1.0.0-rc1-final\lib\net451\Microsoft.AspNet.Http.Extensions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The thread 0x5d84 has exited with code 0 (0x0).
The thread 0x7334 has exited with code 0 (0x0).

EDIT 2

Probably the issue appears only in Debug mode:

public static class Counter
{
    public static int CtrCalled { get; set; }
    public static int IndexCalled { get; set; }
}

public class HomeController : Controller
{
    public HomeController()
    {
        Counter.CtrCalled++;
    }

    public string Index()
    {
        Counter.IndexCalled++;
        return $"Constructor called: {Counter.CtrCalled}, Index called: {Counter.IndexCalled}.";
    }
}

Debug mode output: Constructor called: 2, Index called: 1.

Debug mode with debug points: Constructor called: 2, Index called: 2.

Release mode output: Constructor called: 1, Index called: 1.

like image 677
Miroslav Popov Avatar asked Nov 19 '15 04:11

Miroslav Popov


1 Answers

Daniel Roth answers:

This is VS sending a separate request to setup debugging. If you try to run the app outside of VS you shouldn't see this extra request.

Further discussion here: https://github.com/aspnet/Tooling/issues/261

like image 126
Miroslav Popov Avatar answered Sep 23 '22 23:09

Miroslav Popov