Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

job [LogTableRoute:Log] invoke fail : System.ObjectDisposedException: Cannot access a disposed object. #279

Open
JiaDongming opened this issue Sep 24, 2024 · 7 comments

Comments

@JiaDongming
Copy link

JiaDongming commented Sep 24, 2024

我针对我的日志表Log进行了每日分表,我用的ShardingCore的库是7.6.1.10,对应ef core是6.x的版本;
LogTableRoute.cs
`public class LogTableRoute : AbstractSimpleShardingDayKeyDateTimeVirtualTableRoute
//AbstractSimpleShardingMonthKeyDateTimeVirtualTableRoute
{
public override bool AutoCreateTableByTime()
{
return true;
}

 public override void Configure(EntityMetadataTableBuilder<Log> builder)
 {
     builder.ShardingProperty(o => o.TimeStamp);
     builder.AutoCreateTable(true);//自动建表
     builder.TableSeparator("_");//间隔符
 }

 public override DateTime GetBeginTime()
 {
     return new DateTime(2023, 12, 28);
 }

}依赖注入的地方是:
///


/// 注册日志到数据库模块
///

///
/// myslq的连接字符串,暂时只支持mysql
///
///
public static IServiceCollection AddLogDatabaseSharding(this IServiceCollection services, string connectionString)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}

  //参考的分库分表开源库是:https://github.com/dotnetcore/sharding-core
  //参考文档:https://xuejm.gitee.io/sharding-core-doc/sharding-table/init/
  services.AddDatabaseAccessor(options => {
      // 配置默认数据库
      options.AddDb<LogDbContext>((sp, o) => {
          ShardingCoreExtension.UseDefaultSharding<LogDbContext>(sp, o);
      });
  });

  //依赖注入
  services.AddShardingDbContext<LogDbContext>()
      .UseRouteConfig(op => {
          op.AddShardingTableRoute<LogTableRoute>();
          
      })
      .UseConfig((sp, op) => {
          op.UseShardingQuery((con, b) => {
              b.UseMySql(con, new MySqlServerVersion(new Version()));
                  //.UseLoggerFactory(efLogger);
          });
          op.UseShardingTransaction((con, b) => {
              b.UseMySql(con, new MySqlServerVersion(new Version()));
                  //.UseLoggerFactory(efLogger);
          });
          op.AddDefaultDataSource("ds0", connectionString);
          op.UseShardingMigrationConfigure(b => {
              b.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>();
          });
      }).ReplaceService<ITableEnsureManager, SqlServerTableEnsureManager>().AddShardingCore();

  return services;

}
`
以下是报错信息

info: 2024-09-24 10:06:49.9177837 +08:00 Tuesday L ShardingCore.Jobs.JobRunnerService[0] #113
### job [LogTableRoute:Log] start success.
fail: 2024-09-24 10:06:49.9183123 +08:00 Tuesday L ShardingCore.Jobs.JobRunnerService[0] #113
### job [LogTableRoute:Log] invoke fail : System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'LoggerFactory'.
at Microsoft.Extensions.Logging.LoggerFactory.CreateLogger(String categoryName)
at Microsoft.Extensions.Logging.Logger1..ctor(ILoggerFactory factory) at System.RuntimeMethodHandle.InvokeMethod(Object target, Span1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType)
at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
at ShardingCore.Core.ServiceProviders.ShardingProvider.GetService(Type serviceType, Boolean tryApplicationServiceProvider)
at ShardingCore.Core.ServiceProviders.ShardingProvider.GetService[TService](Boolean tryApplicationServiceProvider)
at ShardingCore.VirtualRoutes.Abstractions.AbstractShardingAutoCreateOperatorVirtualTableRoute`2.ExecuteAsync()
at ShardingCore.Jobs.JobRunnerService.<>c__DisplayClass9_0.<b__0>d.MoveNext().

@JiaDongming
Copy link
Author

请问有人遇见过这个问题么?

@xuejmnet
Copy link
Collaborator

@JiaDongming addDatabaseAccessor是啥东西我记得sharding-core貌似没有的

@JiaDongming
Copy link
Author

@JiaDongming addDatabaseAccessor是啥东西我记得sharding-core貌似没有的

你好,我用了furion框架,这个是他提供的一个扩展方法,你怀疑是这个引起的问题么?

@JiaDongming
Copy link
Author

看报错信息像是ShardingCore里有个job

@JiaDongming
Copy link
Author

看代码里 像是执行这个AbstractShardingAutoCreateOperatorVirtualTableRoute类的ExecuteAsync()方法时,在获取 var logger=RouteShardingProvider
.GetService<ILogger<AbstractShardingAutoCreateOperatorVirtualTableRoute<TEntity, TKey>>>();的时候没有拿到这个log

image

@xuejmnet
Copy link
Collaborator

xuejmnet commented Oct 5, 2024

@JiaDongming 你是否有注入ILogger?

@xuejmnet
Copy link
Collaborator

xuejmnet commented Oct 5, 2024

原则上这个应该是不会报错的才对

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants