My MVC project using Unity has suddenly stopped resolving types in my controllers. I'm comparing the setup with a freshly made mvc application with the Unity nuget package - and I caot see any differences. My applicaton worked previously.
I'm tempted to start a fresh application and slowly moving code over from this application to start identifying the problem, but figured I could ask here to get some useful help.
Global.asax.cs
protected void Application_Start()
{
log4net.Config.XmlConfigurator.Configure();
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
UnityConfig2.GetConfiguredContainer().AddNewExtension<Log4NetExtension>();
UnityConfig2.GetConfiguredContainer().RegisterType<ITest, Test>();
}
UnityMvcActivator.cs
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(xxx.App_Start.UnityWebActivator), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethod(typeof(xxx.App_Start.UnityWebActivator), "Shutdown")]
namespace xxx.App_Start
{
/// <summary>Provides the bootstrapping for integrating Unity with ASP.NET MVC.</summary>
public static class UnityWebActivator
{
/// <summary>Integrates Unity when the application starts.</summary>
public static void Start()
{
var container = UnityConfig2.GetConfiguredContainer();
FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));
var resolver = new UnityDependencyResolver(container);
DependencyResolver.SetResolver(resolver);
// TODO: Uncomment if you want to use PerRequestLifetimeManager
Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
}
/// <summary>Disposes the Unity container when the application is shut down.</summary>
public static void Shutdown()
{
var container = UnityConfig2.GetConfiguredContainer();
container.Dispose();
}
}
}
In my HomeController.cs file I just add a dependency:
[Dependency]
public ITest test { get; set; }
With this setup I get an exception: The current type, xxx.ITest, is an interface and caot be constructed. Are you missing a type mapping?
If I put a breakpoint after the RegisterType<>() method call and execute
UnityConfig2.GetConfiguredContainer().Resolve<ITest>()
within the Immediate Window resolution succeeds.
Anyone have any ideas on how to further investigate this issue?
