Thursday 26 December 2013

The process was terminated due to an internal error in the .NET Runtime


If your .NET process crashes unexpectedly with the following message in the Event Log: "The process was terminated due to an internal error in the .NET Runtime...", there are a few things we can do to investigate it. Unfortunately, the exception already occurred, the process crashed and there is nothing interesting in the logs. First of all we'd like to find out what happens that causes to this crash and where in the code it happens. In order to do this we can start by downloading and installing Debug Diagnostic tool. Then configuring it in the following matter:

Select Crash, because that's what we're investigating:


Select a specific process, because we know what process crashes:


Select the process you want to investigate:


Select "Full Userdump":

With the DebugDiag running in the background, reproduce the scenario that causes the initial process termination. When the process crashes, we'll find in DebugDiag log folder with a few files. The log folder is
C:\Program Files\DebugDiag\Logs
The interesting file is the one with .DMP extension. Double click it and it will be opened in DeBug Diagnostic Analysis. It will convert the DMP file to HTML report and will open it in IE. Yes, yes, IE, even if your favorite browser is Chrome. Ok, ok, just give me the call stack

We are looking for any errors, crashes in that HTML log. In my case I found "access violation" exception and call stack of the exception. Surprisingly, the call stack is pretty obvious code. It accesses a property which is null, so I would expect a NullReferenceException. But it was access violation. What is more interesting, is that that piece of code is surrounded by try/catch block.

Starting from .NET 4.0, access violation exceptions and similar are not thrown in the regular way. They just terminate the process and are not caught in try block. But there are two still to catch them:
  • 1. Using attribute HandleProcessCorruptedStateExceptions on the suspicious method
  • 2. Using <legacyCorruptedStateExceptionsPolicy enabled="true" />
Below code demonstrates how to apply HandleProcessCorruptedStateExceptions attribute on suspicious method. It enables catching the access violation exception.
 

    internal class Program
    {
        private static void Main(string[] args)
        {
            var crashingThread = new Thread(CrashingThread);
            crashingThread.Start();

            Console.ReadKey();
        }

        [HandleProcessCorruptedStateExceptions]
        private static void CrashingThread()
        {
            try
            {
                CrashMe();
            }
            catch (Exception exception)
            {
                Console.WriteLine("I caught access violation");
                Console.WriteLine(exception.ToString());
            }
        }

        private static unsafe void CrashMe()
        {
            unsafe
            {
                int read = *((int*) long.MaxValue - 8);
            }
        }
    }



A few last notes about that attribute:
It can be applied on any level on the call stack of the suspicious code. Even on the Main method. However it won't work if you apply the attribute on the main method, but the exception occurs in another thread. It should be applied on the method that is on the crashing thread!

Please note, that it is not recommended to leave that attribute in production code. It should be used only for debugging purposes.

Enjoy your debugging!

Wednesday 11 December 2013

BizArk.Core 2.0.9 was pushed to NuGet

Bizark.Core 2.0.9 was pushed to NuGet

Release notes:
9151 Empty argument prefix does not work and does not

Example:
 
[CmdLineOptions(ArgumentPrefix = "")]
internal class MyCmdLineObject: CmdLineObject
{
     [CmdLineArg]
     public string Name { get; set; }
}
Can be used like this from command-line
MyConsoleApplication.exe Name Boris

In conjunction with ArgumentDelimiter:
 
[CmdLineOptions(ArgumentPrefix = "", ArgumentDelimiter='=')]
internal class MyCmdLineObject : CmdLineObject
{
     [CmdLineArg]
     public string Name { get; set; }
}
From command line
MyConsoleApplication.exe Name=Boris

Also supports string surrounded by double quotes:
 
[CmdLineOptions(ArgumentPrefix = "", ArgumentDelimiter='=')]
internal class MyCmdLineObject : CmdLineObject
{
     [CmdLineArg]
     public string Path { get; set; }
}
MyConsoleApplication.exe Path="C:\Program Files (x86)\Microsoft.NET\"
The value of Path will be @"C:\Program Files (x86)\Microsoft.NET\" without the leading and ending double quotes

Get from nuget:

