Copied to clipboard

Flag this post as spam?

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


  • sohail 5 posts 75 karma points
    May 29, 2017 @ 07:35
    sohail
    0

    Umbraco Backend issue when setting up Umbraco API with Dependency Injection (Auto fac)

    Hello Everyone,

    I have set up my IOC using autofac for the Dependency Injection, then I am able to resolve dependencies in my controllers as shown in this post

    https://our.umbraco.org/forum/developers/api-questions/41794-Web-API-UmbracoApiController-does-not-use-DependencyResolver?p=1

    It's working fine, but the problem is when i logged into the umbraco backend i got this error.

    https://www.screencast.com/t/f54kNM9SN7J

    I am using Umbraco 7.6.1 please help me out.

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    May 29, 2017 @ 07:44
    Dave Woestenborghs
    0

    Hi Sohail,

    You problably need to register the Umbraco Api Controllers

    builder.RegisterApiControllers(typeof(UmbracoApplication).Assembly);
    

    Dave

  • sohail 5 posts 75 karma points
    May 30, 2017 @ 07:39
    sohail
    0

    Hi Dave,

    Yes, I have registered the Umbraco API Controllers and it's working fine. API controller received my requests and dependency Injection also applied but the problem is whenever, I tried to login to Umbraco Backend its give me this error see the screenshot which is mention below :

    https://www.screencast.com/t/f54kNM9SN7J

    It will not render any tree and other Umbraco related stuff.

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                var builder = new ContainerBuilder();
    
            //Register our controllers from this assembly with Autofac
            builder.Register(c => UmbracoContext.Current).AsSelf();
            builder.RegisterInstance(ApplicationContext.Current).AsSelf();
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            //builder.RegisterApiControllers(typeof(UmbracoApplication).Assembly);
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
    
            //Register types 
            builder.RegisterType<DataFactory>().As<IDataFactory>();
    
    
            builder.RegisterAssemblyTypes(typeof(HeaderRepository).Assembly)
           .Where(t => t.Name.EndsWith("Repository"))
           .AsImplementedInterfaces().InstancePerRequest();
            builder.RegisterAssemblyTypes(typeof(HeaderService).Assembly)
           .Where(t => t.Name.EndsWith("Service"))
           .AsImplementedInterfaces().InstancePerRequest();
    
            //Set up MVC to use Autofac as a dependency resolver
            var container = builder.Build();
            var resolver = new AutofacWebApiDependencyResolver(container);
            GlobalConfiguration.Configuration.DependencyResolver = resolver;
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
    
  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    May 30, 2017 @ 07:44
    Dave Woestenborghs
    0

    Hi Sohail,

    The line to register the Umbraco Api controllers is in comments in the code you posted. Maybe that's the issue ?

  • sohail 5 posts 75 karma points
    May 31, 2017 @ 08:02
  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    May 31, 2017 @ 08:13
    Dave Woestenborghs
    0

    Hi Sohail,

    Can you mark the answer as a solution. So others can find it easier.

    Dave

Please Sign in or register to post replies

Write your reply to:

Draft