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.
ObjectID’s with MongoDB and the mongodb-csharp driver
I mist say, the latest release of mongodb-csharp is rather awesome. Typed collections and LINQ support mean I can worry more about my application than about the data layer.
Here is an example of using typed collections:
public class Customer
{
public Oid Id { get; set; }
public string Name { get; set; }
public CustomerBillingInfo Billing { get; set; }
public List Depts { get; set; }
}
public void AddCustomer(Customer customer) {
...(code removed for simplicity)...
IMongoCollection collection = database.GetCollection();
collection.Save(customer);
}
Gotcha: Beware of the ID property!
One thing that threw me off when I began using the typed collections was that I had defined my ID property as “_id” because That is what MongoDB uses internally. While this made sense to me, the mongod process kept throwing errors whenever I tried the following:
...(code removed for simplicity)... IMongoCollection<customer> collection = database.GetCollection<customer>(); Customer customer = collection.Linq().First(c => c.Name == "test customer"); customer.Name = "A new name!"; collection.Save(customer);
The error looked something like this:
Fri Jun 25 10:43:14 Exception 11000:E11000 duplicate key error index: test06.Customer.$_id_ dup key: { : ObjId(4c24c07a189cf31bd4000002) }
Fri Jun 25 10:43:14 Caught Assertion in insert , continuing
Fri Jun 25 10:43:14 insert test06.Customer exception userassert:E11000 duplicate key error index: test06.Customer.$_id_ dup key: { : ObjId(4c24c07a189cf31bd4000002) } 21ms
It was driving me crazy for days before I realized that the driver was doing some magic – the POCO object needed its ID to be named “Id” instead of “_id” an as soon as I changed that – it started working properly.
Convert C# classes to and from MongoDB Documents automatically using .NET reflection
There are a number of C# based MongoDB projects being actively developed right now but one thing that I needed was a way to convert a standard C# class to a MongoDB document for easy insertion. It isn't hard to manually type out and set each property by hand, but it certainly is not the most efficient way, especially when you know you are going to be doing it a lot.
For example:
Lets say I have a class called SomeClass that looks something like this:
class SomeClass {
public string StringTest;
public int IntTest;
}
And somewhere in my code, I have an instance of this class named someClassInstance. If I want to create a MongoDB document from this class, I’d have to do something like this:
Document document = new Document();
document.add('StringTest', someClassInstance.StringTest);
document.add('IntTest', someClassInstance.IntTest);
So that isn't such a big deal, right? But what about when I have a class with many more properties? Then it starts to get messy and cumbersome. I thought that there should be an straightforward way to easily convert any class to a mongo-csharp compatible Document object. (I am using Sam Corder’s mongo-csharp driver, so that I am targeting the Document object from that library.)
Default values
I also wanted to have a way to specify what the default values were for each class property so that when we did the conversion, we would (hopefully) not end up with any null values. Plus, if for some reason there was a document in MongoDB that was missing a particular key-value pair, the DocumentConverter would automatically fill in that empty field with the default value so in the code we should never have any nulls.
This is something that I would like for my own purposes and may not suit everyone’s needs. If it doesn't, simply leave off the DefaultValueAttribute and you’ll never know the difference.
My proposed solution
I figured the easiest way to accomplish this was to create a class that would encapsulate all the functionality needed to convert to and from Document objects and have my other classes inherit from that one. I imagined that the above code would change to something like this:
class SomeClass : DocumentConverter {
[Attributes.DefaultValue("Default StringTest value!")]
public string StringTest;
[Attributes.DefaultValue(16)]
public int IntTest;
}
And to do the conversion would be very simple. To convert from someClass to Document would be:
Document document = someClassInstance.ToMongoDocument();
To convert from a Document object to someClass would be:
SomeClass someOtherClassInstance = new SomeClass(); omeOtherClassInstance .FromMongoDocument(someDocumentObject);
The DefaultAttribute class
using System;
namespace MyApp.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
class DefaultValueAttribute : Attribute
{
private readonly object _value;
public DefaultValueAttribute(object Value)
{
_value = Value;
}
public object GetDefaultValue()
{
return _value;
}
}
}
The DocumentConverter class
Reflection isn't something that I use too often so there may be better ways of accomplishing what I am trying to do, but this is what I’ve got for now. If there are better ways, please let me know. Without further ado…
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using MyApp.Classes.Attributes;
using MongoDB.Driver;
namespace MyApp.Classes
{
public class DocumentConverter
{
public void FromMongoDocument(Document document)
{
foreach (DictionaryEntry kvp in document)
{
object propertyValue;
if (kvp.Value != null && (kvp.Value.GetType() == typeof(Document)))
{
// We have a document object - Now lets get a reference to the class property's type
var propertyType = GetType().GetProperty(kvp.Key.ToString()).PropertyType;
// create new instance of that class
var propertyInstance = Activator.CreateInstance(propertyType);
// call FromMongoDocument on that class and pass in the document
MethodInfo method = propertyInstance.GetType().GetMethod("FromMongoDocument");
method.Invoke(propertyInstance, new[] { kvp.Value });
propertyValue = propertyInstance;
}
else
{
// This is not a Document so lets just assign the value
propertyValue = kvp.Value;
}
GetType().GetProperty(kvp.Key.ToString()).SetValue(this, propertyValue, null);
}
}
public Document ToMongoDocument()
{
Document document = new Document();
foreach (PropertyInfo property in GetType().GetProperties())
{
// Get the value of this property
object propertyValue = property.GetValue(this, null);
// If this value is null, then lets try to see if there is a default value attribute and assign that
if (propertyValue == null)
{
object[] attributes = property.GetCustomAttributes(typeof(DefaultValueAttribute), true);
foreach (DefaultValueAttribute defaultValue in attributes.Cast())
{
propertyValue = defaultValue.GetDefaultValue();
}
document.Add(property.Name, propertyValue);
}
else
{
// We have a property, now lets see if this property has a ToMongoDocument method
MethodInfo method = propertyValue.GetType().GetMethod("ToMongoDocument");
if (method == null)
{
document.Add(property.Name, property.GetValue(this, null));
}
else
{
document.Add(property.Name, method.Invoke(propertyValue, null));
}
}
}
return document;
}
}
}
That’s all for now
I hope this is useful for someone. It is a rough draft of what I threw together last night at around 1AM while half asleep. So far, it has passed all of my initial tests but if you have suggestions to make it better, please leave some comments here.
The Current State of MongoDB and C#
As a C# developer, I am often disappointed with the lack of drivers and connectors to cool services like MongoDB. All the cool languages (and Java) get all the love but C# is often an afterthought.
Luckily for me, there are some kickass developers in the C# community who also share my frustration and as such, they have begun building their own C# MongoDB drivers.
I keep stumbling across more and more C# related MongoDB projects, so I figured I would write up a list and some short descriptions of these projects.
List of C# MongoDB Projects
Each of these projects are still rather new, so expect some features to be missing or not fully functional. A couple of them are usable in your projects today while the rest are still under heavy development
mongodb-csharp
Originally written by Sam Corder (@SamCorder) with help from a handful of contributers, this is the most complete driver of the bunch. It has been evolving quickly and Sam & team are very quick to resolve any bugs that may arise.
I am using this driver in 2 projects that I am working on and so far things have been great. It even includes GridFS suport.
From the project description:
Current Features
- Connect to a server.
- Query
- Insert
- Update
- Delete
- All BSON types supported
- DBRef support
- Isolation and conversion between BSON types and native .net types.
- Database, Collection and Cursor objects.
- Index handling routines (List, Create, Drop)
- Count
- Roughly 80% unit test coverage. This can and will be improved on.
- Paired connections
- Authentication (Does not reauthorize on auto reconnect yet).
- Database Commands
- Basic Linq support
- GridFS support
- Map Reduce helpers.
- hint, explain, $where
They are currently working on connection management features (auto reconnect, connection pooling, etc).
Get involved or check out the code at their mongodb-csharp project page on Github.
mongodb-net
Written by the unnamed developer at DevFuel.com, the mongo-net project aims to be a C# port of the 10Gen\MongoDB official Java driver. While a lot of work has been done and a load of code written, it is currently unusable. Over the past couple of weeks a significant amount of progress has been made and functionality is beginning to work but it seems that a functional state is months away.
I would keep an eye on this project, though, as having an API compatible with the official Java driver has its benefits.
Get involved or check out the code at their mongodb-net project page on Google Code.
MongoDB.Emitter
Andrew Rondeau’s MongoDB.Emitter is a pretty cool project that provides a strongly-typed Document mapper for C#. It works in conjunction with Sam Corder’s mongodb-csharp driver allowing the programmer to define strongly typed interfaces and properties.
I have not tried this yet, but this will be on my list of things to check out.
Get involved or check out the code at their MongoDB.Emitter project page on bitbucket.
CSMongo
Hugo Bonacci (@hugoware) has been working on a driver of his own called CSMongo. CSMongo doesnt support everything that mongodb-csharp does but it does have some interesting features, such as the approach to creating Mongo Documents. Their approach definitely has a more dynamic feel to it which fites nicely in the unstructured MongoDB world.
I am looking forward to the next version which should have more features including support for Hugo’s own jLinq.
Code doesn't appear to be released yet but you can follow his progress at his blog, Hugoware.
simple-mongodb
Simple-mongdb is another project without public source that is being worked on by Daniel Wertheim (@danielwertheim). I am not sure if it is even being actively developed but it too has some nice ideas. The goal of this project is to keep the driver JSON-centric and should be compatible with awesome Newtonsoft’s JSON.net library.
They have a few examples of the proposed API but no code has been released to make said examples work. This is another one to keep an eye on in the meantime.
Check out the simple-mongodb project page on Google Code.
DocumentConverter
This is a small class I wrote that works with mongodb-csharp. Its name will likely change at some point if and when it gets packaged up and put on source control. It exposes two functions that will allow any C# class to convert to and from a MongoDB Document object automatically. It makes my life a lot easier and it uses System.Reflection to do this.
Read more about DocumentConverter on this blog post.
Conclusion
Well it looks like there is a significant amount of interest in MongoDB from the C# community which is great news because it looks like MongoDB is going to continue to thrive and grow. My bet is that Sam Corder’s driver will be the most common C# driver, simply because it is so far ahead of the rest but time will tell. Extensions of Sam’s project, such as MongoDB.Emitter, are equally as cool as the drivers they are built on.
Thanks toeveryone who has contributed to these drivers. Each of them have some great concepts and I hope that one day we will have a driver that supports all these great ideas and features.
If there are any more projects that I have missed – let me know in the comments!
Some thoughts on string concatenation in C#
I recently stumbled into a blog entry on CodeProject.com that stated some things about string concatenation in C# that went against what I thought to be true, and it got me thinking.
From the article:
string sentence = "The " + "dog " + "ate " + "the " + "cat " + "all " + "day " + "for " + "for " + "fun.";
”That innocent looking line of code actually takes up much more processing power and memory than it appears to. If strings were combined in the ideal way, you would expect that the sentence would be the only string created from this operation. However, since each string is combined to its neighbor in succession, it turns out that 7 other strings are also created (shown in gray in the diagram above). The total amount of unnecessary memory allocations created from this operation is equal to the following equation, where N is the number of strings you are combining…”
But I knew that couldn’t be true, so I fired up Visual Studio and wrote a few simple tests, compiled and then analyzed the generated CIL.
First test:
Original C#:
public string createStringOne()
{
return "The " + "dog " + "ate " + "the " + "cat " + "all " + "day " + "for " + "for " + "fun.";
}
Generated CIL:
.method public hidebysig instance string createStringOne() cil managed
{
.maxstack 8
L_0000: ldstr "The dog ate the cat all day for for fun."
L_0005: ret
}
Second test:
Original C#:
public string createStringTwo()
{
return "The dog ate the cat all day for for fun.";
}
Generated CIL:
.method public hidebysig instance string createStringTwo() cil managed
{
.maxstack 8
L_0000: ldstr "The dog ate the cat all day for for fun."
L_0005: ret
}
Third test:
Original C#:
public string createStringThree()
{
var sb = new StringBuilder();
sb.Append("The ");
sb.Append("dog ");
sb.Append("ate ");
sb.Append("the ");
sb.Append("cat ");
sb.Append("all ");
sb.Append("day ");
sb.Append("for ");
sb.Append("for ");
sb.Append("fun.");
return sb.ToString();
}
Generated CIL:
.method public hidebysig instance string createStringThree() cil managed
{
.maxstack 2
.locals init (
[0] class [mscorlib]System.Text.StringBuilder sb)
L_0000: newobj instance void [mscorlib]System.Text.StringBuilder::.ctor()
L_0005: stloc.0
L_0006: ldloc.0
L_0007: ldstr "The "
L_000c: callvirt instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
L_0011: pop
L_0012: ldloc.0
L_0013: ldstr "dog "
L_0018: callvirt instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
L_001d: pop
L_001e: ldloc.0
L_001f: ldstr "ate "
L_0024: callvirt instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
L_0029: pop
L_002a: ldloc.0
L_002b: ldstr "the "
L_0030: callvirt instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
L_0035: pop
L_0036: ldloc.0
L_0037: ldstr "cat "
L_003c: callvirt instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
L_0041: pop
L_0042: ldloc.0
L_0043: ldstr "all "
L_0048: callvirt instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
L_004d: pop
L_004e: ldloc.0
L_004f: ldstr "day "
L_0054: callvirt instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
L_0059: pop
L_005a: ldloc.0
L_005b: ldstr "for "
L_0060: callvirt instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
L_0065: pop
L_0066: ldloc.0
L_0067: ldstr "for "
L_006c: callvirt instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
L_0071: pop
L_0072: ldloc.0
L_0073: ldstr "fun."
L_0078: callvirt instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
L_007d: pop
L_007e: ldloc.0
L_007f: callvirt instance string [mscorlib]System.Object::ToString()
L_0084: ret
}
Fourth test:
Original C#:
public string createStringFour()
{
return new StringBuilder("The dog ate the cat all day for for fun.").ToString();
}
Generated CIL:
.method public hidebysig instance string createStringFour() cil managed
{
.maxstack 8
L_0000: ldstr "The dog ate the cat all day for for fun."
L_0005: newobj instance void [mscorlib]System.Text.StringBuilder::.ctor(string)
L_000a: callvirt instance string [mscorlib]System.Object::ToString()
L_000f: ret
}
Fifth test:
Original C#:
public string createStringFive()
{
string s = "The ";
s += "dog ";
s += "ate ";
s += "the ";
s += "cat ";
s += "all ";
s += "day ";
s += "for ";
s += "for ";
s += "fun.";
return s;
}
Generated CIL:
.method public hidebysig instance string createStringFive() cil managed
{
.maxstack 2
.locals init (
[0] string s)
L_0000: ldstr "The "
L_0005: stloc.0
L_0006: ldloc.0
L_0007: ldstr "dog "
L_000c: call string [mscorlib]System.String::Concat(string, string)
L_0011: stloc.0
L_0012: ldloc.0
L_0013: ldstr "ate "
L_0018: call string [mscorlib]System.String::Concat(string, string)
L_001d: stloc.0
L_001e: ldloc.0
L_001f: ldstr "the "
L_0024: call string [mscorlib]System.String::Concat(string, string)
L_0029: stloc.0
L_002a: ldloc.0
L_002b: ldstr "cat "
L_0030: call string [mscorlib]System.String::Concat(string, string)
L_0035: stloc.0
L_0036: ldloc.0
L_0037: ldstr "all "
L_003c: call string [mscorlib]System.String::Concat(string, string)
L_0041: stloc.0
L_0042: ldloc.0
L_0043: ldstr "day "
L_0048: call string [mscorlib]System.String::Concat(string, string)
L_004d: stloc.0
L_004e: ldloc.0
L_004f: ldstr "for "
L_0054: call string [mscorlib]System.String::Concat(string, string)
L_0059: stloc.0
L_005a: ldloc.0
L_005b: ldstr "for "
L_0060: call string [mscorlib]System.String::Concat(string, string)
L_0065: stloc.0
L_0066: ldloc.0
L_0067: ldstr "fun."
L_006c: call string [mscorlib]System.String::Concat(string, string)
L_0071: stloc.0
L_0072: ldloc.0
L_0073: ret
}
Results:
So as you can see, the first two methods are essentially the same thing! It doesn't matter if we concatenate one large string from several smaller strings if (and only if) it happens on one operation. If concatenation is done using multiple operations, then it does in fact incur a performance and memory hit.
The use of a StringBuilder in this case (where there are only a small handful of small strings) serves no purpose as far as performance is concerned.
But… how are they equal!?
Compiler optimizations! The C# compiler is an incredible piece of software that can look at your code and “fix” it. It is important when working on code optimizations like the one in the article mentioned above that developers take into consideration compiler optimizations that may alter the code that they think is poorly written.
Comparing while() loops with for() loops in C#
There is always a lot of debate about the speed and performance of loops in any language. I was curious to see what the differences were between a for() loop and a while() loop in C# and ultimately the .NET CLR.
What I found was no big surprise to me, as it simply confirms what I have been reading for many years. While loops are simply faster and less complex than for loops. I did a very simple test to verify this. I created two loops that contained empty bodies. You can see them below:
public void doFor()
{
int i = 0;
int len = 100;
for (i = 0; i < len; i++)
{
}
}
public void doWhile()
{
int i = 100;
while (i-- == 0)
{
}
}
As you can see, both of these loops are as basic as they can be and have no inner bodies to complicate the CIL. Lets take a look at the generated CIL for each of them.
The generated CIL for a for() loop
.method public hidebysig instance void doFor() cil managed
{
.maxstack 2
.locals init (
[0] int32 i,
[1] int32 len)
L_0000: ldc.i4.0
L_0001: stloc.0
L_0002: ldc.i4.s 100
L_0004: stloc.1
L_0005: ldc.i4.0
L_0006: stloc.0
L_0007: br.s L_000d
L_0009: ldloc.0
L_000a: ldc.i4.1
L_000b: add
L_000c: stloc.0
L_000d: ldloc.0
L_000e: ldloc.1
L_000f: blt.s L_0009
L_0011: ret
}
The generated CIL for a while() loop
.method public hidebysig instance void doWhile() cil managed
{
.maxstack 3
.locals init (
[0] int32 i)
L_0000: ldc.i4.s 100
L_0002: stloc.0
L_0003: ldloc.0
L_0004: dup
L_0005: ldc.i4.1
L_0006: sub
L_0007: stloc.0
L_0008: brfalse.s L_0003
L_000a: ret
}
The results are in and there is no surprise.
As you can see, the generated CIL for a for() loop is larger by 6 instructions. Furthermore, in this code the for() loop initializes 2 local variables rather than 1 (but this may vary depending on your code).
What does it all mean?
Probably nothing. 6 more instructions and an additional variable declaration will in most cases have no impact on the performance, even if you are measuring in microseconds. I am curious to see what the resulting difference between the loops above would be in other languages.