39 Comments

Lately I’ve enjoyed working with static web sites. My company’s web pages were previously powered by Wordpress and now they’re just static html pages. There’s few things I especially like with a static web site when compared to other solutions:

  • Performance
  • Hosting options
  • Ease of deployment

Performance

Even though I find the Wordpress near great with all its plugins and themes, the previous web site never felt fast enough. With a static web site you usually don’t have to worry about the performance.

Hosting options

You can host the static site almost everywhere: You don’t need ASP.NET or PHP. You just need a web server like IIS or Apache. Or GitHub.The web server can be run using for example Amazon EC2.

Ease of deployment

In addition to pros mentioned above a static web site is also easy to deploy: There’s no need for a database or configuration. In my case the company web site (html, css and js) is available from a GitHub repository. To host the site on a new web server requires only a Git Clone to the correct directory.

Using Amazon EC2 to host the web site: Automating the deployment

Given that the static web site is available using Git and because we only need a web server to host the site, we can automate the site’s deployment. Here’s how we can do it using Amazon EC2:

1. Launch new instance

Start the Quick Launch Wizard and select Amazon Linux AMI:

image

Amazon Linux supports the configuration of the launch process using cloud-init. Unfortunately I haven’t found any really good examples of using the cloud-init but here’s couple sources: A thread in AWS forums and the following source.

2. Make sure that the instance’s firewall allows the web traffic through

By default the TCP port 80 is blocked. Select a security group which allows the traffic through or create a new security group:

image

3. Input the cloud-init into the “User Data” –field

Here’s a cloud-init initialization code which does the following things:

  1. Installs Apache
  2. Installs Git
  3. Starts the web server
  4. Clones the web site from GitHub into the web server’s root
#cloud-config
packages:
 - httpd
 - git

runcmd:
 - [ /etc/init.d/httpd, restart ]
 - [ git, clone, "-b", "gh-pages", "git://github.com/mikoskinen/softwaremkwebsite.git", "/var/www/html"]

Here’s how the cloud-init initialization code can be set when launching a new Amazon EC2 instance:

 image

And that’s it. After few minutes the Amazon EC2 is ready and hosting the static web site:

image

image

3 Comments

In this tutorial we will go through of couple different ways of using custom constructor parameters when resolving an instance with Unity:

  1. By using the built-in ParameterOverride
  2. By creating a custom ResolverOverride.

Background

When you’re using a DI-container like Unity, you normally don’t have to worry about how the container resolves the new instance. You have configured the container and the container will act based on your configuration. But there may be cases where you have pass in custom constructor parameters for the resolve operation. Some may argue that this screams of bad architecture but there’s situations like bringing a DI-container to a legacy system which may require these kind of actions.

Resolved class

In this tutorial we are resolving the following test class:

    public class MyClass
    {
        public string Hello { get; set; }
        public int Number { get; set; }

        public MyClass(string hello, int number)
        {
            Hello = hello;
            Number = number;
        }
    }

It is registered to the container using RegisterType-method and without passing in any parameters:

            var unity = new UnityContainer();
            unity.RegisterType<MyClass>();

So let’s see how we can pass in the “hello” and “number” variables for the MyClass’ constructor when calling Unity’s Resolve.

Unity ResolverOverride

Unity allows us to pass in a “ResolverOverride” when the container’s Resolve-method is called. ResolverOverride is an abstract base class and Unity comes with few of these built-in. One of them is ParameterOverride which “lets you override a named parameter passed to a constructor.” 

So knowing that we need to pass in a string named “hello” and an integer called “number”, we can resolve the instance with the help of ParameterOverride:

        [Test]
        public void Test()
        {
            var unity = new UnityContainer();
            unity.RegisterType<MyClass>();

            var myObj = unity.Resolve<MyClass>(new ResolverOverride[]
                                           {
                                               new ParameterOverride("hello", "hi there"), new ParameterOverride("number", 21)
                                           });

            Assert.That(myObj.Hello, Is.EqualTo("hi there"));
            Assert.That(myObj.Number, Is.EqualTo(21));
        }

We pass in two instances of ParameterOverride. Both of these take in the name and the value of the parameter.

Custom ResolverOverride: OrderedParametersOverride

