Performance logging for WebForms pages via MVC routing - asp.net-mvc

We have a legacy application that is part-ASP.NET WebForms and part-ASP.NET MVC.
In order to give all URLs an MVC "style" we've registered a set of routes to map the desired URLs back to the WebForms URLs.
e.g.
routes.Map("somemapping", "NiceUrl/{page}").To("~/UglyUrl/UglyPath/{page}.aspx");
On the MVC Controllers we have implemented performance logging of action methods by way of a custom attribute that inherits from ActionFilterAttribute and overrides OnActionExecuting and OnActionExecuted.
We would like to implement similar logging for the WebForms pages. Is it possible to hook into the routing part and log from there?

Using a System.Diagnostics.StopWatch could solve your problem at a global level.
Here is my proposed solution:
1. In the application BeginRequest instantiate a new instance of StopWatch.
2. Call the start method on the stop watch instance.
3. Place the stop watch in the HttpContext.Current.Items collection
4. In the application End Request, get the StopWatch instance from the httpcontext items, call the stop method, and used the "Elapsed" property of your choice to get the necessary time data that you would like to store
this will provide one single place where you can measure the processing time of all requests, mvc and webforms.

Related

ASP.NET MVC Session and SessionStateBehavior

All:
I have been searching high and low for an answer to this, so forgive me if this is a dupe, I just can't seem to find the right answer.
Let's say you have an ASP.NET MVC Controller marked with the [SessionState(SessionStateBehavior.Disabled)]
attribute. Does calling actions on this controller "refresh" the session state, keeping it "alive"? Specifically, I have a AJAX request calling a controller to keep the session "alive" since the application is a single page application driven by javascript, and I don't want the users session to die, so every 30 seconds I make a call up to this controller. Similarly, would it stay alive if the controller was marked SessionStateBehavior.ReadOnly? Finally, is using an ASP.NET MVC Controller for this purpose not the best way (is there a better way)?
Thanks!
Session state will be kept alive automatically with no attributes on the action, depending on how the web.config is configured. To configure it, see here: http://msdn.microsoft.com/en-us/library/h6bb9cz9(v=vs.100).aspx
A controller should be okay, but you may want to look into Web API as it'll offer a few more features in this scenario: http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

In http module's BeginRequest event, how to find that current request is of MVC ?

In http module's BeginRequest event, how to find that current request is of MVC and not of traditoinal ASP.net ?
What are you trying to accomplish? If you want some code to run prior to a particular controller action, or all actions of all controllers you can use action filters. These can be wired up using attributes on your controllers. Alternatively you can set them up dynamically using DI and interception, etc.
I think you're trying to do something in a way that is limiting your options... filters are most likely what you want.

When do the HttpApplication.BeginRequest and EndRequest events fire relative to ActionFilter.OnResultExecuted in the MVC pipeline?

The reason I ask is that I am trying to run MVC side-by-side with WebForms.
For WebForms we use an HttpModule to open and close an NHibernate Session once per request - opening the session during HttpApplication.BeginRequest and HttpApplication.EndRequest. For MVC, the recommended method is to use an ActionFilterAttribute but to keep things simple I have decided to stick with the HttpModule.
However, I am using an ActionFilter to open and close Transactions.
I am getting some strange intermittent problems though and I'm curious whether the issue could be that the HttpApplication.EndRequest method is firing before the ActionFilter's OnResultExecuted method.
Here's the order of execution:
1. HttpApplication.BeginRequest
2. ActionFilterAttribute.OnResultExecuted
3. HttpApplication.EndRequest
The HttpApplication.EndRequest can never fire before ActionFilterAttribute.OnResultExecuted.

What is the lifetime of a ASP.NET MVC Controller?

I'm in the process of developing my MVC application and I was thinking, What is the lifetime of a controller class?
When does it get created? How many instances of a single controller are there? what are the implications of local variables? when is it destroyed?
I'm sure there is a good link somewhere floating around on the internet, but my google-fu couldn't find it.
Stephen Walther has a great article on the life-cycle of a request being handled by the MVC Framework.
Here's a extract from the top of his article, it goes on to explain each step in detail:
Overview of the Lifecycle Steps
There are five main steps that happen when you make a request from an ASP.NET MVC website:
1. The RouteTable is Created
This first step happens only once when an ASP.NET application first starts. The RouteTable maps URLs to handlers.
2. The UrlRoutingModule Intercepts the Request
This second step happens whenever you make a request. The UrlRoutingModule intercepts every request and creates and executes the right handler.
3. The MvcHandler Executes
The MvcHandler creates a controller, passes the controller a ControllerContext, and executes the controller.
4. The Controller Executes
The controller determines which controller method to execute, builds a list of parameters, and executes the method.
5. The RenderView Method is Called
Typically, a controller method calls RenderView() to render content back to the browser. The Controller.RenderView() method delegates its work to a particular ViewEngine
Assuming you don't change the default ControllerFactory, controllers will be created for every request and will be garbage collected "sometime after" the request has completed.
In short, you don't need to worry about race conditions for instance variables (though you do for static variables, obviously). Having said that, I'd recommend keeping your controller actions reentrant for the sake of cleaner code.

Specifying read-only session in ASP.NET MVC

Is there any way to specify that a given Controller or Action uses Session state in a read-only manner? In "old" ASP.NET we used to do something like this:
<%# Page EnableSessionState="ReadOnly" %>
Is there an ASP.NET MVC equivalent? I'm looking to allow my application to serve multiple requests from the same client simultaneously, and turning off session completely is -not- an option in my case.
In Asp.Net MVC3 there is now a SessionStateAttribute that you can decorate your Controller with to force all actions into Read-Write, Read-only, or No session mode.
http://msdn.microsoft.com/en-us/library/system.web.mvc.sessionstateattribute(v=VS.98).aspx
A similar question was asked here: Disable Session state per-request in ASP.Net MVC
The accepted answer deals with creating a custom route handler that ultimately skips binding to the session. This doesn't exactly answer your question (which is how to declare multiple actions to use readonly session access), but it seemed relevant enough to mention.
This could be a bit tricky--my understanding is setting up the session state is something that happened at the IHttpHandler level. With custom handlers, you could specify the session state by using marker interfaces, like IReadOnlySessionState to declare that it only needs readonly session state. You could try appending that to your controller and see how it flies. I'd suspect it won't because ASP.NET mvc controllers happen well after the IHttpHandler is wired up, but it is worth a shot.
Another solution might be to wrap the session state variables in your own class, and expose a readonly version to enforce readonly. Accessing it across multiple requests shouldn't be a problem, but you are correct that you can get race conditions and such if you start trying to write to the same session variables from multiple angles.
What about setting the data you want to be "Read Only" as static in your Model? In this way you could have simultaneous requests to MVC with the same data.

Resources