Castle Windsor 2.0 in ASP.Net MVC 1.0

2009 October 23
by r.claypool

Consider yourself warned:  I’m just getting started with ASP.Net MVC, Dependency Injection (DI) and Inversion of Control (IoC), so this post is more for my benefit than yours.

warning-castle-windsor

ASP.Net MVC 1.0

ASP.Net MVC 1.0 was released in March 2009. Read this tutorial to start learning it. Note that ASP.Net MVC is bin deployable which is very nice if you don’t want to install anything on the production web server.

Inversion of Control and Castle Windsor

Castle Windsor 2.0 was just released this Summer after a very long development period. To start using it, go get the dlls and reference them in your project.

If you are completely new to the IoC pattern, go read James Kovacs’ article and watch these videos.  They will get a typical .Net programmer up to speed pretty fast. You should also go to the canonical source for IoC, but frankly I learned more from the other links and I think you will too.

To learn how to configure the Windsor container in your project, read the project’s documentation and continue with this article for an example in ASP.Net MVC.

Putting them Together

castle-plus-aspnet-mvc

Getting Windsor to work in an ASP.Net MVC application is easy if you know what to do. Most things are that way, huh?

For a bare bones integration, just follow these steps:

  1. Install ASP.NET MVC 1.0.  The Web Platform Installer is great for this and a ton of other projects.  Try it.  You’ll be glad you did.
  2. Open VS 2008,  go to File->New->Project… and select the “ASP.NET MVC Web Application” project template.
  3. Select “No” on the proceeding dialog. Do not create a unit test project.
  4. Download Castle Windsor 2.0
  5. Copy the dlls (Castle.DynamicProxy2, Castle.MicroKernel, Castle.Windsor and Castle.Core) to your solution’s folder or a sub folder.
  6. Add them as references to the web project. (Right-click the “References” folder, select “Add Reference…”, select the “Browse” tab and navigate to the dlls to add them.)
  7. Add a new XML file to the web project.  Name it “windsor.config” and enter the following:
  8. <?xml version="1.0" encoding="utf-8" ?>
    <!--Note: Since this is a web application, all components must have a lifesytle of 'transient' or 'preWebRequest' -->
    <configuration>
      <components>
        <!--This is where we could declaratively add components to the Windsor container if we wanted to.-->
      </components>
    </configuration>
  9. Create an interface IMessageHolder and a class MessageHolder.  (We will use this code to illustrate injecting an instance of MessageHolder into the HomeController.)
  10. public interface IMessageHolder
    {
    	string GetMessage();
    }
     
    public class MessageHolder : IMessageHolder
    {
    	#region IMessageHolder Members
     
    	public string GetMessage()
    	{
    		return "Welcome to ASP.NET MVC using Castle Windsor 2.0!";
    	}
     
    	#endregion
    }
  11. Create a controller factory for Windsor:
  12. public class WindsorControllerFactory : DefaultControllerFactory
    {
    	private readonly IWindsorContainer _container;
     
    	public WindsorControllerFactory(IWindsorContainer container)
    	{
    		_container = container;
    	}
     
    	protected override IController GetControllerInstance(Type controllerType)
    	{
    		if (controllerType == null)
    		{
    			// Force an exception message from the base class.
    			// This will be more informative than what we would get otherwise,
    			// see: http://stackoverflow.com/questions/719678/custom-controller-factory-dependency-injection-structuremap-problems-with-asp/721313#721313
    			return base.GetControllerInstance(controllerType);
    		}
    		// Exception here ????
    		// Check the windsor configuration.
    		// Make sure that Windsor can match the component to load.
    		return _container.Resolve(controllerType) as IController;
    	}
     
    	public override void ReleaseController(IController controller)
    	{
    		_container.Release(controller);
    	}
    }
  13. Open Global.asax.cs and replace it with the following:
  14. public class MvcApplication : HttpApplication
    {
    	/// <remarks>
    	/// This has to be static, because more than one instance
    	/// of the HttpApplication can potentially be created.
    	/// </remarks>
    	private static IWindsorContainer _container;
     
    	public static void RegisterRoutes(RouteCollection routes)
    	{
    		routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
     
    		routes.MapRoute(
    			"Default", // Route name
    			"{controller}/{action}/{id}", // URL with parameters
    			new {controller = "Home", action = "Index", id = ""} // Parameter defaults
    			);
    	}
     
    	protected void Application_Start()
    	{
    		RegisterRoutes(RouteTable.Routes);
    		InitializeWindsor();
     
    		// Setup the MVC framework to use our Windsor controller factory.
    		ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(_container));
    	}
     
    	protected virtual void InitializeWindsor()
    	{
    		if (_container == null)
    		{
    			_container = Build("windsor.config");
    		}
    	}
     
    	public static IWindsorContainer Build(string configPath)
    	{
    		var resource = new FileResource(configPath);
    		var interperter = new XmlInterpreter(resource);
    		var container = new WindsorContainer(interperter);
     
    		// Register controllers
    		container.Register(AllTypes
    					   .Of<Controller>()
    					   .FromAssembly(Assembly.GetExecutingAssembly())
    		/* Modify the name with which the component is registered
    		See: http://blog.andreloker.de/post/2009/03/28/ASPNET-MVC-with-Windsor-programmatic-controller-registration.aspx */
    					   .Configure(c => c.LifeStyle.Transient.Named(c.Implementation.Name.ToLower())));
     
    		// Register all other types.
    		container.Register(
    			Component.For<IMessageHolder>().ImplementedBy<MessageHolder>().LifeStyle.Transient);
     
    		return container;
    	}
    }
  15. Add a constructor to the HomeController and give it an IMessageHolder parameter.
  16.     [HandleError]
        public class HomeController : Controller
        {
            private readonly IMessageHolder _holder;
     
            public HomeController(IMessageHolder holder)
            {
                _holder = holder;
            }
     
            public ActionResult Index()
            {
                ViewData["Message"] = _holder.GetMessage();
     
                return View();
            }
     
            public ActionResult About()
            {
                return View();
            }
        }

    Since IMessageHolder is registered with Windsor, the container will resolve IMessageHolder to an instance of MessageHolder at run-time. This is called dependency injection because the HomeController dependency is injected through the constructor. Of course, you could do this without an IoC container such as Windsor, but in larger (real world) applications the container will greatly simplify the process of managing dependencies, which means you have a better chance of keeping things loosely coupled and falling into the pit of success.

    Notice also that we display the message in Index() to verify that the Windsor container is giving us a valid instance of the type registered for IMessageHolder. This is just a simple way to see the result of what occurred.

  17. Run the application and you should see the message on the home page. Click here to download the sample project.
  18. aspnet-and-windsor-integration

