V
V
Vlad _2021-04-07 16:35:19
ASP.NET
Vlad _, 2021-04-07 16:35:19

Does SignalR call the OnDisconnectedAsync method after the application stops, not after the user disconnects?

I have a Hub that has the OnConnectedAsync and OnDisconnectedAsync methods overridden, the
OnConnectedAsync method is called correctly, but OnDisconnectedAsync is called not after the user actually disconnects, but after I close the application, but if I manually close the connection on the client by calling the connection.stop method () then everything works fine
Perhaps someone has encountered a similar problem and knows its solution, I would like to know the solution to this problem :)

Hub

private readonly ILogger<NotifyHub> logger;
        private readonly ApplicationContext applicationContext;

        public NotifyHub(ILogger<NotifyHub> logger , ApplicationContext applicationContext)
        {
            this.logger = logger;
            this.applicationContext = applicationContext;
        }

        public override async Task<Task> OnConnectedAsync()
        {
            return base.OnConnectedAsync();
        }

        public override async Task<Task> OnDisconnectedAsync(Exception ex)
        {
            return base.OnDisconnectedAsync(ex);
        }
    }


startup
public partial class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }
        public string ConnectionString { get; private set; }

        public void ConfigureServices(IServiceCollection services)
        {
            ConnectionString = Configuration.GetConnectionString("DefaultConnection");

            InitDbContext(services);
            InitTransient(services);

            services.AddAutoMapper(typeof(Startup));
            services.AddControllers();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseCors("CorsPolicy");

            app.UseSwagger();
            app.UseSwaggerUI(op =>
            {
                op.SwaggerEndpoint("/swagger/v1/swagger.json", "HourBoosting WEB API");
                op.RoutePrefix = "docs";
            });

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapHub<NotifyHub>("/api/hub/notify");
            });
        }
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question