Umbraco 7.2.3 Dependency Injection with SimpleInjector
Hi,
I'm trying to get SimpleInjector to work with a SurfaceController but get the following error:
Exception message: No parameterless constructor defined for this object.
I currently using
Umbraco 7.2.3
SimpleInjector.Integration.Web.Mvc 3.0.5
In an external project I have Entity Framework 6
This is my setup
public class Startup : Umbraco.Web.UmbracoApplication
{
protected override void OnApplicationStarted(object sender, EventArgs e)
{
base.OnApplicationStarted(sender, e);
// Create the container as usual.
var container = new Container();
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
// Register your types, for instance:
//Does not help issue: container.Register(()=> UmbracoContext.Current, Lifestyle.Singleton);
container.Register<IDataContext, PortalDataContext>(Lifestyle.Singleton);
// This is an extension method from the integration package.
container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
// This is an extension method from the integration package as well.
container.RegisterMvcIntegratedFilterProvider();
container.Verify();
DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
}
}
And here's my class trying to use it
public class RegisterFormSurfaceController : SurfaceController
{
public RegisterFormSurfaceController(IDataContext dataContext)
{
_dataContext = dataContext;
}
private IDataContext _dataContext { get; }
public ActionResult GetRegister()
{
var model = new RegisterFormViewModel();
var optionsMobile =
(from c in _dataContext.Countries
select new OptionModel
{
Id = c.CountryID,
Value = $"{c.CountryName} ({c.DialCode})"
}).ToList();
model.MobileNumberPrefixOptions = optionsMobile;
return PartialView("~/Views/Partials/RegisterForm.cshtml", model);
}
I've tried various options with no joy, any help is appreciated.
Thanks for the reply, The error I'm getting when my SufraceController is initiated is:
An exception of type 'System.InvalidOperationException' occurred in System.Web.dll but was not handled in user code.
Additional information: An error occurred when trying to create a controller of type 'RegisterFormSurfaceController'. Make sure that the controller has a parameterless public constructor.
I did try the configuration mentioned in the link, but the error did not change, the linked page does mention another error, which does not appear to be applicable to my issue:
No registration for type RenderMvcController could be found and an implicit registration could not be made. For the container to be able to create RenderMvcController, it should contain exactly one public constructor, but it has 2.
Umbraco 7.2.3 Dependency Injection with SimpleInjector
Hi,
I'm trying to get SimpleInjector to work with a SurfaceController but get the following error:
I currently using
This is my setup
And here's my class trying to use it
I've tried various options with no joy, any help is appreciated.
Kind regards and many thanks
Si
Hi Simon
is this of any use? http://simpleinjector.codeplex.com/discussions/586263
I've never setup simple injector but I've used Autofac and recently structure map. Both of these require special configuration for use with Umbraco.
There are guides available for both of those to - is there a reason you need to use Simple injector?
Thanks
Carl
Hi Carl,
Thanks for the reply, The error I'm getting when my SufraceController is initiated is:
I did try the configuration mentioned in the link, but the error did not change, the linked page does mention another error, which does not appear to be applicable to my issue:
Thanks so much for trying to help
Cheers Si
is working on a reply...