I've just setup a new instance of Umbraco 6.1.2 and am using MVC. I'm also using Unity to inject a unit of work into my overriden RenderMvcController (HomeController.cs). While the Home controller works fine and even the web api controllers I've added work fine, when I try and use a page with no special setup at all I get the error
The type UmbracoContext does not have an accessible constructor.
that also contains
[ResolutionFailedException: Resolution of the dependency failed, type = "Umbraco.Web.Mvc.RenderMvcController", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type UmbracoContext does not have an accessible constructor.
so I'm guessing I need to setup something in my Unity container to inject the context or I've seen a couple of posts that talk about writting your own ControllerFactory. I've not been able to find any useful articles on the web that have working examples.
This is my Unity setup
public static class UnityConfig
{
public static void Initialise()
{
var container = BuildUnityContainer();
GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
I've just come across your post, and I am having the same problem. I am using the UmbracoApiController and injecting my IUnitOfWork into the controller using unity. I am also setting up unity in my own EventHalder class (inherited from IApplicationEventHandler) which deals with OnApplicationStarting/OnApplicationStarted. The code in my OnApplicationStarting method is:
Umbraco 6, MVC and Unity
I've just setup a new instance of Umbraco 6.1.2 and am using MVC. I'm also using Unity to inject a unit of work into my overriden RenderMvcController (HomeController.cs). While the Home controller works fine and even the web api controllers I've added work fine, when I try and use a page with no special setup at all I get the error
The type UmbracoContext does not have an accessible constructor.
that also contains
so I'm guessing I need to setup something in my Unity container to inject the context or I've seen a couple of posts that talk about writting your own ControllerFactory. I've not been able to find any useful articles on the web that have working examples.
This is my Unity setup
public static class UnityConfig
{
public static void Initialise()
{
var container = BuildUnityContainer();
GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
RegisterTypes(container);
IControllerFactory factory = new UmbracoControllerFactory(container);
ControllerBuilder.Current.SetControllerFactory(factory);
return container;
}
public static void RegisterTypes(IUnityContainer container)
{
string connectionString = ConfigurationManager.ConnectionStrings["EntityContainer"].ConnectionString;
container.RegisterType<IDatabaseFactory, DatabaseFactory>
(new HttpContextLifetimeManager<IDatabaseFactory>())
.RegisterType<DatabaseFactory>(new InjectionConstructor(connectionString));
container.RegisterType<IUnitOfWork, UnitOfWork>();
container.RegisterType<IDeviceManager, DeviceManager>();
}
}
Any help on this would be great as I'm new to Unity and this is my first Umbraco project using Dependency Injection.
well this seems to have worked, not sure if its correct though
container.RegisterInstance(typeof (UmbracoContext), UmbracoContext.Current);
Hi Matthew,
I've just come across your post, and I am having the same problem. I am using the UmbracoApiController and injecting my IUnitOfWork into the controller using unity. I am also setting up unity in my own EventHalder class (inherited from IApplicationEventHandler) which deals with OnApplicationStarting/OnApplicationStarted. The code in my OnApplicationStarting method is:
var unity = new UnityContainer();
unity.RegisterType<PropertiesApiController>();
unity.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager());
unity.RegisterInstance(typeof (UmbracoContext), UmbracoContext.Current);
config.DependencyResolver = new IoCContainer(unity);
however I get the same error as you "The type IUnitOfWork does not have an accessible constructor."
Have you had any luck with this/can you post the code you have that fixed this please?
Thanks,
Justin
Did anyone manage to figure this out?
I'm having the same issue BUT only when trying to upgrade?
http://our.umbraco.org/forum/getting-started/installing-umbraco/53839-Unable-To-Upgrade-When-Using-Unity-IOC-The-type-UmbracoContext-does-not-have-an-accessible-constructor
is working on a reply...