Project Source Code for this post.

I hope that this walk-through helps someone get started with Windsor. It really is an incredible project to learn and if used correctly it will make your programs much better. Have fun with it and Happy Programming!

Batch file error: is not recognized

2009 October 22
by r.claypool

Is your batch file reporting an error like this?

'n++ECHO' is not recognized as an internal or external command, operable program or batch file.

'n++ECHO' is not recognized as an internal or external command, operable program or batch file.

Open the file in Notepad++, select ‘Format -> Encode in ANSI’ and save.

encode-file-in-ansi

Hope that helps someone.  Happy Programming!

Terms and Conditions

2009 September 22
by r.claypool

The intent of this blog is to distribute valuable information and contribute to the technology and programming communities.  To that end, here’s how you may use and reuse what you find on this site:

creativecommons_logo

Techencoder is licensed under CC 3.0.  That means you are free to copy, distribute, transmit and adapt my work on this site for commercial and non-commercial purposes – as long as you attribute the work back to me.  A short notice including my name and a link to Techencoder will suffice.  An all expenses paid trip to Tuscany … even better!

In addition, any source code posted to this site may be used according to terms of the MIT license unless otherwise noted.   I avoid re-posting other people’s code without proper attribution and license info, but please be careful to distinguish what I claim as my own and what I do not.

If you have any doubt about the ownership or terms of anything on this site, contact me at the following address:

email_address

VMware Workstation: Cannot find a valid peer process

2009 July 12
tags: ,
by r.claypool

I am running VMware Workstation 6.1.5 (build-126130) on CentOS 5.3 (Final).

One of the guest machines was reporting an error when I tried to power on the most recent snapshot. Snapshots further back in the timeline powered on without any problem.

Error: Unable to change virtual machine power state: Cannot find a valid peer process to connect to.

Error: Unable to change virtual machine power state: Cannot find a valid peer process to connect to.

Apparently I’m not the only one with this problem. Others have been reporting it since at least early 2005. The forums say to delete unused lock files and restart any hung VMware processes (or restart the host machine), which did not help.

Then I tried to clone the snapshot before making any more changes – just to have a backup – and that failed too!

Cloning failed: reached the host's limit for open files

Cloning failed: reached the host's limit for open files

Thinking the two errors are related, I looked into how many files are actually part of this VM. The list was HUGE. (more than 1500 files in the folder)

I had 13 snapshots before the one giving me trouble, so I deleted a few of them to reduce the number of files VMWare would need to open.
This fixed everything.

I could have increased the max number of allowable files, but some of the snapshots were not needed any longer.  Your situation might be different, but this is a solution I couldn’t find anywhere else on the web. Hope that helps someone.

Accelerating Future, Part 2.5

2009 July 1
by r.claypool

This is an ‘out-of-band’ post for my Accelerating Future series…  I didn’t plan on covering Virtual Reality as a significant technology for change, but the folks over at Blue Mars really surprised me with this demo:

Taken in today’s context, it might look like “just another game” or “just a nice demo” but the potential is much bigger than that.

Mobile smart phones are bringing the Internet closer to everyday activities.  GPS and RFID are bringing location awareness to everyday objects. Smaller (and cheaper) high definition sensors and displays are making our window into the digital world easier to wear and carry.  New computer human interfaces are making our interactions with software more natural. Moore’s law is bringing the raw processing power and storage needed for life-like simulations.  It’s all quite a convergence!

So virtual and augmented reality are not going to be “just a game”.  They will work with all of tomorrow’s technologies to provide an environment for modeling the real world along with all its social interactions and information.  Just about every part of our lives will be affected by this next step of the web, just like we have been changed by the web as it is today.

Creative Commons Attribution 3.0 United States
Creative Commons Attribution 3.0 United States