Timeout expired. XAF .Net6 Winforms Entity Framework Core

152 Views Asked by At

I am troubleshooting why so many sessions are open in SQL Server with my XAF 23.1.6 EF Core 7.0.3 project.

In SSMS I ran

SELECT *
FROM sys.dm_exec_sessions
WHERE session_id > 50 AND host_name = 'MYCOMPUTER'
ORDER BY last_request_end_time ASC

To see what sessions were open.

I created a new project from the wizard with standard security and ran it in the Visual Studio IDE whilst monitoring the sessions in SSMS.

It seems that the number of sessions only increases as I use the program.

For example if I open the Users View and then the Roles View the sessions increase. But the sessions do not decrease when I close these views.

How do I get XAF to close unneeded sessions?

The user experience is time out errors.

[Update]

The DbContext code is

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);
    }
}

[Update]

Demo project at GitHub here

[Update]

I was able to repeat the issue with a simple console project that simply opened a dbcontext added a record and closed. It appears that sessions are not closed until the program exits.

[Update]

From this question it appears I am on the wrong track investigating the number of open sessions to resolve why the application is timing out.

It turns out that in some of my code ( not the linked demo ) I have

Some of my code ( not in the sample ) is like the following;

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);
    // the following line was missing
    detailView.Closed += (sender, args) => ((DetailView)sender).ObjectSpace.Dispose();
    e.ShowViewParameters.CreatedView = detailView;
    e.ShowViewParameters.NewWindowTarget = NewWindowTarget.MdiChild;
    e.ShowViewParameters.TargetWindow = TargetWindow.NewWindow;
}

It makes sense that this is the problem. I will know in the next few days wether the issue is fixed.

0

There are 0 best solutions below