Use intelliitrace to debug and track MVC Framework Action calls

Source: Internet
Author: User

The difference between intelliitrace debugging and normal breakpoint plus one-step tracing mode is that it supports simulated re-debugging of historical processes. When we want to know how the application was executed under normal debugging, we usually stop debugging and re-Add the breakpoint to start debugging. With intelliitrace, we can use its unique historical debugging function to "go back to the past", so that a debugging can effectively locate the problem. Now I want to use this function to find out how the Controller's Action method is called in the open-source MVC framework.

As we all know, MVC intercepts the address bar parameters through URL routing to obtain the Controller and Action values, and uses these two string types to locate the Controller and the Controller method, and then returns the view. The problem is that it is impossible to directly implement the class and call the method only by knowing the class name and Method Name of the string. As a result, "naturally" Thought of reflection. Many people complain that Microsoft's new features are at the cost of performance because of the high performance cost. C # is a language with low performance. But what is the truth? If there is no investigation, there is no right to speak. Let's start the investigation first.

Introduce the MVC open source file to the Project

1. Download the MVC Framework source code: ASP. net mvc 2 source code ASP. net mvc 3 source code. This article uses MVC2.

2. Create an MVC project in VS2010 and delete the reference "System. Web. Mvc ". Decompress the source code package, copy the SystemWebMvc directory under src to the project folder, add a project to the solution, and then add references to the open source project.

Click to download the configured Project

Debug with intelliitrace

Step 1: Because intelliitrace debugging is disabled by default, you must enable it first. Security F5 enters the debugging status. In the "intelliitrace" window on the right, click Open intelliitrace settings. Select "enable intelliitrace" and click "intelliitrace events and call information" in a single-choice combination, as shown in.

  

After the configuration is complete, click OK, and then stop debugging. You can breakpoint the HomeController Index method to start debugging. The program requests the Index page to hit the breakpoint, at this time, you will find that there are several arrows around the breakpoint, which is used for debugging by intelliitrace.

Now, we need to demonstrate the magic of debugging intelliitrace. Since we want to know who called the Index method, how can we operate it? It's time to witness the miracle! Is it easy to know the caller if we want to let the program execute backwards? I saw an upward double arrow. I told him to "roll back in one step" and click it once. The program located the Execute method of a class and called the _ executor delegate, then we analyzed the code and found that this delegate was implemented in the GetExecutor function below it. we focused on GetExecutor and I pasted it below.

        private static ActionExecutor GetExecutor(MethodInfo methodInfo) {            // Parameters to executor            ParameterExpression controllerParameter = Expression.Parameter(typeof(ControllerBase), "controller");            ParameterExpression parametersParameter = Expression.Parameter(typeof(object[]), "parameters");            // Build parameter list            List<Expression> parameters = new List<Expression>();            ParameterInfo[] paramInfos = methodInfo.GetParameters();            for (int i = 0; i < paramInfos.Length; i++) {                ParameterInfo paramInfo = paramInfos[i];                BinaryExpression valueObj = Expression.ArrayIndex(parametersParameter, Expression.Constant(i));                UnaryExpression valueCast = Expression.Convert(valueObj, paramInfo.ParameterType);                // valueCast is "(Ti) parameters[i]"                parameters.Add(valueCast);            }            // Call method            UnaryExpression instanceCast = (!methodInfo.IsStatic) ? Expression.Convert(controllerParameter, methodInfo.ReflectedType) : null;            MethodCallExpression methodCall = methodCall = Expression.Call(instanceCast, methodInfo, parameters);            // methodCall is "((TController) controller) method((T0) parameters[0], (T1) parameters[1], ...)"            // Create function            if (methodCall.Type == typeof(void)) {                Expression<VoidActionExecutor> lambda = Expression.Lambda<VoidActionExecutor>(methodCall, controllerParameter, parametersParameter);                VoidActionExecutor voidExecutor = lambda.Compile();                return WrapVoidAction(voidExecutor);            }            else {                // must coerce methodCall to match ActionExecutor signature                UnaryExpression castMethodCall = Expression.Convert(methodCall, typeof(object));                Expression<ActionExecutor> lambda = Expression.Lambda<ActionExecutor>(castMethodCall, controllerParameter, parametersParameter);                return lambda.Compile();            }        }

If you have studied the expression tree, you must know that this method constructs an Expression Tree that calls the Action method and returns the delegate of the call process through lambda. Compile. Lao Zhao has already analyzed the efficiency of lambda Expression Tree calling methods in this article [Click to learn]. Its efficiency is almost the same as that of static calling. So those who are worried about "reflection" performance reduction can rest assured.

Postscript

Although the topic of this article is not clear enough, I have discussed debugging and MVC (I like to follow my mind, haha), which also reflects some problems. If we analyze this problem through common debugging methods, we should first give the MVC source code to the analysis side. Then, we can easily find the entry point, the Controller's Execute method, after a breakpoint is added, the system starts to pull water. After a deep dive, the system executes the Action. Then we need to debug it again and recall where we jumped to the Action, finally found. So hard! With intelliitrace, the task is completed by clicking the mouse. In addition, selecting the source code of the MVC framework indicates that the value of such debugging functions can be reflected only when the project is complex enough. In addition, I also learned how to map the MVC Framework to the method through the address bar parameters.

By Lipan)
Source: [Lipan] (http://www.cnblogs.com/lipan)
Copyright: The copyright of this article is shared by the author and the blog. The detailed link of this article must be noted during reprinting; otherwise, the author will be held legally liable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.