使用Autofac.Extras.NHibernate时出错

使用Autofac.Extras.NHibernate时出错,nhibernate,autofac,Nhibernate,Autofac,我试图在我的模型中注入依赖项,这是NHibernate创建的 我想做的和这里一样: 但我的容器是Autofac 所以,我发现 我看到了我认为是Autofac.Extras.NHibernate的起源的帖子 我的问题是Autofac.Extras.NHibernate中的代码与Chad post中描述的代码不同 查看源代码,我(认为)找到了如何使用以下方法设置字节码提供程序: Cfg.Environment.BytecodeProvider = new AutofacBytecodeProvide

我试图在我的模型中注入依赖项,这是NHibernate创建的

我想做的和这里一样:

但我的容器是Autofac

所以,我发现

我看到了我认为是Autofac.Extras.NHibernate的起源的帖子

我的问题是Autofac.Extras.NHibernate中的代码与Chad post中描述的代码不同

查看源代码,我(认为)找到了如何使用以下方法设置字节码提供程序:

Cfg.Environment.BytecodeProvider = new AutofacBytecodeProvider(Container, new DefaultProxyFactoryFactory(), new DefaultCollectionTypeFactory());
但现在,当我尝试从数据库检索数据时,遇到了一个异常:

[PropertyAccessException:无法通过NHibernate.Autofac2.App_Start.Model.User.Id的反射设置程序设置属性值]

如果我对设置字节码提供程序的行进行注释,代码就会起作用

我创建了一个POC来模拟:

我的模型:

 public class User
{
    private readonly ISomeService _someService;

    public User(ISomeService someService)
    {
        this._someService = someService;
    }

    public virtual long Id { get; set; }

    public virtual string Name { get; set; }

    public virtual string GetTranslate
    {
        get { return this._someService != null ? this._someService.T(this.Name) : " No Translate"  + this.Name; }
    }
}
我的映射:

 public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Id(x => x.Id);
        Map(x => x.Name)
          .Length(16)
          .Not.Nullable();

    }

}
公共类用户映射:类映射
{
公共用户映射()
{
Id(x=>x.Id);
映射(x=>x.Name)
.长度(16)
.Not.Nullable();
}
}
使用Fluent Nhibernate创建Autofac容器和SessionFactory:

            // Create your builder.
        var builder = new ContainerBuilder();

        builder.RegisterType<SomeService>().As<ISomeService>();
        builder.RegisterType<User>().As<IUser>();

        Container = builder.Build();

        SessionFactory = Fluently.Configure()
                                 .Database(MsSqlConfiguration.MsSql2005.ConnectionString("Data Source=(local);Initial Catalog=NHibernate.Autofac;User ID=test;Password=102030;Pooling=True"))
                                 .Mappings(m => m.FluentMappings.AddFromAssemblyOf<MvcApplication>())
                                 .ExposeConfiguration(config => config.Properties.Add("use_proxy_validator", "false"))
                                 .ExposeConfiguration(config =>
                                     {
                                         //config.Properties.Add("proxyfactory.factory_class", "");
                                         Cfg.Environment.BytecodeProvider = new AutofacBytecodeProvider(Container, new DefaultProxyFactoryFactory(), new DefaultCollectionTypeFactory());

                                         new SchemaExport(config).Drop(false, false);
                                         new SchemaExport(config).Create(false, true);
                                     })
                                 .BuildSessionFactory();
//创建生成器。
var builder=new ContainerBuilder();
builder.RegisterType().As();
builder.RegisterType().As();
Container=builder.Build();
SessionFactory=fluntly.Configure()
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(“数据源=(本地);初始目录=NHibernate.Autofac;用户ID=测试;密码=102030;池=真”))
.Mappings(m=>m.FluentMappings.AddFromAssemblyOf())
.ExposeConfiguration(config=>config.Properties.Add(“使用代理验证程序”、“false”))
.ExposeConfiguration(配置=>
{
//config.Properties.Add(“proxyfactory.factory_class”,”);
Cfg.Environment.BytecodeProvider=新的AutofacBytecodeProvider(容器,新的DefaultProxyFactoryFactory(),新的DefaultCollectionTypeFactory());
新建SchemaExport(config).Drop(false,false);
新建SchemaExport(config).Create(false,true);
})
.BuildSessionFactory();

嗯,我找到了一个适合我的解决方案

现在,我正在使用

IEntityInjector实现:

    public class EntityInjector : IEntityInjector
{
    private readonly IContainer _container;

    public EntityInjector(IContainer container)
    {
        _container = container;
    }

    public object[] GetConstructorParameters(System.Type type)
    {
        var constructor = type.GetConstructors().FirstOrDefault();

        if (constructor != null)
            return constructor.GetParameters().Select(a => a.ParameterType).Select(b => this._container.Resolve(b)).ToArray();

        return null;
    }
}
在Global.asax中:

            Initializer.RegisterBytecodeProvider(new EntityInjector(Container));

        SessionFactory = Fluently.Configure()
                                 .Database(MsSqlConfiguration.MsSql2005.ConnectionString("Data Source=(local);Initial Catalog=NHibernate.Autofac;User ID=XXX;Password=XXXX;Pooling=True"))
                                 .Mappings(m => m.FluentMappings.AddFromAssemblyOf<MvcApplication>())
                                 .ExposeConfiguration(config => config.Properties.Add("use_proxy_validator", "false"))
                                 .ExposeConfiguration(config =>
                                     {
                                         new SchemaExport(config).Drop(false, false);
                                         new SchemaExport(config).Create(false, true);
                                     })
                                 .BuildSessionFactory();
Initializer.RegisterBytecodeProvider(新的EntityInjector(容器));
SessionFactory=fluntly.Configure()
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(“数据源=(本地);初始目录=NHibernate.Autofac;用户ID=XXX;密码=XXXX;池=True”))
.Mappings(m=>m.FluentMappings.AddFromAssemblyOf())
.ExposeConfiguration(config=>config.Properties.Add(“使用代理验证程序”、“false”))
.ExposeConfiguration(配置=>
{
新建SchemaExport(config).Drop(false,false);
新建SchemaExport(config).Create(false,true);
})
.BuildSessionFactory();