I am currently working on an Umbraco package and since it's quite an extensive package separated in multiple projects, I would like to make use of an IOC container to inject dependencies.
The first thing that came to my mind when I wanted to implement this is the Umbraco documentation on using IOC https://our.umbraco.org/documentation/reference/using-ioc. That example uses Autofac as IOC container. I used the exact same code to try to get mine to work:
Unfortunately Umbraco crashes as soon as I login on the backend saying "Object reference not set to an instance of an object" in the ApplicationTreeController. Exception:
I also found it interesting that the example uses only Dependency resolver.SetResolver and no GlobalConfiguration.Configuration.DependencyResolver to set the dependency resolver for WebAPI. Why doesn't the documented way to use IOC work? Am I doing something wrong or is the documentation out of date/incorrect?
So I am trying to use this IOC container in a package. Isn't that going to conflict with other packages using their own IOC container? I couldn't find any good information or documentation on this matter
You're missing a bit. Try this; it's from some of my code:
You need to register the Umbraco controllers and the UmbracoContext.
public static ContainerBuilder GetContainerBuilder(params Assembly[] controllerAssemblies)
{
// IoC setup
var builder = new ContainerBuilder();
// Register Umbraco Context, MVC Controllers and API Controllers.
var umbracoAssembly = typeof(UmbracoApplication).Assembly;
builder.Register(c => UmbracoContext.Current).AsSelf();
builder.RegisterControllers(umbracoAssembly);
builder.RegisterApiControllers(umbracoAssembly);
var currentAssembly = typeof(DependencyInjectionConfig).Assembly;
builder.RegisterControllers(currentAssembly);
builder.RegisterApiControllers(currentAssembly);
if (controllerAssemblies != null && controllerAssemblies.Any())
{
builder.RegisterControllers(controllerAssemblies);
builder.RegisterApiControllers(controllerAssemblies);
}
// Register the types we need to resolve with Autofac here
// Set Resolvers
var container = builder.Build();
System.Web.Mvc.DependancyResolver.SetResolver(new AutofacDependencyResolver(container));
System.Web.Http.GlobalConfiguration.Configuration.DependancyResolver = new AutofacWebApiDependencyResolver(container);
return builder;
}
Personally I wouldn't ship any IOC container with a package. You never know if people are using one in their site. I think even if it's the same one it could give issues (for example different versions used).
If you want you can always do Poor mans DI using somekind of service factory.
using an IOC container in a package
I am currently working on an Umbraco package and since it's quite an extensive package separated in multiple projects, I would like to make use of an IOC container to inject dependencies.
The first thing that came to my mind when I wanted to implement this is the Umbraco documentation on using IOC https://our.umbraco.org/documentation/reference/using-ioc. That example uses Autofac as IOC container. I used the exact same code to try to get mine to work:
Unfortunately Umbraco crashes as soon as I login on the backend saying "Object reference not set to an instance of an object" in the ApplicationTreeController. Exception:
I also found it interesting that the example uses only Dependency resolver.SetResolver and no GlobalConfiguration.Configuration.DependencyResolver to set the dependency resolver for WebAPI. Why doesn't the documented way to use IOC work? Am I doing something wrong or is the documentation out of date/incorrect?
I am using the following autofac dependencies:
<package id="Autofac" version="4.6.0" targetFramework="net452" /> <package id="Autofac.Mvc5" version="4.0.2" targetFramework="net452" />
So I am trying to use this IOC container in a package. Isn't that going to conflict with other packages using their own IOC container? I couldn't find any good information or documentation on this matter
You're missing a bit. Try this; it's from some of my code:
You need to register the Umbraco controllers and the UmbracoContext.
You saved my life. Thanks!
I hope this doesn't conflict with other packages using IOC containers as well.
Hi Bernard,
Personally I wouldn't ship any IOC container with a package. You never know if people are using one in their site. I think even if it's the same one it could give issues (for example different versions used).
If you want you can always do Poor mans DI using somekind of service factory.
Dave
Thanks. That's what I was afraid of. I will look into poor mans DI
is working on a reply...