ngeor.net | blogProudly under construction since 2010

25Oct 2011 Continuous Deployment with a Windows Service project

For a long time now I had a long running process here at home implemented as a Windows Console application. The application was quite stable, but from time to time I had to fix a bug or add a new feature. Before deploying a new version, I would have to login to the deployment machine where the application was running and terminate it, otherwise deployment would fail trying to copy over the new files. Quite tedious. And then I would have to start it again, as soon as the deployment server had finished its job. I like to automate this kind of things.

Converting the application to a Windows Service is something I should have done in any case. But it turns out it also solves this particular problem too. Say hello to the servicecontroller task in NAnt (which, in case you didn't notice, reached version 0.91 finally a couple of days ago). Using this task in your build script, you can start and stop any Windows Service you want. What I did was to modify my build script to stop my Windows Service just before it copies the files and then start it again when the copying is done. All of that done through the build server, no manual steps required.

Oh, by the way, I used SharpDevelop to create the Windows Service. I only have the free Express edition of Visual Studio at home and that one doesn't support Windows Service projects. Turns out that SharpDevelop supports that and has some nice features too that Visual Studio doesn't even support without Resharper, for instance easily running and debugging NUnit unit tests from within the IDE (ok to be fair you can attach the Visual Studio debugger to the NUnit GUI and debug your unit test, but not in the Express edition).

SharpDevelop also seems to support wix installers, but that's something I didn't try, perhaps some other time.

23Oct 2011 WCF and deferred execution - where is my Exception?

A long time ago, I blogged about WCF and deferred LINQ queries and some surprises that combination may have. Back then, our WCF service would crash inexplicably when the returning type of an operation would contain an enumerable whose evaluation was deferred until after the WCF operation was out of scope.

Today I'll revisit the same problem with a different approach: the missing exception.

Consider this small WCF service implementation:

public class DemoService : IDemoService
{
    public IEnumerable<string> GetNames()
    {
        try
        {
            return DoGetNames();
        }
        catch (Exception ex)
        {
            // TODO: Log exception
            return Enumerable.Empty<string>();
        }
    }
}

The implementation is hidden in the DoGetNames method, which we'll see in a moment. I had a similar implementation in a project recently, and I thought that the try catch statement here would protect me from all unexpected exceptions that may occur in DoGetNames. However, that wasn't the case. Once again, the cause was deferred execution. Let's see a very basic implementation of DoGetNames that can cause this problem:

private IEnumerable<string> DoGetNames()
{
    yield return "Alice";
    yield return "Bob";
    throw new ApplicationException("I am throwing an exception");
}

Because DoGetNames is implemented with a deferred execution iterator, it really doesn't get invoked until after the execution leaves the try catch block. So not only our exception is not logged, but on top of that the client's call fails. I had spent some time wondering why on earth my exception was not been logged at all... until I thought to start searching for these kind of cases.

So if you're thinking that you've safe guarded your WCF operations with a simple try catch, make sure you haven't missed any sneaky LINQ statements or similar code that is lazily executed.

Hope this helps.

Tags:
Categories: Coding

21Jul 2011 Overriding EPiServer pages

It is not very frequent or pretty but still sometimes it’s inevitable: we have to “steal” the code of EPiServer through Reflector/ILSpy/other and modify it to suit our needs. This way you can customize pages or user controls anyway you wish, beyond the way EPiServer allows through conventional channels.

The process is simple. Through Reflector or ILSpy you "steal" the code of the page you want to override. You compile that class, with a different namespace, into a new assembly. Then, you modifiy the markup file to point to your new class. That's it.

Let's see an example, let's say you want to customize the NewPage.aspx that EPiServer provides. What would you do:

  1. Open NewPage.aspx in a text editor to figure out what class it inherits from ( e.g. EPiServer.UI.Edit.NewPage )
  2. Open the assembly that contains that class in Reflector or ILSpy ( typically the assembly is EPiServer.UI.dll - at least for EPiServer 5 that I'm currently using )
  3. Find the class and save it into a C# code file.
  4. Edit this class in a text editor and change the namespace.
  5. Compile this class into a new assembly and copy the assembly in EPiServer's installation bin folder.
  6. Edit NewPage.aspx and change the class it inherits from, setting it to your new class.

At this point, the NewPage.aspx should still work but now the code is yours to do as you please.

Hopefully you won't have to do many of these hacks. But regardless of that, you should be careful on the maintenance of your code. An idea could be that you keep all of these "stolen" classes in a separate assembly. Also you could mirror the namespace structure of the classes you're "stealing", so if you're taking the code from the class EPiServer.UI.Edit.NewPage, you can place its copy at MyProject.UI.Edit.NewPage. This way of organizing the “stolen” code offers the advantage that we know immediately the original location of the class we “stole” from, in case we need to check back for the original. It also removes the need of thinking where the “stolen” code should be placed and serves as a straight forward convention for the developers to use.

Another good idea is to add comments in the code about all the things you change and why you are doing it. These hacks should be justified and easy to isolate. Otherwise upgrading to the next EPiServer version might become difficult. You should know what your hacks do and why you implemented them in the first place; maybe the next EPiServer version will have them as built-in features thus making your hacks obsolete. And if you still need the hack, you should be able to identify and isolate the customized part of the code in order to try and reapply the hack in the next version.

Hope this helps.

Categories: Coding

20Jul 2011 WCF, JSON and the DateTime

I'm playing with a WCF service that works with JSON. If you haven't done this already, it's fairly easy. In the web.config, make sure your service is using the webHttpBinding binding. Also you'll need an endpoint behavior that looks like this:

<endpointBehaviors>
  <behavior name="MyBehavior">
    <webHttp />
  </behavior>
</endpointBehaviors>

 

Also, you'll need to annotate your operation methods with an attribute that tells WCF to use JSON (otherwise it uses XML):

[OperationContract]
[WebInvoke(
Method = "POST",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]

 

That's it.

I had some problems however with the DateTime type. Unfortunately JSON doesn't have a standard for dates so Microsoft chose its own way. Basically it looks like "/Date(1224043200000)/"; the number represents the count of milliseconds since epoch.  So when you're consuming your service, you'll have to deserialize this format yourself. You can find some suggestions here, I picked the new "Date(parseInt(jsonDate.substr(6)))".

So if the date is represented by the count of milliseconds since epoch, what happens with older dates? This gave me a headache today, trying to figure out why my service wasn't returning anything... I had forgotten some DateTime set to its default value, which in .NET is 0000-00-00 00:00... I wasn't getting any error message whatsoever so I was troubleshooting blindly and the date was the last place to look. Usually it's some object referencing some other object that shouldn't or something like that...

Another promising solution is to use the Json.NET library which offers a custom date time formatting which is more human readable, but I didn't try it out, I just made sure all my dates where properly set.

Hope this helps.

Categories: Coding

12Jul 2011 MasterType directive and namespace conflicts

Let me start by saying that I hope you won't run into this one and that you probably won't.

In ASP.NET you can use the MasterType directive in your pages to have a strongly typed reference to your master page class. So if you add this directive on top of your page:

<%@ MasterType VirtualPath="~/MyMasterPage.master" %>

You page now has a strongly typed reference to your master page. The Master property has been overriden and now you can access your master page's properties and methods directly:

protected void Page_Load(object sender, EventArgs e)
{
  // assuming our master page has a property called MySiteName
  Title = Master.MySiteName + " - Home Page";
}

Behind the scenes, it's the automatically generated designer code that makes this possible. The MasterType directive causes the Master property to be overriden in the aspx.designer.cs file:

public new MyWebApp.MyMasterPage Master
{
  get
  {
    return (MyWebApp.MyMasterPage)base.Master;
  }
}

So far so good. There's one rare scenario that can break this nice setup. If the root namespace element is repeated somewhere inside the namespace of the master page, the automatically generated code doesn't compile anymore. I think an example is probably needed here to see what I am talking about.

If your fully qualified type name of your master page is for example MyWebApp.Templates.MyWebApp.MyMasterPage, the code doesn't compile anymore because of the automatically generated code:

public new MyWebApp.Templates.MyWebApp.MyMasterPage Master
{
  get
  {
    return (MyWebApp.Templates.MyWebApp.MyMasterPage)base.Master;
  }
}

This doesn't compile anymore, because the "MyWebApp" will be interpreted as "MyWebApp.Templates.MyWebApp" and it is actually trying to find MyWebApp.Templates.MyWebApp.Templates.MyWebApp.MyMasterPage ! Amazing. Had the auto generating tool emitted a simple "global::" in front of the type, this would have worked.

Technically, we can blame the auto generated code. The big question is why would you want to setup your namespaces like that and I have no answer to that. I can only say that I've seen this one too.

You might think you can win this hopeless situation by using the variation of the MasterType directive with the TypeName attribute.

<%@ MasterType TypeName="MyWebApp.Templates.MyWebApp.MyMasterPage" %>

instead of specifying the virtual path, we specify the type of our master page. This doesn't make the situation any different. But you might try to specify the global prefix or the correct type yourself:

<%@ MasterType TypeName="global::MyWebApp.Templates.MyWebApp.MyMasterPage" %>

or

<%@ MasterType TypeName="MyWebApp.MyMasterPage" %>

Surprisingly, this will get your code to compile! The TypeName attribute gets copied as-is directly to the designer code file so both these options pass compilation. However, now the page will throw an exception at run time, complaining that the type can't be found... it's a lose lose situation. Just don't set-up your namespaces like that, not just because of this problem, but for sanity's sake.

Categories: Coding

5Jul 2011 An ode to the objects that are born to die

Sometimes we're coding without putting too much thought in it. It's just one more line of code, an innocent line of code. But how many times have you not found a bug that comes down to fixing a single line of code?

To be more specific, I'd like to say some things about the thoughtless instantiation of objects we all do. When fine tuning the performance of an application, one of the important things to notice is how many objects you create and if you can somehow reduce them. The reason is that objects take up some memory and some CPU cycles from the garbage collector that needs to clean up the mess.

Consider this piece of code:

if (url.ToLower() == "/homepage") {
  // Do something clever
}

 

There are two things wrong here. First, the ToLower method is dependent on the current culture. The Invariant Culture should be prefered instead. Also, according to MSDN, ToUpperInvariant is actually more reliable that ToLowerInvariant so strings should be normalized to uppercase.

But the code does something even worse. It is creating a new string object with that call to ToLower, just to do the equality comparison and throw it away. There's a better way to do it:

if (string.Equals(url, "/homepage", StringComparison.OrdinalIgnoreCase) {
  // That's better
}

 

Since that comparison is case insensitive, you can chose between StringComparison.OrdinalIgnoreCase and StringComparison.InvariantCultureIgnoreCase. The first is supposed to be faster, as it is the old school byte to byte comparison but it might not be appropriate for the problem at hand. You can read more in this stackoverflow discussion. But in any case, you just saved the life of a string object!

Another thing I've seen (and done) is driven by excessive love for syntactic sugar. Well, too much sugar makes you fat and syntactic sugar is no different. I'm talking about objects that implement IDisposable just for the pretty using statement but without themselves owning any unmanaged resources or any other IDisposable. Consider this example:

class MyClass
{
  private bool busy;

  public void Start()
  {
    busy = true;
  }

  public void Stop()
  {
    busy = false;
  }
}

class Program
{
  MyClass myClass = new MyClass();
  myClass.Start();
  // do some work
  myClass.Stop();
}

 

There's some class that the main program is using. There's a Start and a Stop method and some statements in between. The requirement is that the Stop method must always be called, even if an exception occurs in between. If you're thinking "try - finally", then you haven't seen this one:

class MyClass
{
  private bool busy;

  public void Start()
  {
    busy = true;
  }

  public void Stop()
  {
    busy = false;
  }
}

class MyClassHelper : IDisposable
{
  private MyClass instance;

  public MyClassHelper(MyClass instance)
  {
    this.instance = instance;
    instance.Start();
  }

  public void Dispose()
  {
    instance.Stop();
  }
}

class Program
{
  MyClass myClass = new MyClass();
  using (MyClassHelper syntacticSugar = new MyClassHelper(myClass))
  {
    // do some work
  }
}

 

This is what happens when a C# developer realizes that the using block is a glorified try - finally block and decides it's cool to replace all try - finally blocks with using statements... you can spare the life (and the very class definition) of the MyClassHelper object by using the humble try - finally block and leave the using for the real IDisposables that are releasing COM+ objects, database connections, nuclear reactors, that sort of things. Like this:

class MyClass
{
  private bool busy;

  public void Start()
  {
    busy = true;
  }

  public void Stop()
  {
    busy = false;
  }
}

class Program
{
  MyClass myClass = new MyClass();
  myClass.Start();
  try
  {
    // do some work
  }
  finally
  {
    myClass.Stop();
  }
}

 

Hope this helps.

Categories: Coding

2Jul 2011 IIS 7 gives 404.17 error with svc WCF services

If you try to access a svc service hosted in IIS and you get the following error:

HTTP Error 404.17 - Not Found - The requested content appears to be script and will not be served by the static file handler.

the cause is most likely misconfigured handler mappings for the svc file extension. The correct mapping for the svc file depends on the application pool's .NET version and managed pipeline mode.

There is an easy way to restore the correct mapping for the svc file extension. If you're using .NET 2.0 in your pool, you can run the command "ServiceModelReg.exe -i" from the folder "C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation". If you're using .NET 4.0, you can run the command "aspnet_regiis.exe -i" from "C:\Windows\Microsoft.NET\Framework\v4.0.30319".

There's a catch here. If your IIS hosts both .NET 2.0 and .NET 4.0 sites, .NET 4.0 settings will disappear as soon as you run the ServiceModelReg.exe command and your .NET 4.0 site won't work anymore. If that's the case, make sure you run first the ServiceModelReg.exe command and after that's done run the aspnet_regiis.exe.

Categories: Coding

9Jun 2011 Exceptions using type initializer based singletons

A popular web page describing how to implement a singleton in C# is this one, where several ways to implement a singleton are discussed. I usually pick the last option, which, according to the author of that page, has the most benefits. The author goes on to mention some problems that can occur if an exception is thrown in the constructor, but I never paid attention to that until recently.

In our project at work, we implemented a small singleton based on that last option, the one using type initializers. The purpose of the singleton was to fetch some records from the database (around 200 of them) and cache them in memory. The call to fetch the records from the database was done in the constructor of the singleton, so our code looked more or less like this:

public sealed class Singleton
{
    private Dictionary cache;

    Singleton()
    {
        // access database
        cache = RetrieveData();
    }

    public string GetValue(string key)
    {
        return cache[key];
    }

    private Dictionary RetrieveData()
    {
        // access the heavy database query
        return data;
    }

    public static Singleton Instance
    {
        get
        {
            return Nested.instance;
        }
    }
    
    class Nested
    {
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static Nested()
        {
        }

        internal static readonly Singleton instance = new Singleton();
    }
}

Most of the above code is the boilerplate singleton code. Notice that in the constructor of the Singleton class, the database is been accessed.

What will happen is the database is out of reach temporarily? We were experiencing some SQL timeouts. I thought that once the SQL problems had been resolved, .NET would be able to create the singleton normally. I found however that this isn't the case. Once an exception is thrown in the type initialization, the exception just stays there and gets thrown for ever and ever. The exception never goes away. Our database logs got many error rows about the same exception, which kind of gave it away. Since the singleton was accessed in the URL rewriter of our site, it basically made the entire site give the 500 page until somebody restarted IIS. Ouch.

The bug however didn't occur that often. First of all, we didn't have that many database problems. Secondly, the database problem would have to occur at the same time when the singleton was initialized. In the entire life span of the web application (and therefore the singleton), that's a bit improbable. Yet, it happened. Twice. But it got noticed so here's what I did to solve this.

This is a different way to write the class. The same singleton pattern is used while the class doesn't suffer from this problem:

public sealed class Singleton
{
    private Dictionary cache;
    private object cacheLock = new object();

    Singleton()
    {
    }

    public string GetValue(string key)
    {
        EnsureData();

        return cache[key];
    }

    private void EnsureData()
    {
        if (cache == null)
        {
            lock (cacheLock)
            {
                if (cache == null)
                {
                    cache = RetrieveData();
                }
            }
        }
    }

    private Dictionary RetrieveData()
    {
        // access the heavy database query
        return data;
    }

    public static Singleton Instance
    {
        get
        {
            return Nested.instance;
        }
    }
    
    class Nested
    {
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static Nested()
        {
        }

        internal static readonly Singleton instance = new Singleton();
    }
}

What did we change? The constructor is now empty. That means that the singleton pattern is now responsible only for one thing: initializing the singleton in a thread safe way. Nothing more, so nothing can go wrong with the type initialization. So we got rid of the "permanent" exception problem.

We still need to load the cache in a thread safe way. That is been done with the EnsureData method. If the cache hasn't been populated yet, we lock on the helper cacheLock object to ensure only one thread can continue from there on. We check again that the cache is still null and then we access the database. This technique is called the Double-checked locking. With this implementation, SQL timeouts will end up to the 500 page as expected, but once the database is back online the site will resume working normally.

If you would like to see it in action, you can paste the following code in a demo web application. In your Default.aspx Page_Load, access the Singleton.Instance.Greet method. See that "DateTime.Now.Minutes < 30" line? Change it to something that will throw an exception, so if the time is 11:47, make it for instance "DateTime.Now.Minutes < 50". When you load the Default.aspx, you will get an exception with the date time as an error message. Loading the page over and over again will still show the same date and time when the exception first occurred and the exception will keep on occurring, even after the time has passed 11:50.

public sealed class Singleton
{
    private Dictionary cache;

    Singleton()
    {
        if (DateTime.Now.Minutes < 30)
        {
            throw new Exception(DateTime.Now.ToString());
        }
    }

    public string Greet()
    {
        return "Hello world";
    }

    public static Singleton Instance
    {
        get
        {
            return Nested.instance;
        }
    }
    
    class Nested
    {
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static Nested()
        {
        }

        internal static readonly Singleton instance = new Singleton();
    }
}

It's a tricky situation, one of those .NET features that you probably only get to know the hard way. Hope this helps.

Categories: Coding

6Jun 2011 How to use SQLite with Mono and Fluent NHibernate

So I have setup my project playing nice in Windows .NET 4.0 with SQLite and Fluent NHibernate. I opened it up from my Mac to see if it would still work and, big surprise, it didn't. It complained about System.Data.SQLite.dll not found, even though the dll was there.

I struggled a bit with this; the official page is quite bad and what's more than that it doesn't offer precompiled dlls but some installer. Searching a bit more, I found that Mono has its own implementation at assembly Mono.Data.Sqlite. I removed the reference to System.Data.SQLite and referenced Mono's assembly... still nothing. The problem is that Fluent NHibernate was still trying to find System.Data.SQLite.dll. Apparently something was missing from this piece of my code:

private IPersistenceConfigurer ConfigureSQLite()
{
  return SQLiteConfiguration.Standard.UsingFile(DbFile);
}

Well I was very lucky at this point because I found this article that explains how to tell NHibernate to use Mono.Data.Sqlite. The first part, which is to include the small driver class MonoSqliteDriver that works with Mono.Data.Sqlite, stays the same. I still had to that. But with Fluent NHibernate the second part is done in code (and not configuration) like this:

private IPersistenceConfigurer ConfigureSQLite()
{
  return SQLiteConfiguration
      .Standard
      .Driver<MonoSqliteDriver>()
      .UsingFile(DbFile);
}

And that's it! Hope this helps.

Categories: Coding

5Jun 2011 Thread-safe code

Consider this code:

if (!dictionary.ContainsKey(key)) {
  dictionary.Add(key, value);
}

In a multithreaded environment, thread A and B may check for the existence of the key at the same time and both determine that the key doesn’t exist. Then, they both try to add it. One of them gets the ArgumentException about the key already been there, because the other thread just added it.

Thread A Thread B
1 ContainsKey (returns false) ContainsKey (returns false)
2 Add Add

.NET collections are not synchronized as you know. So the above table shows the two threads happily running in parallel.

What I saw in my project's code is that in some cases the problem was "solved" by using a ThreadSafeDictionary in place of the standard .NET Dictionary class. This custom ThreadSafeDictionary class has synchronized methods. However, this doesn’t actually solve the problem. Consider these possible execution paths:

Thread B gets exception:

Thread A Thread B
1 ContainsKey (returns false) Blocked by Thread A
2 Waits for system to resume Thread A ContainsKey (return false)
3 Add (ok) Blocked by Thread A
4 Add (exception)

No exception scenario (if you’re lucky):

Thread A Thread B
1 ContainsKey (returns false) Blocked by Thread A
2 Add (ok) ContainsKey (return true)

Thread A gets exception:

Thread A Thread B
1 ContainsKey (returns false) Blocked by Thread A
2 Waits for system to resume Thread A ContainsKey (return false)
3 Blocked by Thread B Add (ok)
4 Add (exception)

Another one:

Thread A Thread B
1 ContainsKey (returns false) Blocked by Thread A
2 Add (ok) ContainsKey (return false because Add of Thread A hasn’t quite finished yet)
3 Add (exception)

So the problem is not solved. Why? While the ContainsKey and Add methods of the ThreadSafeDictionary are thread-safe on their own,  the business logic that they create combined isn’t. What you need to do, is to lock your entire block of code that needs to be ran by one thread at a time. This is called the Critical Section. The above code should be written like:

lock (synchronizeObject) {
  if (!dictionary.ContainsKey(key)) {
    dictionary.Add(key, value);
  }
}

This way you lock the entire code block for single thread access and you ensure your data integrity. And, the critical section should be as big as necessary but not more.

The issue becomes more serious for structures that have a long life time (e.g. application scope), which increases the chances of failure over time.

Categories: Coding

Recent Comments

Comment RSS

Tag cloud

Month List

Log in