Sergey Maskalik

Sergey Maskalik's blog

In the pursuit of mastery

First of all MiniProfiler is awesome! And lucky for me some smart people made it work with WCF. Since there is no official guide on setting up the project I had to get the examples and work from there. Here is what I had to do in order to get it working.

There is no Nuget package for MiniProfiler.WCF library so you have to download source code from GitHub and compile it under Release configuration. Then take MiniProfiler.dll and MiniProfiler.WCF dlls and place them in your common folder where you keep your external libraries.

Service setup

Next, in your WCF Service project register wcfMiniProfilerBehavior extensions under extensions element (you can find out the version number with right click and going to properties on the MiniProfiler.Wcf.dll file. )

...
    <extensions>
      <behaviorExtensions>
        <add name="wcfMiniProfilerBehavior" type="StackExchange.Profiling.Wcf.WcfMiniProfilerBehavior, Miniprofiler.Wcf, Version=2.0.4.0, Culture=neutral" />
      </behaviorExtensions>
    </extensions>
 </system.serviceModel>

And in your WCF Service project you need to add the following endPointBehavior under behaviors element.

<behaviors>
...
      <endpointBehaviors>
        <behavior>
          <wcfMiniProfilerBehavior />
        </behavior>
      </endpointBehaviors>
</behaviors>

Oh yea and don’t forget to reference a MiniProfiler.Wcf in your Service Project. You will need to register MiniProfiler assembly in other projects where the actual profiling is going to take place, for example your data layer would have a reference to that file and wrap a connection with a dbprofiled connection, for more setup details see the official site.

Client Setup

If you are using proxies for your client setup you would register the following configuration in your client’s web.config.

  <system.serviceModel>
    <extensions>
      <behaviorExtensions>
        <add name="wcfMiniProfilerBehavior" type="StackExchange.Profiling.Wcf.WcfMiniProfilerBehavior, Miniprofiler.Wcf, Version=2.0.4.0, Culture=neutral" />
      </behaviorExtensions>

And behaviors

<behaviors>
  <endpointBehaviors>
    <behavior>
      <webHttp/>
      <wcfMiniProfilerBehavior />
    </behavior>
  </endpointBehaviors>
</behaviors>

Just like in the Sample Project.

If you are using ChannelFactory without generating proxies you would need to add a behavior in the following way, for example:

ChannelFactory = new ChannelFactory<T>(Binding, Endpoint);
ChannelFactory.Endpoint.Behaviors.Add(new WcfMiniProfilerBehavior());
Channel = ChannelFactory.CreateChannel();

That’s almost it, just add the standard initialization to Application_BeginRequest and etc. mine looks like this

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    if (Request.IsLocal)
    {
        MiniProfiler.Start();
    }
}

protected void Application_EndRequest(Object sender, EventArgs e)
{
    MiniProfiler.Stop();
}

private void InitProfilerSettings()
{
    // some things should never be seen
    var ignored = MiniProfiler.Settings.IgnoredPaths.ToList();
    ignored.Add("WebResource.axd");
    ignored.Add("/Styles/");
    MiniProfiler.Settings.IgnoredPaths = ignored.ToArray();

    MiniProfiler.Settings.SqlFormatter = new StackExchange.Profiling.SqlFormatters.SqlServerFormatter();
}

Also, need to add handler activation into web.config under system.webserver

<system.webServer>
...
<handlers>
<add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" 
type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />
</handlers>

Finally, adding Mini Profile rendering scripts in your master page.

<%= StackExchange.Profiling.MiniProfiler.RenderIncludes() %>

Regular expressions are very handy especially when you are refactoring and there is a lot of manual changes. Visual Studio Replace with regex came in really handy when porting our code base at work to use MiniProfiler. Since MiniProfiler returns a DbConnection rather than SqlConnection, there are few small shorthands that no longer worked with DbConnection.

The following example will work with SqlCommand but would not work with the DbCommand because DbCommand.Parameters doesn’t expose a Value property which is a short hand notation that returns the last SqlParameter Value.

using(var cn = SqlTools.GetSqlConnection("Default"))
{
    var cmd = cn.CreateCommand();
    cmd.Parameters.Add(new SqlParameter("@Password", SqlDbType.NVarChar, 128)).Value = password;

So I needed to modify this code in about 650 places to use object initializer like this:

cmd.Parameters.Add(new SqlParameter("@UserName", SqlDbType.NVarChar, 256) { Value = username });

With a little bit of research I wrote a regular expression for Visial Studio Replace to fix this issue. Which looks like this:

Find what: new SqlParameter\({[^)]*}\)\)\.{[A-Za-z]+} = {[^\}]+};

Repleace with what: new SqlParameter(\1) { \2 = \3 });

And vua-lah!, it is all fixed.

One important feature to notice is that you use curly braces to mark the values that you are extracting from the string, this is different from other flavors where you use parenthesis.

Update 04-23-2013

Visual Studio 2012 is using a different syntax to capture strings. The above expression would have to be slightly modified, as follows:

Find what: new SqlParameter\(([^)]*)\)\)\.([A-Za-z]+) = ([^\}]+);

Repleace with what: new SqlParameter($1) { $2 = $3 });

It’s sad to see many professional programmers typing with two or three fingers. Like in any profession you should become a master of your craft, and for us developers touch typing is a basic skill that you must master. Like a chef who has never learned how to properly prepare vegetables it’s not efficient, makes your already difficult profession harder and just not professional.

When I first started my programming career I have to admit I was pretty mediocre. Even though I could type about 45 words per minute (WPM) I was still typing with 3 fingers and looking at the keyboard. In the beginning typing uncommon characters made me feel awkward and frustrated and my WMP went out the window. I think my real WMP while writing code was about 10-13. I think i really decided to take the bull by the horns when I realized that it actually interfered with my coding. I’m not good at multitasking and when I write my code I need to keep in the flow of one task and that thinking about task at hand. Looking at the keyboard while typing will slow you down and slow down the flow of your thoughts and eventually your brain will get frustrated and get distracted.

It’s pretty amazing how much such a basic skill of touch typing can improve the quality of your career. I can tell that it made my job a lot more pleasurable. I actually look forward to writing code since it doesn’t require much effort. Also, I can tell that I’m relying on IDE much less and it just feels great.

For me working doing 15 minutes of practice every day worked magic, but you gotta stick with it for at least for couple weeks. Also don’t cheat yourself and making sure every finger is doing it’s proper job. You will actually see results pretty fast.

So I challenge you to take 15 minutes out of your day and head over to Typing for developers and stick to taking lessons for about 2 weeks.

That’s it and enjoy!