Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Simon Jackson 11 posts 31 karma points
    Sep 21, 2015 @ 21:51
    Simon Jackson
    0

    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.

    Kind regards and many thanks

    Si

  • Carl Jackson 139 posts 478 karma points
    Sep 22, 2015 @ 11:17
    Carl Jackson
    0

    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

  • Simon Jackson 11 posts 31 karma points
    Sep 22, 2015 @ 21:23
    Simon Jackson
    0

    Hi Carl,

    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.

    Thanks so much for trying to help

    Cheers Si

Please Sign in or register to post replies

Write your reply to:

Draft