1

我有一个 DevExpress 示例 mvc 网站应用程序。它使用温莎城堡作为国际奥委会。我只是尝试通过 Autofac 替换,但没有运气!

这是示例代码:

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>())

这是我的 Autofac 转换:

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();

关于我的研究,Autofac 上没有 AsFactory() 的实现。

这是IAdminContextFactory界面:

public interface IAdminContextFactory
{
    AdminContext Retrieve();
}

这是错误应用程序说:

在“公共绑定标志”中找不到“Admin.Infrastructure.EFRepository.IAdminContextFactory”类型的构造函数。

谁能帮忙?

谢谢。

4

1 回答 1

3

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>();
于 2012-12-11T08:55:30.593 回答