Autofac Contianer

Topics: Core, Customizing Orchard, Troubleshooting
Mar 21, 2016 at 2:39 PM

I'm wondering if we can currently access a autofac container object that is available during the lifetime of the application. I'm trying to set up Automapper for my entities that are set up outside of the Orchard content types, but I can't create custom type resolvers at the moment because they need access to the autofac container.

Here is what I'm trying to do:
 public class OrchardShellEvents : IOrchardShellEvents
        public void Activated()
            Mapper.Initialize(x =>
                x.ConstructServicesUsing(type => DependencyResolver.Current.GetService(type));

        public void Terminating()
            //do nothing
This will allow me to use the autofac service to inject dependencies into automapper TypeConverters like this:
    public class PurchaserInfoConverter : ITypeConverter<PurchaserInfoViewModel, PurchaserInfoRecord>
        private readonly IRepository<StateRecord> _stateRepo;
        public PurchaserInfoConverter(IRepository<StateRecord> stateRepo)
            _stateRepo = stateRepo;

        public PurchaserInfoRecord Convert(ResolutionContext context)
            var viewModel = context.SourceValue as PurchaserInfoViewModel;

            return new PurchaserInfoRecord
                Address = viewModel.Address,
                City = viewModel.City,
                Company = viewModel.Company,
                Address2 = viewModel.Address2,
                Email = viewModel.Email,
                FirstName = viewModel.FirstName,
                LastName = viewModel.LastName,
                Phone = viewModel.Phone,
                State = viewModel.StateId.HasValue ? _stateRepo.Get(viewModel.StateId.Value) : null,
                Zip = viewModel.Zip,
                Fax = viewModel.Fax,
                Title = viewModel.Title
But unfortunately I have not been able to figure out how I can get access to a container object that lasts the lifetime of the application. Is it possible to do this?
Mar 29, 2016 at 10:01 AM
I am also interested in this topic.
I need to create CQRS processor, which will be able to return an instance of the specified interface.

Something like:
    public TResult Process<TResult>(IQuery<TResult> query)
            var handlerType = typeof(IQueryHandler<,>).MakeGenericType(query.GetType(), typeof(TResult));

            dynamic handler = this.getInstance(handlerType);

            return handler.Handle((dynamic)query);
How can I get an instance of not known type in compilation time?