Category Archives: Code

Software Development

Using Castle Windsor DI for controllers in an Asp.Net WebApi Project

There are contrary signals on the web regarding Window DI and the new-ish MVC-ish WebApi IDependencyResolver. Fortunately, by late 2012 Mark Seemann summarised the state-of-the-art: don’t use it.

The reason: it doesn’t allow your DI container to properly manage lifetimes. Instead, do this:

public class WindsorCompositionRoot : IHttpControllerActivator
{
    private readonly IWindsorContainer container;
 
    public WindsorCompositionRoot(IWindsorContainer container)
    {
        this.container = container;
    }
 
    public IHttpController Create(
        HttpRequestMessage request,
        HttpControllerDescriptor controllerDescriptor,
        Type controllerType)
    {
        var controller =
            (IHttpController)this.container.Resolve(controllerType);
 
        request.RegisterForDispose(
            new Release(
                () => this.container.Release(controller)));
 
        return controller;
    }
 
    private class Release : IDisposable
    {
        private readonly Action release;
 
        public Release(Action release)
        {
            this.release = release;
        }
 
        public void Dispose()
        {
            this.release();
        }
    }
}

and wire it up with this line during Application_Start in Global.asax.cs

GlobalConfiguration.Configuration.Services.Replace(
    typeof(IHttpControllerActivator), new WindsorCompositionRoot());

Using Windsor for plain old MVC html pages inside WebApi projects

if you are using MVC controllers with views for html pages as well as IHttpControllers in your WebApi project, then you need to know that the WebApi controllers & activation is a quite separate subsystem from the MVC controllers. You still need the usual WindsorControllerFactory to hook Windsor into the MVC pages as well as the above for the WebApi controllers.

Code for a WindsorControllerFactory can be found in a couple of places, but this is from the Castle Windsor tutorial for Asp.Net 3 and is still valid for Asp.Net 4:

using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Castle.MicroKernel;
 
public class WindsorControllerFactory : DefaultControllerFactory
{
    private readonly IKernel kernel;
 
    public WindsorControllerFactory(IKernel kernel)
    {
        this.kernel = kernel;
    }
 
    public override void ReleaseController(IController controller)
    {
        kernel.ReleaseComponent(controller);
    }
 
    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        if (controllerType == null)
        {
            throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path));
        }
        return (IController)kernel.Resolve(controllerType);
    }
}

and again, you must hook it into the application object in Global.asax.cs

private static IWindsorContainer container;

//These lines should be called during Application_Start
container = new WindsorContainer().Install(FromAssembly.This());
var controllerFactory = new WindsorControllerFactory(container.Kernel);
ControllerBuilder.Current.SetControllerFactory(controllerFactory);

// And this in Application_End:
container.Dispose();

Asp.Net MVC Project Guids

The famously unhelpful Visual Studio error message ‘The project type is not supported by this installation‘ usually means that there is something you don’t have installed. But it doesn’t tell you what.

You can work out what you’re missing if you know the Guids to look for in the project file. Look for a line near the top of the file something like this:

<ProjectTypeGuids>{E53F8FEA-EAE0-44A6-8774-FFD645390401};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>

After which you can decode the Guid using either the table here:

Guid Tools Needed to Open Project Build references
{E53F8FEA-EAE0-44A6-8774-FFD645390401} Asp.Net MVC 3 System.Web.Mvc 3.0.0.0
{E3E379DF-F4C6-4180-9B81-6769533ABE47} Asp.Net MVC 4 System.Web.Mvc 4.0.0.0
{349c5851-65df-11da-9384-00065b846f21} Web Designer

Or the more extensive list at http://www.mztools.com/articles/2008/mz2008017.aspx. If you’re stuck try http://google.com/search?q=visual+studio+project+type+guid+list

Adaptable Design Up Front

ADUF
Adaptable Design Up Front
Adaptable Design Up Front
A term to describe the idea of (1) avoiding Big Design Up Front (2) doing Enough Design Up Front and (3) recognising that for agile development that up-front design must very specifically be a design that caters for future change. First coined as far as I know in a blog post by Hayim Makabee.

Doing Architecture with Agile Teams

Alan Gawthorpe & I a talk last month on doing architecture with agile teams:

We did the session agile-style: We had cards for our topics, and the ‘customers’ in the audience prioritised. I think it worked okay, it would probably go smoother doing it a second time. Some of the significant books & articles that went into the slides were:

Agile Methodologies for the Enterprise

* Scott Ambler & Mark Lines – Disciplined Agile Delivery
* Scott’s whitepaper on Agile@Scale

What does a technical architecture for agile look like?

* Coplien & Bjørnvig, Lean Architecture for Agile Software Development
* Poppendieck & Poppendieck, Lean Software Development: An Agile Toolkit
* http://alistair.cockburn.us/Walking+skeleton

The further reading list

* George Fairbanks, Just Enough Software Architecture
* The UK government’s national audit office report on Agile Governance
* http://www.disciplinedagiledelivery.com/
* http://en.wikipedia.org/wiki/OpenUP

Visual Studio 2012 Command Prompt Here

Something I always want with VS projects: The ‘Command Line here’ menu option within Visual Studio; and the ‘Visual Studio Tools Command Line here’ option in Windows explorer. So here they are for VS2012.

NB: that the Explorer right-click works when you right click on a folder icon but not, sadly, when you right click on empty space. If anyone has a solution for that I’ll be glad to know.

Windows Explorer “VS2012 Command Prompt Here” Right-Click Menu Item

To add a Visual Studio 2012 Command Prompt Here to your Explorer Right-Click menu, save this as a .reg file to your desktop, and then run it:

Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\Command Line VS2012]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\Command Line VS2012\command]
@="cmd.exe /k echo on & pushd \"%1\" & \"C:\\Program Files\\Microsoft Visual Studio 11.0\\VC\\vcvarsall.bat\" x86"

If you have 64-bit Windows, you might want:

Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\Command Line VS2012]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\Command Line VS2012\command]
@="cmd.exe /k echo on & pushd \"%1\" & \"C:\\Program Files\\Microsoft Visual Studio 11.0\\VC\\vcvarsall.bat\" x64"

Visual Studio 2012 External Tools Command Prompt Here Menu Item

In visual studio choose TOOLS – External Tools. Press Add to add a new command with fields as follows:

Title: Command Line Here
Command: %COMSPEC%
Arguments: /K "%VS110COMNTOOLS%\VsDevCmd"
Initial Directory: $(ItemDir)

i.e.:

Visual Studio External Tools Command Line Here VS2012 Dialog Box

Sorted. You can add shortcut key to via TOOLS – Customize – Keyboard and setting a shortcut key for Tools.ExternalCommand{X} … but you have to work out by counting what number your {X} is.
Visual Studio Customize Keyboard Tools.ExternalCommandX Dialog Box