MvcExtensions with Unity

May 28, 2012 at 4:43 PM

Good day, hazzik! Could you post a example of using Unity with MvcExtensions, please. Or do you still recommend Windsor? Do you plan to stop producing or make new commercial product on this basis?

Developer
May 28, 2012 at 7:46 PM

Hi, using Unity almost the same as Windsor.

1. Download package from nuget (or you could download release as zip archive):

install-package MvcExtensions.Unity 

2. Inherit your application from MvcExtensions.Unity.UnityMvcApplication

http://mvcextensions.codeplex.com/SourceControl/changeset/view/e5699e89a5e4#Trunk%2fSample%2fDemo.Web.Unity%2fGlobal.asax.cs

3. Register your services in modules (IModule)

http://mvcextensions.codeplex.com/SourceControl/changeset/view/e5699e89a5e4#Trunk%2fSample%2fDemo.Web.Unity%2fInfrastructure%2fRegisterServices.cs

Aslo examples for each popular IoC container are located in 'Samples' directory in the source control.

 >Do you plan to stop producing or make new commercial product on this basis?

No and no. Now we are developing version 2.5 which is focused on easily extension of fluent metadata and validation enchancements. Also in plans support for native MVC IoC adapters (such as Ninject.Mvc3, Autofac.Web.Mvc, Unity.Mvc3, etc) and possible support of WebActivator.

Developer
May 28, 2012 at 7:48 PM
Edited May 28, 2012 at 7:48 PM

Also we had moved source code for version 2.5 to github http://mvcextensions.github.com

May 29, 2012 at 8:32 AM

Thanks! But... I have a error in my mvc4 application.

System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=MvcExtensions
  StackTrace:
       at MvcExtensions.Bootstrapper.Execute[TTask](IEnumerable`1 tasks) in d:\BuildAgent-02\work\5dc85169f18b1630\src\MvcExtensions\Bootstrapper\Bootstrapper.cs:line 176
       at MvcExtensions.Bootstrapper.ExecuteBootstrapperTasks() in d:\BuildAgent-02\work\5dc85169f18b1630\src\MvcExtensions\Bootstrapper\Bootstrapper.cs:line 104
       at MvcExtensions.ExtendedMvcApplication.Application_Start() in d:\BuildAgent-02\work\5dc85169f18b1630\src\MvcExtensions\ExtendedMvcApplication.cs:line 75
  Call stack location: MvcExtensions.dll!MvcExtensions.ExtendedMvcApplication.Application_Start() Line 76

The file 'd:\BuildAgent-02\work\5dc85169f18b1630\src\MvcExtensions\ExtendedMvcApplication.cs' does not exist.
Looking in script documents for 'd:\BuildAgent-02\work\5dc85169f18b1630\src\MvcExtensions\ExtendedMvcApplication.cs'...
Looking in the projects for 'd:\BuildAgent-02\work\5dc85169f18b1630\src\MvcExtensions\ExtendedMvcApplication.cs'.
The file was not found in a project.

My code:

public class MvcApplication : UnityMvcApplication
    {
        public MvcApplication()
        {
            Bootstrapper.BootstrapperTasks
                        .Include<RegisterModelMetadata>()
                        .Include<RegisterControllers>()
                        .Include<RegisterRoutes>()
                        .Include<RegisterActionInvokers>();
        }
        public class RegisterRoutes : RegisterRoutesBase
        {
            public RegisterRoutes(RouteCollection routes)
                : base(routes)
            {
            }
            protected override void Register()
            {
                Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
                Routes.MapRoute(
                    "Default",
                    "{controller}/{action}/{id}", // URL with parameters
                    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );
            }
        }

public class RegisterServices : IModule
    {
        public void Load(IUnityContainer container)
        {
            container.RegisterType<IMyService, IMyService>(new PerRequestLifetimeManager());
        }
    }

Packages installed: Microsoft.Practices.ServiceLocation, Microsoft.Practices.Unity, MvcExtensions, MvcExtensions.Unity.

Developer
May 29, 2012 at 9:19 AM

SpaceMarine, which versions exactly are you using? 

May 29, 2012 at 10:09 AM

VS 11.0.50214.1 betarel; AspNetMvc 4.0.10906.0; CommonServiceLocator 1.0; Unity 2.1.505.0; MvcExtensions 2.5.0-rc8005 pre; MvcExtensions.Unity 2.5.0-rc8005 pre

all installed without errors from package console.