I am trying to implement Unity IOC in a Umbraco 7.3.2 project get YSOD at application startup.
Sofar I have only the required dependencies added to the project and also register UmbracoContext as following:
container.RegisterType<UmbracoContext>(
new ContainerControlledLifetimeManager(),
new InjectionFactory(c => UmbracoContext.Current));
Then, Unity throws the following exception:
The current type, Umbraco.Core.Models.IPublishedContent, is an interface and cannot be constructed. Are you missing a type mapping?
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The current type, Umbraco.Core.Models.IPublishedContent, is an interface and cannot be constructed. Are you missing a type mapping?
Source Error: ...... etc.
What do I miss? How to find out which types I need to register to make Umbraco controllers work?
I went for a lame solution choosing Autofac as IoC container, but I didn't have any preference about a particular IoC container and Autofac proved to work good.
Following the tutorial I created the class MyAwesomeContext
public class MyAwesomeContext
{
public MyAwesomeContext()
{
MyId = Guid.NewGuid();
}
public Guid MyId { get; private set; }
}
And after that I modified the global.asax following the second method suggested in the tutorial, because the first one was not working:
public class MyApplication : IApplicationEventHandler
{
public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
}
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
var builder = new ContainerBuilder();
//register all controllers found in this assembly
builder.RegisterControllers(typeof(MyApplication).Assembly);
//add custom class to the container as Transient instance
builder.RegisterType<MyAwesomeContext>();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
}
}
After compiling I got two errors : one on the back end and one in the fron end
Front End Error :
System.MissingMethodException: No parameterless constructor defined for this object.
Back End Error
Received an error from the server
Failed to retrieve data for application tree content
Object reference not set to an instance of an object.
EXCEPTION DETAILS:
System.NullReferenceException: Object reference not set to an instance of an object.
*I think that Umbraco 7 is not fully compatible with IOC Container ( I tried with Unity, Ninject and Autofac) *
Obviosly, the tutorial on the documentation is not working
We have autofac working without any problem. But there are some Umbraco core things you need to register as well.
This what we have :
// Register the UmbracoContext as a factory, the controllers require this in their constructor
builder.Register(c => UmbracoContext.Current).AsSelf();
builder.RegisterControllers(typeof(MyApplication).Assembly);
builder.RegisterApiControllers(typeof(MyApplication).Assembly);
builder.RegisterApiControllers(typeof(UmbracoApplication).Assembly);
builder.RegisterType<ApplicationContext>().AsSelf();
// register your own services here
// create container
var container = builder.Build();
// setup the mvc dependency resolver to user autofac
var resolver = new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = resolver;
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
where IPublishedContent does not have any registered types.
Try to register the RenderMvcController constructor explicitly to avoid that:
container.RegisterType<UmbracoContext>(
new ContainerControlledLifetimeManager(),
new InjectionFactory(c => UmbracoContext.Current))
.RegisterType<RenderMvcController>(
new InjectionConstructor(new ResolvedParameter<UmbracoContext>()))
Umbraco 7.3.2 and Unity IOC error
I am trying to implement Unity IOC in a Umbraco 7.3.2 project get YSOD at application startup.
Sofar I have only the required dependencies added to the project and also register UmbracoContext as following:
Then, Unity throws the following exception:
What do I miss? How to find out which types I need to register to make Umbraco controllers work?
Thanks!
I had similar problem with Unity and Umbraco 7.2.8. You can take a look at my implementation
https://our.umbraco.org/forum/developers/api-questions/72485-umbraco-728-and-unity-dependency-resolver
With this solution the back office breaks and does not work properly.
I asked for some help but I didn't get any help for now
Hi Alex,
Did you have any luck with this? I'm getting the same error when accessing the Umbraco backoffice.
Thanks,
Razvan
I went for a lame solution choosing Autofac as IoC container, but I didn't have any preference about a particular IoC container and Autofac proved to work good.
Thank you for your suggestion, I'll give it a try.
Dependecy Injection update:
I tried to follow the tutorial on Umbraco documentation at https://our.umbraco.org/documentation/reference/using-ioc
I started creating with Visual Studio 2012 a new project
With the Package Manager Console in Visual Studio 2012 I added Umbraco and Autofac
PM > Install-Package UmbracoCms
PM> Install-Package Autofac.Mvc5
Following the tutorial I created the class MyAwesomeContext
And after that I modified the global.asax following the second method suggested in the tutorial, because the first one was not working:
After compiling I got two errors : one on the back end and one in the fron end
Front End Error :
System.MissingMethodException: No parameterless constructor defined for this object.
Back End Error
Received an error from the server Failed to retrieve data for application tree content
Object reference not set to an instance of an object.
EXCEPTION DETAILS:
System.NullReferenceException: Object reference not set to an instance of an object.
*I think that Umbraco 7 is not fully compatible with IOC Container ( I tried with Unity, Ninject and Autofac) *
Obviosly, the tutorial on the documentation is not working
Hi Alex,
We have autofac working without any problem. But there are some Umbraco core things you need to register as well.
This what we have :
Dave
It seems that one more constructor have been added to RenderMvcController in latest releases:
so, by default Unity use this constructor with the most parameters and next try to resolve UmbracoHelper using the constructor:
where IPublishedContent does not have any registered types. Try to register the RenderMvcController constructor explicitly to avoid that:
Thanks Alexey for sharing your solution!
I use StructureMap and had the same issue, this is the solution:
We had same issue with unity and umbraco and Merchello, we solved the issue by registering types
Thanks for sharing - this resolved some issues for me.
Add an empty controller with name of the document type (ex. ContentPageController) and inherit from Umbraco.Web.Mvc.RenderMvcController
is working on a reply...