XAF 23.1.6 EF Core项目SQL Server会话未关闭及超时问题排查
XAF 23.1.6 + EF Core 7.0.3 未关闭SQL Server会话问题排查
我正在排查XAF 23.1.6 + EF Core 7.0.3项目中SQL Server存在大量未关闭会话的问题,通过SSMS执行以下查询查看开放会话:
SELECT * FROM sys.dm_exec_sessions WHERE session_id > 50 AND host_name = 'MYCOMPUTER' ORDER BY last_request_end_time ASC
通过向导创建带标准安全的新项目,在Visual Studio中运行并监控SSMS会话,发现操作程序时会话数量只增不减——比如打开用户视图和角色视图后会话增加,但关闭视图后会话并未减少,最终导致用户体验出现超时错误。
更新记录
- DbContext代码:
using DevExpress.ExpressApp.Design; using DevExpress.ExpressApp.EFCore.DesignTime; using DevExpress.Persistent.BaseImpl.EF; using DevExpress.Persistent.BaseImpl.EF.PermissionPolicy; using Microsoft.EntityFrameworkCore; namespace MyApp.Module.BusinessObjects; public class MyAppContextInitializer : DbContextTypesInfoInitializerBase { protected override DbContext CreateDbContext() { var optionsBuilder = new DbContextOptionsBuilder<MyAppEFCoreDbContext>() .UseSqlServer(";") .UseChangeTrackingProxies() .UseObjectSpaceLinkProxies(); return new MyAppEFCoreDbContext(optionsBuilder.Options); } } [TypesInfoInitializer(typeof(MyAppContextInitializer))] public class MyAppEFCoreDbContext : DbContext { public MyAppEFCoreDbContext(DbContextOptions<MyAppEFCoreDbContext> options) : base(options) { } public DbSet<ModelDifference> ModelDifferences { get; set; } public DbSet<ModelDifferenceAspect> ModelDifferenceAspects { get; set; } public DbSet<PermissionPolicyRole> Roles { get; set; } public DbSet<MyApp.Module.BusinessObjects.ApplicationUser> Users { get; set; } public DbSet<MyApp.Module.BusinessObjects.ApplicationUserLoginInfo> UserLoginInfos { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.HasChangeTrackingStrategy(ChangeTrackingStrategy.ChangingAndChangedNotificationsWithOriginalValues); modelBuilder.UsePropertyAccessMode(PropertyAccessMode.PreferFieldDuringConstruction); modelBuilder.Entity<MyApp.Module.BusinessObjects.ApplicationUserLoginInfo>( b => { b.HasIndex( nameof(DevExpress.ExpressApp.Security.ISecurityUserLoginInfo.LoginProviderName), nameof(DevExpress.ExpressApp.Security.ISecurityUserLoginInfo.ProviderUserKey)) .IsUnique(); }); modelBuilder.Entity<ModelDifference>() .HasMany(t => t.Aspects) .WithOne(t => t.Owner) .OnDelete(DeleteBehavior.Cascade); } }
- 演示项目:已准备演示项目并上传至代码仓库。
- 问题重现:在简单控制台项目中重现了问题:打开DbContext添加记录后关闭,会话直到程序退出才关闭。
- 问题根源定位:通过相关技术资料得知,此前通过开放会话数排查超时错误的方向有误,最终发现部分自定义代码存在问题:
public static void StartPurchaseOrderDetailView(XafApplication application, ActionBaseEventArgs e) { var os = application.CreateObjectSpace(typeof(PurchaseOrder)); var h = os.CreateObject<PurchaseOrder>(); var detailView = application.CreateDetailView(os, h); // 以下代码缺失 detailView.Closed += (sender, args) => ((DetailView)sender).ObjectSpace.Dispose(); e.ShowViewParameters.CreatedView = detailView; e.ShowViewParameters.NewWindowTarget = NewWindowTarget.MdiChild; e.ShowViewParameters.TargetWindow = TargetWindow.NewWindow; }
上述代码中缺失了detailView.Closed事件绑定的Dispose逻辑,导致ObjectSpace未被及时释放,进而引发数据库会话无法关闭。后续将验证该修复方案是否生效。
内容的提问来源于stack exchange,提问作者Kirsten
相关产品推荐
相关产品推荐

