I used unity as dependency resolver for several projects with Umbraco 7.2.6 and it was working good.
I started using Umbraco 7.2.8 and the same implementation doesn't work anymore. Umbraco back end does not update or remove any content.
Here is my implementation
With the Package Manager Console in Visual Studio 2012 I added Unity.MVC4 and Unity Web Api
I tried also with Ninject and the result is the same: the back office breaks.
Does anyone have a working example of Dependecy Injection with any dependency resolver?
Meanwhile I've upgraded to 7.3.7 and I switched to Autofac, which works nicely. Here's an extract from my Autofac configuration, hope it will point you in the right direction:
using System.Globalization;
using System.Reflection;
using System.Web.Http;
using System.Web.Mvc;
using Autofac;
using Autofac.Core;
using Autofac.Integration.Mvc;
using Autofac.Integration.WebApi;
using Umbraco.Core;
using Umbraco.Web;
public static class AutofacConfiguration
{
private static CultureInfo currentCulture = new CultureInfo("en-GB");
public static void ConfigureContainer()
{
var builder = new ContainerBuilder();
// Register the UmbracoContext as a factory, the controllers require this in their constructor
builder.Register(c => UmbracoContext.Current).AsSelf();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterApiControllers(typeof(UmbracoContext).Assembly);
builder.RegisterType<ApplicationContext>().AsSelf();
// register your own services here
RegisterContextObjects(builder);
// create container and setup the mvc dependency resolver to user autofac
var container = builder.Build();
var resolver = new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = resolver;
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
private static void RegisterContextObjects(ContainerBuilder builder)
{
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().WithParameters(
new[]
{
new ResolvedParameter((p, c) => p.Name == "currentCulture", (p, c) => currentCulture)
});
}
}
I've given up on Unity now and will try with Autofac...which version of the MVC and WebApi package are you using?
Ta
Mark
Update: All working now. Thanks Razvan! Much appreciated....for anyone else coming across this post add Autofac MVC5 and WebApi2 packages also. My Umbraco version is 7.4.3
Umbraco 7.2.8 and Unity Dependency Resolver Issue
Hello everyone,
I used unity as dependency resolver for several projects with Umbraco 7.2.6 and it was working good. I started using Umbraco 7.2.8 and the same implementation doesn't work anymore. Umbraco back end does not update or remove any content.
Here is my implementation
With the Package Manager Console in Visual Studio 2012 I added Unity.MVC4 and Unity Web Api
PM > Install-Package Unity.Mvc4
PM> Install-Package Unity.WebAPI -Version 0.10.0
After installing Unity a new class is added to my project : bootstrapper.cs
My global.asax
My HomePage controller
My Interface
My service
Basic DI stuff. But it breaks Umbraco 7.2.8 Back end
I tried to follow several tutorial on the internet, but I can not find the solution.
I also tried to follow the tutorial Umbraco documentation - with Autofac but I was not able to make it work
Does anyone have a any clue?
I tried also with Ninject and the result is the same: the back office breaks. Does anyone have a working example of Dependecy Injection with any dependency resolver?
Thanks in adavance
Don't know a solution for this but I'm also interested. Want to wire up ninject to make a package Plugin ready.
Maybe one way would be to create a custom dependency resolver?
Regards David
Hi David,
it's a pity not to be able to use Dependency Injection. I will give it a try with the new version of Umbraco and let you know. 'Who dares wins...'
Hi,
I was wondering if you had any luck with this. I'm getting the same error when accessing the Umbraco backoffice.
Thanks, Razvan
Hi There,
Same issue here too. Any resolution yet for anyone?
Thanks in advance
Hi Mark,
Meanwhile I've upgraded to 7.3.7 and I switched to Autofac, which works nicely. Here's an extract from my Autofac configuration, hope it will point you in the right direction:
Hi Mark,
I will try with your method. Thanks for sharing it.
Enrico
Hi guys,
if you need sometimes to avoid using Dependency Resolvers you can injct dependency in following way
This an example of DI in an Umbraco Surface controller. I have an interface called IContactRepository and a repository called ContactRepository.
I hope it will be helpful.
Thanks Razvan,
I've given up on Unity now and will try with Autofac...which version of the MVC and WebApi package are you using?
Ta Mark
Update: All working now. Thanks Razvan! Much appreciated....for anyone else coming across this post add Autofac MVC5 and WebApi2 packages also. My Umbraco version is 7.4.3
Hi Mark,
I'm using Autofac.Mvc5 v 3.3.4, Autofac.WebApi2 v 3.4.0, Autofac v 3.5.0, Microsoft.AspNet.Mvc v 5.2.3 and WebApi v 5.2.0.
Hope that helps.
Cheers, Razvan
is working on a reply...