MVC Mini Profiler with Entity Framework and SQL Server CE 4.0

Recently I was trying to use MVC Mini Profiler released by Stack Overflow folks with my site on ASP.NET MVC 3 framework and had some problems to use its all features, because I had a little specific setup (more on that a bit later).

It was a piece of cake to use it for profiling usual code with:

using (MiniProfiler.Current.Step("Some profiling step description"))

Everything worked like a charm with just following a step-by-step guide on project’s site (http://code.google.com/p/mvc-mini-profiler/).

Problems started when I got to DB profiling support. I should point out that the specific situation I was in includes using SQL Server CE 4.0 with Entity Framework 4 with DB first approach.

As you can read from their instructions, you have to substitute their profiled connection for the real connection you usually use. But as it turned out not everything is so simple and after some experimenting and searching through Stack Overflow and generally Internet I arrived at the following solution to get Entity Framework’s ObjectContext for you DB Model.

public static T GetDb<T>(string connectionStringName) where T: ObjectContext
{
    var connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
    var entityConnStr = new EntityConnectionStringBuilder(connectionString);
    var realConnection = new SqlCeConnection(entityConnStr.ProviderConnectionString);
    var profiledConnection = ProfiledDbConnection.Get(realConnection, MiniProfiler.Current);
    return profiledConnection.CreateObjectContext<T>();
}

Here connectionStringName is the name of connection string in your web.config (or app.config) file, T specifies the type of your ObjectContext.

So what do we do here is the following:

  1. We get connection string for web.config (or app.config). If you are using Entity Framework, this connection string will contain not only connection string per se, but also some metadata, which in my case was causing throwing exception by SqlCeConnection constructor.
  2. So we need to get rid of this metadata. We create EntityConnectionStringBuilder which parses nicely our long connection string and provides us with separate parts of it.
  3. For the bare connection to SQL Server CE 4.0 we need just a provider connection string part, which we extract with ProviderConnectionString property.
  4. After that we use ProfiledDbConnection.Get helper method provided by MVC Mini Profiler to create a profiled version of that connection, which we should use instead of standard one.
  5. We use another helper method (which is extension method and is contained within a static class ObjectContextUtils which is also provided by MVC Mini Profiler) to create our DB model object context.

And that’s all. Works very well for me. If you are using not CE edition of SQL Server, you have to change the SqlCeConnection class to SqlConnection.

I suppose you’ll create one more method for your specific application to not provide connection string name throughout all your application:

public static MyObjectContext GetDb() 
{
    return GetDb<MyObjectContext>("MyObjectContextConnStr");
}

Hope it helps not only for me next time you’ll try to use MVC Mini Profiler which is a really awesome project. Thanks to Stack Overflow guys for this thing! :)

Update (2011-08-02):

The original post was written while using MvcMiniProfiler 1.4. After updating today to version 1.7, DB profiling stopped working giving the following error message: “Unable to find the requested .Net Framework Data Provider. It may not be installed.” After quick googling, it appears that the following addition to web.config is enough to overcome this problem:

<system.data>
    <DbProviderFactories>
        <remove invariant="MvcMiniProfiler.Data.ProfiledDbProvider" />
        <add description="MvcMiniProfiler.Data.ProfiledDbProvider" invariant="MvcMiniProfiler.Data.ProfiledDbProvider" type="MvcMiniProfiler.Data.ProfiledDbProviderFactory, MvcMiniProfiler" name="MvcMiniProfiler.Data.ProfiledDbProvider" />
    </DbProviderFactories>
</system.data>

We just register DB provider for MvcMiniProfiler and everything keeps working. :)

Could not load file or assembly ‘<some assembly name>’ or one of its dependencies. An attempt was made to load a program with an incorrect format.

Today I stuck with the following problem: when I run my web project through local dev server or IIS Express — everything is fine, but when I host it through IIS, I get “Could not load file or assembly ‘<some assembly name>’ or one of its dependencies. An attempt was made to load a program with an incorrect format.”

