Reputation: 457
I have a DevExpress example mvc web site application. It use Castle Windsor as an IOC. I just tried to replace via Autofac but no luck!
here is the sample code:
container
.Register(Component
.For<DbRepositories.ClinicalStudyContext>()
.LifestylePerWebRequest()
.DependsOn(new { connectionString }))
.Register(Component
.For<DbRepositories.IClinicalStudyContextFactory>()
.AsFactory())
.Register(Component
.For<FirstStartInitializer>()
.LifestyleTransient())
.Register(Component
.For<IUserRepository>()
.ImplementedBy<DbRepositories.UserRepository>())
and this is my Autofac conversion:
var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.Register(c =>
new DbRepositories.AdminContext(connectionString))
.InstancePerHttpRequest();
builder.RegisterType<DbRepositories.IAdminContextFactory>()
.As<DbRepositories.IAdminContextFactory>();
builder.RegisterType<DbRepositories.UserRepository>()
.As<IUserRepository>().InstancePerHttpRequest();
there is no implementetion for AsFactory() on Autofac regarding my research.
this is the IAdminContextFactory
interface:
public interface IAdminContextFactory
{
AdminContext Retrieve();
}
and this is the error application says:
No constructors on type 'Admin.Infrastructure.EFRepository.IAdminContextFactory' can be found with 'Public binding flags'.
can anyone help?
thanks.
Upvotes: 1
Views: 2388
Reputation: 33920
Your IAdminContextFactory
registration will fail, because the first part of the RegisterType
must be a service type. In this case, a class that implements the IAdminContextFactory
interface. Autofac tries to build an instance of the type, which certainly will fail because you cannot instantiate an interface.
So, what you need is an implementation of the IAdminContextFactory
interface. The Castle AsFactory
method generates this implementation for you. You can get this behavior with the Autofac AggregateService extra.
With the bits in place you can do:
builder.RegisterAggregateService<IAdminContextFactory>();
Upvotes: 3