# Friday, March 20, 2009

I was having trouble getting an ASP.NET 2.0 site up and running today on a (new to me) server.  I could browse static content from the site, but any ASPX pages would return a 404 error.   At first I thought I was mistyping the URL, then I thought that ASP.NET just wasn't registered so I ran the aspnet_regiis.exe utility, but still no dice. 

I played around with all kinds of settings until I find that ASP.NET was completely disabled on the server under Web Service Extensions.  For ASP.NET to work the WSE for ASP.NET must be set to allowed!  I have no idea why it was disabled, but it was.  Argh!

Friday, March 20, 2009 9:16:01 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [1]  | 
# Tuesday, February 10, 2009
I think this should be pretty obvious to the next person to look at the code.
public class CrappyTextComparer : ITextCompareReportGenerator { ...

public class BeyondCompare : ITextCompareReportGenerator { ...


Tuesday, February 10, 2009 5:46:15 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  | 
# Friday, February 06, 2009

This is a follow up to a previous post where I declared my laptop free of Vista. So to finally answer your question Ryan, its going well.

I've been running Ubuntu Intrepid (8.10) for the past 6 weeks almost every day and haven't had any problems. On the contrary, my laptop is super speedy now, and for whatever reason I swear the wireless works better and faster than it ever did with Vista.

I haven't missed MS Office all that much. I primarily use Outlook in MS Office anyway, and that's not by choice, so like most everyone I use GMail when not at work. When I do need to write an actual document I've found OpenOffice and Google docs more than adequate for my meager needs. In fact Google docs has actually been preferable since some of the docs I've needed to share authorship. I will have to concede that the UI in OpenOffice leaves room for improvement. The icons are hard to see and distinguish from their standard MS Office counter parts.

I've been using Gimp for image editing because it comes preinstalled, but its a little too technical for my needs, and thus difficult to do simple things. Even Photoshop is easier to use IMO. I find Paint.Net superior for my needs so I plan on switching since it will run on Mono. In fact a lot of the winform compatibility testing for Mono has been done using Paint.NET.

I have missed Visual Studio a little, but really what I miss are my ReSharper keyboard shortcuts. I've been primarily using Eclipse since I have been primarily doing Java and Groovy programming in my laptop. Both of these dev environments were quite easy to setup, especially the Android plugin for Eclipse, it works really quite well for editing and debugging. The Grails integration into Eclipse is pretty rough, so I'm looking into buying IntelliJ IDEA since I hear it has really good Grails support.  That and I'm already familiar with the keyboard shortcuts from ReSharper. I use the IDEA shortcuts in Re# which always throws my co-workers off when they try to pair with me.

The command line has been a little tricky to get used to, but has been more consistent between commands than Windows. And the translucent terminal windows are sure purdy. Ray pointed me to a free Ubuntu pocket guide that has helped smooth things over. I now often find myself incorrectly trying to use *nix commands in my DOS prompts at work. Perhaps its time to install Cygwin to iron it the discrepancy?

Not everything has been booz and cigars though. I've had serious issues with Mono, specifically MonoDevelop. The problems of course are my own making. The version of Mono and MonoDevelop packaged with Intrepid isn't quite as new as I would like, so I decided to build my own from the SVN trunk.  That was about 4 weeks ago.  After installing and bulding another 20 prerequisite libraries I finally got MonoDevelop to not only compile and load without crashing, but I also got it to compile and run my first C# app MonoDevelop!

This one goal of getting the latest version of MonoDevelop and Mono running on my Ubuntu box has taught me more about Mono and Linux than anything else I've done.  Despite the hair pulling its been a rewarding journey. I feel like I've earned my first Linux merit badge.

See Dylan, I gots its werking without resorting to a VM and the package manager!
GRails | Java | Linux
Friday, February 06, 2009 7:49:04 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [2]  | 
# Saturday, January 24, 2009

Update: Resharper 4.5 beta is out, and it natively supports MsTest.

Update: I updated this plugin to work with ReSharper 4.5

Chances are, if you have ReSharper you're using the built in ReSharper test runner. The Resharper test runner is pretty frictionless assuming you're using one of the open source testing frameworks like NUnit.

If you're stuck using MSTest for some reason, like in my unfortunate case my company has standardized on it... then you're pretty much stuck using the MSTest runner, which really sucks for numerous reasons.

The MSTest runner likes to muck around with vsdmi and testconfig files (or something like that, I can't remember) and is pretty slow. Up until VS 2008 it was almost completely useless for TDD.

Its almost usable in VS 2008, but I still hate the test failure reports. I can't just scan a bunch of grouped tests to see which one's failed and why. To really see why, I have to open a new test report tab in VS. Even more annoyingly, MSTest refuses to find my resource satellite assemblies without additional hoop jumping. I like to call that friction.

After finally having enough of this I decided to create my own MSTest ReSharper 4 plugin (Apache 2.0 license). It was actually quite easy to hook into the ReSharper test infrastructure, especially since JetBrains gives you most of the code to do it in the form of a csUnit plugin. A few deletes and edits later, and I have a functional MSTest plugin.

image

Not only does it work, it works better. My satellite assemblies are found right out of the gate, reports are inline with the runner, and for a moment I almost lapse back into mistakingly typing NUnit attributes.

image

Now mind you, its not perfect, but it works really well for my needs. Some gotchas, or differences between the standard MSTest runner and my plugin:

  • TestContext is always null, the runner doesn't provide that. Unit tests shouldn't need it anyway.
  • AssemblyInitialize is not honored, much like like TestDriven.NET. I get around this using a static initializer in my base class test fixture (I need this for some slow integration tests if you're wondering).
  • MSTest seems to create a new test fixture instance between each test run, this plugin only creates a single instance of the fixture. Generally this shouldn't be a problem if your TestInitialize method is written correctly.

Binaries and source are available on my Google Code web site. To install, just drop the DLL into the ReSharper bin\plugins folder and restart VS 2008. Happy testing!

Saturday, January 24, 2009 2:29:37 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [5]  | 
# Tuesday, January 13, 2009
Because I keep forgetting, and writing is supposed to help you remember things...

./etc$ sudo gedit environment

Tuesday, January 13, 2009 2:16:19 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [1]  | 
# Monday, December 22, 2008

I've been playing around with Grails on my Ubuntu box for the past week and really like the ability to do fast web development while still retaining all the power that the JVM provides.  Unfortunately, it also comes with the baggage of Java, which became pretty apparent once I needed to map a date time object.

Since I still have a day job, and I don't see us switching from .NET anytime soon, I thought I would spend a little time with ASP.NET MVC and IronPython to see where things are. I was able to find a sample MVC sample application on CodePlex which made use of IronPython in the views and the global.asax. The one feature I really wanted support for was writing controllers in python, apparently that's in the works but not currently supported.  Not much python in the sample, but pretty neat to know it works.

... Well actually at some point it probably did.  I'm using the first beta release of ASP.NET and some things have changed since the IronPython sample came out.  Needless to say, the sample no longer works, I get an HttpParseException with the message "HtmlHelper' object has no attribute 'ActionLink'"

Pretty strange message huh?  In C# I can find this method no problem. Long story short, IronPython can't use extension methods like C# can.

At some point the MVC team moved the HtmlHelper.ActionLink method from System.Web.Mvc to Microsoft.Web.Mvc.  The latter is the ASP.NET futures assembly, which provides a bunch of extension methods that they may add to the System DLL at a later date.

Monday, December 22, 2008 7:34:35 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  | 
# Monday, December 15, 2008

Today I start platform rehab, and this time I'm serious about getting clean. I'm writing this blog post from my laptop, yes my laptop, running Ubuntu 8.10. You don't get serious about dumping Windows until you take the plunge of installing Linux on your laptop. If you're like me, your laptop is your primary development box.

To my surprise, even my el-cheap-o laptop was absolutely painless to install Linux on. I suspect that maybe, just maybe, that even my 65 year old mom could do it. Even the wireless works connecting to the commuter trains free wifi. The only slightly difficult part was setting up my development environment, and that was mostly because I'm not super familiar with the OS or tooling, all of which are my problems.

Why did I switch? To people who use Linux already this is a rhetorical question, but for me the reason was pretty simple: Linux is free and good. I generally don't play games anymore, especially on my laptop. I don't really use MS Office too much, and Open Office is more than OK for my needs. Firefox is just as good on Linux as Windows.

More importantly though, Linux is the gateway to the backbone of the Internet. You don't build a web company on a proprietary per license platform when you have equivalent or better alternatives for free; after all Microsoft is dead. This speaks directly to competitive advantage and more importantly to a company's bottom line. I'm cheap. Startups, if their going to be successful, should be cheap.

Now back to GRails for me.

Monday, December 15, 2008 4:46:26 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [2]  | 
# Tuesday, December 09, 2008

First of all let me say that I love IoC containers.  I love them not because they are trendy, or that they help testability, or that they allow me to use AOP. No, the real reason I love IoC containers is because they let me be lazy.

When I'm designing a class I don't need to worry about how I get can get a dependency.  The container allows me to worry about that later, by separating construction from logic.  Think about that for a second. Every time we use the new keyword we're explicitly creating a dependency between implementations.  Sometimes this isn't a big deal, but if we have a dependency that itself has 10 other dependencies this can get out of hand real quick.

Containers are smart, they know which implementations fulfill which contracts.  Somewhere on application startup we just tell the container what contracts and what implementations we have and it figures out the dependencies for us. If we have a FooService that requires an IBarRepository implementation, the container will automatically new up a class that implements IBarRepository and give it to FooService on creation.

Things get a bit less academic and a bit more troublesome when we try to implement a container with ASP.NET.  Normally we would want the container to new up our page instance and provide any dependencies it my have, but with ASP.NET we don't get the chance to have the container create the page instances with all of its required dependencies.  We could create our own page handler factory to do this, but that's a lot of work to do right.

Sprint.NET has its own page handler factory which works very well, but the last I checked, Spring.NET configuration is very verbose and requires that each page that needs the container to construct it be registered with the container. It works, but I prefer convention over configuration.

My favorite .NET container is Windsor because it allows me to do more with less work (configuration), but unfortunately there's no built in way to use Windsor in a truly IoC way with ASP.NET.  The only supported way is to get a reference to the container and ask the container to resolve an instance of something, which directly makes your page dependant upon the container, and generally just adds noise where its not needed; you would see calls like this all over:

IFooService service = Container.Resolve<IFooService>();

Rather than explicitly asking the container for a service instance, I would much rather have the container inject any required dependencies into my page instance when it gets constructed.  The problem is how do we do this with Windsor?

Windsor AspNetModule

The approach I've taken is to create an http module that will inject dependencies into a page just after instantiation using setter injection.  This means that the container will inject any dependencies that my page may have using public property setters.  Constructor injection would be better, but IMO its not worth the effort of having to create my own page handler factory, here we can rely on the tried and true ASP.NET infrastructure.  Perhaps someday I'll change my mind, but for now this is acceptable.

Since I plan on using this container with a large amount of legacy code that doesn't use dependency injection I want to make the use of the container somewhat explicit.  I don't want to waste cycles scanning over a page that doesn't need any dependencies injected.  To enforce this, I make the pages that require injection declare so using a class level attribute.  Here's a succinct example with one dependency:

[UsesInjection]
public partial class CustomerDetail : Page
{
    public ICustomerRepository CustomerRepository { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        customerGrid.DataSource = CustomerRepository.GetAll();
        customerGrid.DataBind();
    }
}

When the module sees the current handler with the UsesInjection attribute it scans the class for public setter properties and tries to resolve any dependencies by type. If an instance is registered with that type in the container, the module tries to set the property. Here an ICustomerRepository instance gets injected. All of this happens before the page lifecycle begins, so its safe to use these dependencies on page init or page load.

If more explicit control is required, we can change the module's behavior by changing the class level UsesInjection attribute.

[UsesInjection(For.ExplicitProperties)]
public partial class CustomerDetail : Page
{
    private ICustomerRepository customerRepository;

    [RequiredDependency]
    public ICustomerRepository CustomerRepository
    {
        get { return customerRepository; }
        set { customerRepository = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        customerGrid.DataSource = CustomerRepository.GetAll();
        customerGrid.DataBind();
    }
}

The For.ExplicitProperties does two things:

  1. Optimizes the scanning process to only consider explicitly tagged properties (the default behavior considers all public settable properties).
  2. An exception will be thrown if a dependency couldn't be found in the container to inject. This makes is pretty evident if you forget to register a dependency with the container.

With the page level code in place we then need to setup our container and register our components. This is easily accomplished by implementing Castle.Windsor.IContainerAccessor in our global.asax and creating a static container with the registered components our web application needs to run.  Here we register to use an in memory customer repository implementation.

public class Global : System.Web.HttpApplication, IContainerAccessor
{
    private readonly IWindsorContainer container = new WindsorContainer();

    public Global()
    {
        container.Register(
            Component.For<ICustomerRepository>()
                .ImplementedBy<InMemoryCustomerRepository>());
    }

    public IWindsorContainer Container
    {
        get { return container; }
    }
}

We do this in the global.asax because we share the container between all requests, and thus only want one instance of it. Also, the IContainerAcessor is used as a service locator which is required by the AspNetWindsor http module to find your container instance which it will use to resolve dependencies.

The very last thing we need to do is register the AspNetWindsor http module in our web.config, just like any other module.

<add name="WindsorModule" type="Sneal.AspNetWindsorIntegration.AspNetDependencyBuilderModule, Sneal.AspNetWindsorIntegration"/>

From here on out, all ASP.NET requests get intercepted by this module and "enriched" by the container as long as they are tagged with the UsesInjection page level attribute.

The code is available under the Apache 2 license in my SVN repository here on Google Code.  A compiled binary is also available for download.

.NET | IoC
Tuesday, December 09, 2008 7:26:26 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  |