PM> Install-Package BizArk.Core

Wednesday 4 December 2013

Goodbye Autofac

Autofac is one of my favorite IoC containers, due to its fluent, easy-to-learn syntax and great performance. However recently it let me down in some occasions and I had to get rid of it in the main project, I am working on. But let me explain it from the beginning.

Since Autofac 3.0, it is compiled against Portable .NET Framework. It is a subset of the usual .NET Framework, which runs on various devices and platforms. Such as Silverlight, mobile etc. The portable framework version number is 2.0.5.0. So far, so good. It worked well in my project with .NET Framework 4.5. But...

1. When we tried to obfuscate Autofac.dll using Dotfuscator version 4.8, it complained that mscorlib.dll version 2.0.5.0 cannot be found. Although it exists in the corresponding directory under C:\Windows\Microsoft.NET\Framework . Travis Illig suggested me to upgrade the Dotfuscator to latest version, which would definitely resolve the problem.

From Dotfuscator v 4.9 release notes:
  • Using String Encryption on Portable Class Libraries will no longer cause Windows Store apps to fail certification.
http://www.preemptive.com/support/dotfuscator-support/dotfuscator-pro-change-log/464

Unfortunately, upgrading Dotfuscator means purchasing a new license, which is pretty expensive. We managed to overcome this problem by putting mscorlib.dll v2.0.5.0 in one of the internal directories. Dotfuscator scans directories and managed to find it successfully.

2. The second problem appeared when we started testing the product on clean machines. We get the following exception, despite the fact that Portable Framework exists on the machine:

Could not load file or assembly 'System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes' or one of its dependencies. 

It happens as a result of Microsoft Known Bug KB2468871. It was fixed by the Redmond folks, but the relevant update is not installed on the machine.

So it means, that in order to use Autofac we have to required from our customers to install Windows Updates. Sometimes it might cause issues with corporate customers, that have strict policy regarding machine updates. It was decided that we'll stop using Autofac in this major project and will switch to something less "sensitive".

Nevertheless, we still use Autofac in many other internal projects. So it is not a real "Goodbye"




Wednesday 13 November 2013

Opt in and opt out using Autofac IoC container

I am a big fan of dependency injection and inversion of control. I strive to use these techniques in almost every project I am involved with. Usually when I develop the project from the ground, the whole design is ready for automatic types scanning and auto wiring. So it is very easy to wire them up using a short piece of code. All the examples in this post will be using Autofac - the addictive IoC container.
 
public IContainer Initialize()
{
    var builder = new ContainerBuilder();
    builder.RegisterAssemblyTypes(GetType().Assembly).AsImplementedInterfaces();
    return builder.Build();
}

But sometimes, I need to prevent some classes from being registered, for example, the instance need to be created in run time with specific context parameters. In this case I use the opt-out, i.e. register all the types except those having a custom attribute:
 
[AttributeUsage(AttributeTargets.Class)]
public class DontRegisterInContainerAttribute : Attribute
{

}

public IContainer Initialize()
{
    var builder = new ContainerBuilder();
    builder.RegisterAssemblyTypes(GetType().Assembly)
        .Where(t => !t.HasAttribute<DontRegisterInContainerAttribute>())
        .AsImplementedInterfaces();
    return builder.Build();
}
In order to use the attribute in this syntax, I am using an extension method:
 
public static class TypeExtensions
{
    public static bool HasAttribute<T>(this Type @this)
    {
        return @this.GetCustomAttributes(typeof (T), false).Any();
    }
}

In some projects, when I am modifying existing code, which is not "IoC-ready", I need to register only a few classes. Hopefully their number will increase in the future. But currently what I am actually want is to type the container: register only these classes, without actually listing them or assume they reside in some namespace or have something in their name. In such cases I use the opposite:
 
public IContainer Initialize()
{
    var builder = new ContainerBuilder();
    builder.RegisterAssemblyTypes(GetType().Assembly)
        .Where(t => t.HasAttribute<RegisterInContainerAttribute>())
        .AsImplementedInterfaces();
    return builder.Build();
}

With the corresponding custom attribute:
 
