autofac register InstancePerScope SqlSugarClient报错 返回

SqlSugar 沟通中
16 325

register SqlSugarScope InstanceSingle 没问题

register InstancePerLifetimeScope SqlSugarClient 就会报错

微信图片_20250214183303.png


热忱回答16

  • 葫芦娃 葫芦娃 VIP0
    1个月前

    补充下,查询的是分表数据

    0 回复
  • fate sta fate sta VIP0
    1个月前
    0 回复
  • 葫芦娃 葫芦娃 VIP0
    1个月前
    autofac.Register<ISqlSugarClient>(s =>
    {
        SnowFlakeSingle.WorkId = 1;
        var sqlSugar = new SqlSugarClient(new ConnectionConfig()
        {
            DbType = DbType.MySql,
            ConnectionString = "server=.;uid=sa;pwd=sasa;database=SQLSUGAR4XTEST",
            IsAutoCloseConnection = true,
            InitKeyType = InitKeyType.Attribute,
            ConfigureExternalServices = new ConfigureExternalServices()
            {
                EntityService = (s, p) =>
                {
                }
            }
        },
       db =>
       {
           db.Aop.OnLogExecuting = (sql, pars) =>
           {
               Console.WriteLine(sql);
           };
       });
        if (!db.DbMaintenance.IsAnyTable("Customer", false))
        {
            sqlSugar.CodeFirst.SplitTables()//初始化分表
                         .InitTables<Customer>();
        }
        return sqlSugar;
    }).InstancePerLifetimeScope();
    
    ISqlSugarClient _db;
    public TestController(SugarDb db) { _db = db; }
    [HttpGet]
    public async object Get()
    {
        
        //用例代码 只有一张分表,表里只有三条数据吗,测试用的
        var result = await _db.Queryable<Customer>().SplitTable(Convert.ToDateTime("2025-01-01"), Convert.ToDateTime("2025-03-01")).ToListAsync();
        return result;
    }
    
    //用例实体
    [SplitTable(SplitType.Month)]
    [SugarTable("test_customer_{year}{month}{day}")]
    public class Customer
    {
        [SugarColumn(IsPrimaryKey = true)]
        public long Id { get; set; }
        [SugarColumn(Length = 30, DefaultValue = "''")]
        public string CustomerName { get; set; } = null!;
        [SplitField]
        public DateTime InsertTime { get; set; }
    }


    0 回复
  • 葫芦娃 葫芦娃 VIP0
    1个月前

    我贴了伪代码,因为是autofac做ioc容器的

    0 回复
  • 葫芦娃 葫芦娃 VIP0
    1个月前

    autofac.Register<ISqlSugarClient> 改成 SqlSugarScope且是单例的,就不会有问题,但是文档说client高效,而且我这是分表业务,所以想用client

    0 回复
  • fate sta fate sta VIP0
    1个月前

    提供完整的DEMO,删掉OBJ和BIN上传

    ORM这块没有问题

    因为sqlsugarscope底层就是sqlsugarclient

    0 回复
  • fate sta fate sta VIP0
    1个月前

    我要求提供DEMO必须提供DEMO

    0 回复
  • 葫芦娃 葫芦娃 VIP0
    1个月前

    WebApplicationTest.rar

    demo已上传

    0 回复
  • 葫芦娃 葫芦娃 VIP0
    1个月前

    我一开始以为是数据库问题,我后面又换了个库还是一样问题,用client,我自己单步请求50%几率报错,我用jmeter压测100次,基本都在报错,用scope就不会有问题

    0 回复
  • fate sta fate sta VIP0
    1个月前

    不要给我这么复杂的DEMO,一个WEB项目就够了

    0 回复
  • fate sta fate sta VIP0
    1个月前

    所有代码硬编码不要任何封装

    0 回复
  • 葫芦娃 葫芦娃 VIP0
    1个月前

    我找到原因了,明天详说

    0 回复
  • 因为用了autofac,而且我分了三层,每层的生命周期都是lifetimeScope,然后用client就会报错,就算改成瞬态的也一样会错,这个要怎么办,把client设成single的也不会报错,但是不是不推荐么,只推荐scope用single

    0 回复
  • scope底层就是client 实现 ,不存你说的问题

    0 回复
  • sqlsugarscope 单例原理 本质等于sqlsugarclient ioc是  scope

    sqlsugarscope 对错误代码容错率更高 ,自动实现了new sqlsugarclient

    避免了一些线程乱用问题,但是只要sqlsugarscope能用sqlsugarclient必然是OK的

    因为底层就是sqlsugarclient

    0 回复
  • 那我再试试

    0 回复