One crucial detail: my server is 64-bit.

The solution is in application pool settings: in advanced settings of AppPool, set Enable 32-bit applications to true.

Silverlight memory leaks finding

Quick post to not forget :)

Article (mentions x86 WinDbg installation under x64 OS): http://dotnetspeak.com/index.php/2010/09/using-windbg-to-find-memory-leaks-in-silverlight-applications/

Also quick cheatsheet:

.load C:\Program Files (x86)\Microsoft Silverlight\4.0.60129.0\sos.dll
!dumpheap -stat
!dumpheap -MT <mt>
!gcroot <addr>

Slow Visual Studio 2010 tab closing

If you experience this issue try to add your project’s folder to your antivirus location exception. I have Microsoft Security Essentials and this slowness irritated me for quite some time until I accidentally found on some forum this advice. And it really helped me (after reboot, actually, but I think it depends on antivirus).

Hope it helps you as well.

Update: just found interesting discussion on VS performanve optimizations on StackOverflow.com: Visual Studio Optimizations.

Kubuntu stuff

I recently decided to completely switch to Kubuntu as my main OS and while it is really user-friendly comparing to the dark times when I first tried *nixes, still it lacks tiny bits and some tuning, so I’ll gather different useful links to posts, tutorials and how-to’s that helped me to make work and “living” in Kubuntu more pleasant.

Update from 16 Aug 2010:

Recently I updated to KDE SC 4.5. I won’t describe changes as they are described in a lot places, but I had some regular crashes from Nepomuk service, so I have disabled it… It’s not hard :)

Also there were troubles with KPackageKit: it saw all the new KDE SC 4.5 version packages, but categorized them as Blocked updates. I wonder what it is. Still, I wanted to update my system quite badly, so Google to the rescue! :) Here is a comfortable two-command line to install all available updates:

  • sudo apt-get update && sudo apt-get dist-upgrade 

Silverlight and tcp.net bindings for WCF

I’m working with Silverlight 4 on my work for quite some time (from early betas).

And I was wondering about new cool feature of the Silverlight 4 - tcp.net binding. But it seems using it is quite more involved than BasicHttpBinding, but still - I want to understand it and use on my Silverlight project. Here I’ll post some useful links I’ve found - they are for me, just to not google for them one more time. I plan to come back here after IIS Express is out for beta testing (because one can’t use tcp.net with integrated in VisualStudio ASP.NET web-server, but I don’t want to install heavy-weight IIS7 on my working machine).

So, here are those links:

I’ve finally made it! Now I can read distance to the closest object with the help of ultrasonic range finder Devantech SRF10. It took two days to make it work and communicate (actually it is due to my own stupidity…) and a lot of help from my father! Thanks, dad! 

On this video you can see how things look like now. You can see that I reworked LEDs so they look much more solid :) and representable. Also I put all DB-9 contacts into cases, so it is much easier to unplug them, and quite a lot safer to use them because you don’t need to worry about accidental short circuit.

I tried to demonstrate the work of sonar. You can see that LEDs can work in to modes, which I can switch with that tiny black button (so called “soft button”) on Command module. In the first of them LED-console shows a demonstrably a distance to the object: the furthest the object - the more LEDs are on. Seems very natural to me and what about you? As you can somehow see I move my hand closer of farther to/from range finder and LEDs dynamically (they refresh about three times per second right now, but no problem to make them refresh 15 times and even more often) change their “level of signal”.
The second mode represents the measured distance in binary format on LED-console with the least significant bit represented by the closest to us LED. The value of distance is in centimeters. This mode is handy to check a robustness and accuracy of sonar. As you can see it is actually not to consistent, it returns for almost the same situation quite different values. But I think the situation can be improved by playing with internal settings of range finder. I didn’t get into that yet, though, but it will be my next logical step and I’ll report of the results. Ok, thats all for today, enjoy this video and don’t forget that it is very pleasant to get some feedback from you, people!