Use PostSharp to easily allow MiniProfiler to profile your ASP.NET application
Scott Hanselman has written a great post detailing how to include the MiniProfiler in your ASP.NET application. I tried the profiler out and it works great. Its lightweight and easy to configure but by default it does not give you very much information about your requests and method calls.
Admittedly, it is a ”mini” profiler so you cannot expect great things from it, such as memory usage like you would get from a full featured out-of-process profiler – but this thing can go pretty far.
Unfortunately, I did not want to add using() statements all over my code in every method I want to profile. Being able to mark methods with attributes would be great, and ASP.NET MVC lets you do just that which makes it easy to mark your base controller with an attribute that allows you to profile actions. This does not, however, give you any information about the methods being called from within the action.
C# has no built in way to intercept method invocation so the only way to accomplish this is to use some sort of Aspect Oriented Programming (AOP) library\utility. There are a few out there, including:
- Unity
- Sprint.NET
- PostSharp
- Castle Windsor Dynamic Proxy
- StructureMap
- and a few other smaller players
I’ve decided to use PostSharp Community Edition. It is a free, commercially backed product, and it works pretty well. With PostSharp, you can define attributes (or aspects) that allow you to run code before or after a method is invoked and catch & handle exceptions. IT does this by post-processing the IL that is generated by the C# compiler and modifies the areas of the code necessary to make your code work as you intended.
It does add an additional process to the compilation but if you need this sort of power, its worth it.
Here is the code for using PostSharp and MiniProfiler together
And wherever you want to profile, you can mark either the entire class or a specific methods with [ProfilingAspect] and now you will see those method calls showing up in your MiniProfiler logs.
Pretty easy, isnt it?
Call mulitple services or methods asynchronously in ASP.NET MVC controller
I recently had to write some code that reached out to both Google and Bing, performed searches on both, combined and manipulated the results and return the result to the client via JSON.
My problem was that sometimes either of those two services could be slow and there is a possibility that I may need to add additional data sources at a later time. The code originally looked like this:
As you can see, this code would accomplish the goal without a hitch, however if Google took longer than usual, Bing would have to wait for Google to finish. This just creates a bad experience for my users. I needed a way to be able to fire off both of those searches at the same time without blocking one another, however to be of any use to my users, the action would have to wait for both of them to complete before combining the results and return them to the client. This problem would be amplified by adding additional data services (like Yahoo, for example).
Even though I had done this several times in PHP, I had no idea how to accomplish it in C#. I know that AsyncControllers exist and it sounds like they rock, but what if I wanted to keep this all in the same controller as other related code?
I did some digging and found nothing, so I turned to a developers best friend – StackOverflow. I posted a question asking how to accomplish this and got back a few great answers.
This is what I decided on, and it works great:
You can create as many Task’s as you need, and then start each of them with the Start() method. Then using the Task.WaitAll() method, you instruct your program to wait until all of those Task’s have completed before continuing to the next line of code.
You can also specify a timeout as the second parameter of Task.WaitAll that will prevent your Task’s from running indefinitely, which is definitely helpful for Ajax requests when the client is waiting on some response.