[AttributeUsage(AttributeTargets.Class)]
public class RegisterInContainerAttribute : Attribute
{
         
}

Using this method of registering only types marked with the attribute also makes the code clearer to co-workers, who might look up for usages of the attribute and find out what classes are registered in the container. In case they are not familiar with Agent Mulder Resharper plugin.


Thursday 24 October 2013

BizArk documentation on Console application

It seems that the best documentation is "single page" with code snippets. Projects with such kind of documentation are easy to understand and to use. I've just added such documentation to BizArk project. 



Tuesday 17 September 2013

BizArk::Command-line parsing library

Some time ago I needed a simple command line parsing library. After some research I came to BizArk ToolKit by Brian Brewder. Since then I became addicted to this library, contributed a few features of my own, fixed a few bugs. Now I am proud to tell that its recent release version Bizark.Core 2.0.3 is available on nuget. Please don't get confused with the BizArk toolkit of older versions. Here is a sample usage:
 
    public enum ConsoleMode
    {
        Copy,
        Move
    }

    public class SimpleCommandLineArgs : CmdLineObject
    {
        [CmdLineArg]
        public ConsoleMode Mode { get; set; }

        [CmdLineArg]
        public string Path { get; set; }
    }

    public static class Program
    {
        private static void Main(string[] args)
        {
            ConsoleApplication.RunProgram<SimpleCommandLineArgs>(Start);
        }

        private static void Start(SimpleCommandLineArgs args)
        {
            // Your code goes here
        }
    }
It will parse the following command-line arguments and create an instance of SimpleCommandLineArgs:
 
C:> YouConsole.exe /Mode Move /Path C:\ 
Now available on nuget.org

PM> Install-Package BizArk.Core

For more info visit project site on CodePlex: BizArk

Tuesday 27 August 2013

Resharper's extract interface moves class property attributes to the interface

When I use ReSharper's magnificent feature of extract interface (Refactor -> Extract Interface), I have mixed feelings. On one hand I am happy that it saved me a lot of dirty work of manual cop-pasting the attributes, methods etc. On the other hand I am frustrated why class's attributes are copied to the interface. What the interface has to do with WCF attribute DataContract or Data Annotation attributes? I would expect ReSharper at least has an option whether to extract with or without the attributes. Apparently there is an open issue on TeamCity youTrack. The issue was open 7 (seven!!!) years ago and has only 3 votes (including myself).
Up vote for the issue: 


Tuesday 20 August 2013

How to install a development machine for .NET development

Recently I had to install a development machine from scratch. Like many developers I hate doing things twice, thus I summarized my experience in a few lines of command-line script. The script is divided to three logical parts:

  1. .NET Development environment
  2. Repositories
  3. Database

Let me introduce Chocolatey: the magical command-line installer that made my life much easier since I was introduced to it. You need to install it once by pasting  the magical piece of code to command-line console:
C:\>@powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin - See more at: http://chocolatey.org/#sthash.6Kd97faR.dpuf

.NET Development contains 
  1. VS2012 Professional. From my experience Professional edition is good enough for most of the development tasks. The Express edition is not supported by Resharper, so it is not acceptable for me. 
  2. Resharper - needless to explain its importance here.
  3. Nuget Package Manager - same
  4. Resharper Nuget plugin - a very nice plugin for Resharper, that significantly made my life easier: when I type a class ,which is not referenced in my project, the plugin looks whether a corresponding package is already referenced is adds reference to that package (instead of adding reference to bin\Debug\some_package.dll )
cinst VisualStudio2012Professional
cinst resharper
cinst NugetPackageManager
cinst resharper-nuget

Repositories:
These command-line lines install Tortoise SVN and VisualSVN.
cinst tortoisesvn
cinst visualsvn

Database:
You may choose any database, I chosen MySql for the small project I am working on. These lines of code would install MySQL server. Please note that root has empty password after the installation. It would also install a free SQL Client tool: HeidiSQL
cinst mysql
cinst HeidiSQL


P.S. A few last words about licensing. I do not encourage anyone working with illegal software, the opposite. All the above programs, except Visual Studio and Resharper, are free. They were especially chosen for the small development projects and are free of charge. Visual Studio license can be acquired by applying to BizSpark program of Microsft, for more details . Resharper license can be provided for free by JetBrains for open source projects.

