public async Task LongRunning() { ServiceContainer services = new ServiceContainer(); services.AddScopedPool <ISingleService, SingleService>(); for (int i = 0; i < 50; i++) { IContainerScope scope1 = services.CreateScope(); IContainerScope scope2 = services.CreateScope(); ISingleService service1 = await services.Get <ISingleService>(scope1); ISingleService service2 = await services.Get <ISingleService>(scope2); Assert.NotNull(service1); Assert.NotNull(service2); Assert.NotEqual(service1, service2); await Task.Delay(10); scope1.Dispose(); scope2.Dispose(); } }
public void Consumed(Exception error) { if (_scope != null) { _scope.Dispose(); _scope = null; } }
/// <summary> /// Triggered when a non-websocket request available. /// </summary> private async Task RequestAsync(ITwinoServer server, HttpRequest request, HttpResponse response) { IContainerScope scope = Mvc.Services.CreateScope(); try { if (App.Descriptors.Count > 0) { MiddlewareRunner runner = new MiddlewareRunner(Mvc, scope); await runner.RunSequence(App, request, response); if (runner.LastResult != null) { WriteResponse(response, runner.LastResult); return; } } await RequestMvc(server, request, response, scope); } catch (Exception ex) { if (Mvc.IsDevelopment) { IErrorHandler handler = new DevelopmentErrorHandler(); await handler.Error(request, ex); } else if (Mvc.ErrorHandler != null) { await Mvc.ErrorHandler.Error(request, ex); } else { WriteResponse(request.Response, StatusCodeResult.InternalServerError()); } if (request.Response.StreamSuppressed && request.Response.ResponseStream != null) { GC.ReRegisterForFinalize(request.Response.ResponseStream); } } finally { scope.Dispose(); } }
public async Task WaitLimitAndGet() { ServiceContainer services = new ServiceContainer(); services.AddTransientPool <ISingleService, SingleService>(o => { o.PoolMaxSize = 10; o.ExceedLimitWhenWaitTimeout = false; o.WaitAvailableDuration = TimeSpan.FromMilliseconds(5000); }); IContainerScope scope = services.CreateScope(); for (int i = 0; i < 10; i++) { ISingleService service = await services.Get <ISingleService>(scope); Assert.NotNull(service); } DateTime start = DateTime.UtcNow; Thread th = new Thread(() => { Thread.Sleep(500); scope.Dispose(); }); th.Start(); IContainerScope scope2 = services.CreateScope(); ISingleService s = await services.Get <ISingleService>(scope2); Assert.NotNull(s); DateTime end = DateTime.UtcNow; TimeSpan time = end - start; Assert.True(time > TimeSpan.FromMilliseconds(490)); Assert.True(time < TimeSpan.FromMilliseconds(750)); }