0

I'm new to AutoFac and i am trying to auto inject my property with the instance that i registered with AutoFac.

  public class PersonalDataProvider
    {
        public ISocialAppUnitOfWork SocialAppUnitOfWork
        {
            get
            {
                var resolver = Setup();
                return resolver.Resolve<ISocialAppUnitOfWork>();
            }
        }

        private IContainer Setup()
        {
            var builder = new ContainerBuilder();
            builder.RegisterType<ISocialAppUnitOfWork>().PropertiesAutowired();
            return builder.Build();
        }
    }

The line that causes the except: builder.RegisterType().PropertiesAutowired();

I'd like to get the ISocialAppUnitOfWork of the provider.

I'm not sure what's going on at the moment, any help would be appreciated.

Jamie
  • 3,031
  • 5
  • 36
  • 59
  • Probably because the Setup code is inside the class you are trying to wire up. – DavidG Sep 24 '14 at 12:05
  • Jamie your public property `SocialAppUnitOfWork` is exposed for what reason? Whatever uses that should just take a dependency on `ISocialAppUnitOfWork` and not get it from `PersonalDataProvider` – wal Sep 24 '14 at 12:07
  • @wal Because i need to look something up in the SocialAppUnitOfWork. It's a one line code that i need to get from this interface in my view. Otherwise i will need to create a new controller for it. – Jamie Sep 24 '14 at 12:11
  • where does `PersonalDataProvider` get new-ed? you should instead register `PersonalDataProvider` with Autofac and this class should take a dependency (via ctor) on `ISocialAppUnitOfWork`. you can't do this `builder.RegisterType()` (assuming your ISocialAppUnitOfWork is an interface) as you're not defining what the concrete implementation is. – wal Sep 24 '14 at 12:20
  • With Autofac you should not be building/registering in this fashion (ie each time your SocialAppUnitOfWork property is called) - you need to register ONCE and then keep a reference to the IContainer. – wal Sep 24 '14 at 12:23
  • This line: `builder.RegisterType().PropertiesAutowired();` will fail because you can't `RegisterType` on an interface. It needs to be a concrete class. – Jim Bolla Sep 25 '14 at 13:14

0 Answers0