Monday 19 August 2013

How to kill ASP.NET Development Server or IIS Express

When working on a web project I need to kill all those instances of ASP.NET Development Server IIS Express. A simple and quick solution for this is a KillCassini extension for Visual Studio. Using a shortcut, Shift+Alt+K it stops all the instances of ASP.NET Development Server, also known as Cassini, and IIS Express as well.

Download from Visual Studio Extensions Gallery:




Sunday 4 August 2013

Type safe configuration mapper

Almost every project needs some sort of configuration, that is external of the source code. Sometimes we spend time to find the best library that fits our needs and sometimes we give up and write something in-house. I would like to save your time in both these tasks and suggest what I have found and I use in my projects.

I was looking for a library that would allow me to represent all parameters as an interface or class. The parameters would be strongly typed with minimum overhead in mapping parameter to property. Providing default values would be also nice, but not necessarily. The best library that exactly fits these needs is ConfigReader. Here are a few simple steps how to use it in your project:

1. Put your configuration parameters in the regular app.config or web.config file. Parameters may be of any primitive data types:
 

 <appsettings>
    <add key="ApplicationConfiguration.Duration" value="30" />
    <add key="ApplicationConfiguration.Distance" value="3" />
    <add key="ApplicationConfiguration.Author" value="Boris Modylevsky" />
 </appsettings>

2. Create an interface with properties to represent the configuration in code. Give properties the same names as parameters names. Properties may be read-only - having only getter without setter. We don't need to specify no special mapping attributes, nothing. Just an interface with properties. Here is a corresponding interface:
 

public interface IApplicationConfiguration
{
    int Duration { get; }
    double Distance { get; }
    string Author { get; }
}
3. Next step we need to initialize our configuration interface from the configuration file. If you are using Inversion of Control (IoC) containers, it is better to to in the bootstrapper - where all the classes are registered in the container. I will show how this could be done in Autofac. First of all we create a ConfigurationModule class, which defines configuration registration in Autofac:
 
using System.Diagnostics.Contracts;
using Autofac;
using ConfigReader;

public class ConfigurationModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        Contract.Assert(builder != null, "Builder container is null");

        var configReader = new ConfigurationReader().SetupConfigOf<IApplicationConfiguration>();
        var configuration = configReader.ConfigBrowser.Get<IApplicationConfiguration>();
        builder.RegisterInstance(configuration).As<IApplicationConfiguration>();
    }
}
4. Then we just register the ConfigurationModule in our container builder and that's all!
 

using Autofac;

public static class Bootstrapper
{
    public static IContainer Container { get; private set; }

    public static IContainer Initialize()
    {
        var containerBuilder = new ContainerBuilder();
        containerBuilder.RegisterModule<ConfigurationModule>();
        Container = containerBuilder.Build();
        return Container;
    }
}
5. Use it by defining as constructor parameter. IoC container will take care to transfer the correct instance into your constructor:
 
public class SomeClassThatUsesConfiguration
{
    private readonly IApplicationConfiguration _configuration;

    public SomeClassThatUsesConfiguration(IApplicationConfiguration configuration)
    {
        _configuration = configuration;
    }

    public double GetVelocity()
    {
        return _configuration.Distance/_configuration.Duration;
    }
}
If you like the above and would like to use it in your project, download it from nuget.org:

PM> Install-Package ConfigReader




Friday 26 July 2013

How to add grammar and spelling check on your website

I've just noticed that I have posted my previous post with a minor typo. I will immediately correct it. But you may wonder how to add grammar and spelling checks on your website with minimal integration required.
1. Copy and paste the below on on your webpage
2. Add any textarea control and it will be automatically checked by Ginger spelling and grammar checker


<script type="text/javascript" src="http://cdn.gingersoftware.com/webWidget/gingerEditor.min.js"></script>
<script type="text/javascript">var init = GS_WEB_WIDGET.namespace("initParams");if (init && init.hasOwnProperty("init")) { init.init({apiKey:"03243e85-a1a7-4383-8338-2f1db1cb9b46"});}</script>
For more integration options see: http://www.gingersoftware.com/self_service

