首页 新闻 会员 周边

关于 Autofac 生命周期

1
悬赏园豆:10 [已解决问题] 解决于 2016-04-18 23:52

初学Autofac对生命周期不熟悉.

问题是这样的:

当我使用InstancePerLifetimeScope 注册时在 TestServer 服务中 使用 RepositoryContext 和 UnitOfWork 是同一个对象 。这个服务正常. 但其他的服务(MenuServer、CatalogServer)使用 RepositoryContext 和 UnitOfWork 抛出异常:数据库链接已关闭.

原因是请求 一个页面 调用多个服务 他用的 RepositoryContext 和 UnitOfWork 还是之前使用过的对象。但链接被关了 所以抛出异常:数据库链接已关闭.

然后我改成 InstancePerDependency (对每一个依赖或每一次调用创建一个新的唯一的实例) 注册时。
TestServer 服务中使用了 UnitOfWork 跨服务事务提交。结果还是抛出异常: 事务不是与当前连接无关联,就是已完成。

现在不知道怎么弄了请各位赐教.

 

注册代码

 public void Registrar(ContainerBuilder builder, ITypeFinder typeFinder)
        {// 注册仓库
            builder.RegisterType<RepositoryContext>().As<IRepositoryContext>().InstancePerLifetimeScope();
            builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerLifetimeScope();
            builder.RegisterGeneric(typeof(Reprtory<>)).As(typeof(IReprtory<>)).InstancePerLifetimeScope();


            // 服务注册
            builder.RegisterType<CustomerServer>().As<ICustomerServer>().PropertiesAutowired().InstancePerLifetimeScope();// 注册客户业务
            builder.RegisterType<MenuServer>().As<IMenuServer>().PropertiesAutowired().InstancePerLifetimeScope();// 注册菜单
            builder.RegisterType<CatalogServer>().As<ICatalogServer>().PropertiesAutowired().InstancePerDependency();// 注册商品目录
            builder.RegisterType<TestServer>().As<ITextServer>().PropertiesAutowired().InstancePerLifetimeScope();//测试
            builder.RegisterType<Test1Server>().As<ITextServer1>().PropertiesAutowired().InstancePerLifetimeScope();//测试1

        }

服务代码

 public class TestServer : Reprtory<Test>, ITextServer
    {
        public TestServer(IRepositoryContext context,
                          IUnitOfWork unitOfWork
                        ): base(context, unitOfWork){
        }

        public void t()
        {
            Test t1= new Test() { Name = "0aasfd35", Value = 31 };
            Test1 t2 = new Test1() { Name = "122343", Value = 33300 };

            try
            {
                UnitOfWork.BeginTran();
                var e = GetById(1);
                Inser(t1);

                Test1Server x1 = new Test1Server(Context, UnitOfWork);
                x1.t();
                UnitOfWork.Commit();
            }
            catch (Exception)
            {
                UnitOfWork.Rollback();
            }
        }
    }

    public class Test1Server : Reprtory<Test1>, ITextServer1
    {
        public Test1Server(IRepositoryContext context,
                          IUnitOfWork unitOfWork
          )
            : base(context, unitOfWork) { }

        public void t()
        {

            Test1 o1 = new Test1() { Name = "0", Value = 1 };
            Test1 o2 = new Test1() { Name = "123", Value = 1000 };

            
            Inser(o1);
            Inser(o2);
            
        }
    }

 

  public class RepositoryContext : DisposableObject, IRepositoryContext
    {
        public RepositoryContext()
        {
            InitConnection();
        }

        private IDbConnection _dbConnection;
        public IDbConnection DbConnection { get { return  _dbConnection; } }

        /// <summary>
        /// 初始化
        /// </summary>
        public void InitConnection()
        {
            if (this._dbConnection == null)
                this._dbConnection = new SqlConnection(ConfigSettings.DBconnectionString);
            if (this._dbConnection.State == ConnectionState.Closed)
                this._dbConnection.Open();
        }


        protected override void Dispose(bool disposing)
        {
            if (!disposing) return;
            if (this._dbConnection.State != ConnectionState.Open) return;
            this._dbConnection.Close();
            this._dbConnection.Dispose();
        }
    }
mycing的主页 mycing | 初学一级 | 园豆:197
提问于:2016-03-08 20:43
< >
分享
最佳答案
0

注册代码改为:

 // 注册仓库
            IRepositoryContext _repositoryContext = null;
            builder.Register(c => {
                _repositoryContext = new RepositoryContext();
                return _repositoryContext;
            }).As<IRepositoryContext>().InstancePerDependency();
            
            builder.Register(c => {
                var unitOfWork = new UnitOfWork(_repositoryContext);
                return unitOfWork;
            }).As<IUnitOfWork>().InstancePerLifetimeScope();

即可.

mycing | 初学一级 |园豆:197 | 2016-03-08 23:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册