But what if you don’t like passing in the parameter names and instead you want to pass in just the parameter values, in correct order? In order to achieve this we can create a custom ResolverOverride. Here’s one way to do it:

    public class OrderedParametersOverride : ResolverOverride
    {
        private readonly Queue<InjectionParameterValue> parameterValues;

        public OrderedParametersOverride(IEnumerable<object> parameterValues)
        {
            this.parameterValues = new Queue<InjectionParameterValue>();
            foreach (var parameterValue in parameterValues)
            {
                this.parameterValues.Enqueue(InjectionParameterValue.ToParameter(parameterValue));
            }
        }

        public override IDependencyResolverPolicy GetResolver(IBuilderContext context, Type dependencyType)
        {
            if (parameterValues.Count < 1)
                return null;

            var value = this.parameterValues.Dequeue();
            return value.GetResolverPolicy(dependencyType);
        }
    }

The parameter values are passed  in through the constructor and put into a queue. When the container is resolving an instance, the parameters are used in the order which they were given to the OrderedParametersOverride.

Here’s a sample usage of the new OrderedParametersOverride:

        [Test]
        public void TestOrderedParametersOverride()
        {
            var unity = new UnityContainer();
            unity.RegisterType<MyClass>();

            var myObj = unity.Resolve<MyClass>(new OrderedParametersOverride(new object[] {"greetings", 24 }));

            Assert.That(myObj.Hello, Is.EqualTo("greetings"));
            Assert.That(myObj.Number, Is.EqualTo(24));
        }

Sample code

The above examples can be found from GitHub.

0 Comments

Updated 28.10.2011: Fixed the link

We have an application which uses the lovely portable class libraries to share classes between a Silverlight and an ASP.NET MVC application. Everything went fine until the application was deployed to a test server, at which point the ASP.NET MVC site stopped working with the following error:

Could not load file or assembly 'System.Xml.Serialization”.

Our ASP.NET MVC makes a call to a WebService and the required proxy classes are inside a portable class library project. Turns out that solving this issue was very easy once we knew where to look at. You just need to install a .NET Framework 4 patch, the KB2468871 to be more precise. After we installed the update, everything just worked. And the reason we didn’t encounter this problem with out development environments was because the VS 2010 SP1 includes this patch.

0 Comments

When using the msysgit and the git svn fetch you may encounter the error:

"can't locate config_heavy.pl in @INC ... at /usr/lib/perl5/5.8.8/msys/Config.pm line 66".

This has been reported to the msysgit-team and the fix should be available in an upcoming version as stated here: http://code.google.com/p/msysgit/issues/detail?id=433. In the mean time you can download the git-svn-fix.zip from the following GitHub-repository to get around the problem. After downloading the file, extract it into the following folder: C:Program Files (x86)Git.

You can check if the installation succeeded by making sure that the file Config_heavy.pl exists in the folder C:Program Files (x86)Gitlibperl55.8.8msys.

Git-svn-fix.zip location: https://github.com/mikoskinen/blog/tree/master/git-svn-fix

102 Comments

The IIF-statements in VB.NET are a handy way to shorten our code and to make it more readable. It’s also an easy way to introduce subtle bugs into our systems.

Given the following code:

        Dim amount = 100

        IIf(amount = 100, amount = 30, amount = 50)
        Console.WriteLine(amount)

When run, the app will print out 30, yes? Well, actually no. Here’s the actual output when the app is executed:

image

What happens is that the code inside the true-part of the IIF-statement is not an assignment.Instead, it’s interpreted by the compiler as aboolean expression. Now let’s try to modify our code so that it stores the return value of the IIF-statement in a variable:

        Dim amount = 100

        Dim outPutValue = IIf(amount = 100, amount = 30, amount = 50)
        Console.WriteLine(amount)
        Console.WriteLine(outPutValue)

This is the output when executed:

image

As you can see, the IIF statement returns False, because it compares the equality of variable “amount” to value 30.

To correct the above code one should change it to the following:

        Dim amount = 100

        amount = IIf(amount = 100, 30, 50)
        Console.WriteLine(amount)

Bottom line: IIF-statements in VB.NET shouldn’t be used for assignments.