Thursday 25 July 2013

Automatic Deployment :: The final solution

In my previous post I lited tools used for automatic deployment. Although all of them have significant advantages, they did not fit for our needs. Or if they did, they seem to be too complex for such simple deployment process. So let's start with what we needed. We needed a script/tool that:
1. Downloads binaries 
2. Updates configuration files
3. Stops a windows service
4. Copies rellevant files 
5. Starts a windows service
6. Checks that the service is started and responding properly
7. Iterates the above steps for several servers in several data centers

One of the most important decisions we've made before and during the tools selection, was the following. Use the same deployment tool/script for deployment to test/staging environment as for production deployment. This rule seems to be pretty obvious and maybe irrelevant, but it saves a lot of times and script fixing during produciton debug. When the product is deployed to test machines on a frwquently basis it tested each time. If there is some new configuration parameter that needs to be updated. If we forget to update the deployment script it fails in deploy to test machine. Then we fix it in deploy to test machine and produciton deployment works smoothly.

So let's go back to tool seleciton: we use TeamCity as our continious integration system for builds automated tests etc. And it seemed so natural to use TeamCity for automatic deployment as well. We wrote, or actually, reused Powershell script previously written in order to extend Puppet. These scripts do the steps described above. 

So the winners are TeamCity and Powershell


Tuesday 16 July 2013

Automatic Deployment :: Choosing the magic tool

About a year ago I was asked to automate the deployment process. Actually it has some level of automation, but it should be significantly improved. When my boss says "significantly improve", he means "completely rewrite". In the beginning I was looking for that magic tool, that click-and-relax solutions that will do the work. Polling at StackOverflow and googling around resulted with a list of tools. This post will focus on advantages and disadvantages of these tools. In the next post I will try to explain what solution was chosen and why.

eyeShare by Ayehu http://www.ayehu.com/

eyeShare is a descent UI based tool for deployment automation and monitoring. I was interested only in the automation part. It provides and drag and drop user interface with different kinds of actions: Copy, Stop Service, Start Service etc. It has many cool features, such as email and SMS notifications and listeners. The listeners allow the user to respond to some failure in some action. For example, deployment of the database failed, eyeShare will send email/SMS with notification and will wait for reply: proceed or rollback database changes. This might be extremely useful. However it has some major drawbacks for our basic scenario:

  1. deployment processes stored by eyeShare are not version controlled, they are stored in SQL Server database with no history tracking; 
  2. it requires installation of the UI management on each machine and there is no web UI accessible from all the machines; 
  3. something that took me sometime to understand and I had to talk to their support: let say I have action A and then action B. I assume that until action A is finished, action B does not start. Basic serial processing. However eyeShare executes action in parallel, even of they are organized serially in the deployment workflow. There are some workaround that need to be done using timers and timeout in order to overcome this behavior. 
Octopus Deploy http://octopusdeploy.com/

Octopus Deploy is a cool deployment project for ASP.NET and Windows Service projects. Actually I did not dive too much into it, since it requires any software component will be delivered as a nuget package. Since our product has many dependent components, it was decided that it would be too much work to package every component as nuget package.


Puppet http://puppetlabs.com/

Puppet, puppet, puppet! Everybody in the DevOps world talk about Puppet. If your want automatic deployment you have to use Puppet. 

I was curious when starting to learn about Puppet. I have to admit, that the learning curve was steep, but at the end we had automatic deployment POC using Puppet. After the POC was completed it was decided not to proceed with it. Here are some of the reasons. My feeling was that Puppet it more Linux/Unix oriented, that Windows. Since our main product is developed in .NET and should be deployed to Windows servers, the whole overhead with Linux style paths and Linux style commands is ridiculous. Some of the operations are not supported in Puppet agent on Windows and I had to write scripts in Powershell. At the end it resulted that most of the things are written in Powershell, so there was no reason to use Puppet as a manager of these scripts.

For the actual selected and working solution wait for the next post:
http://borismod.blogspot.co.il/2013/07/automatic-deployment-final-solution.html