예제 #1
0
 internal static async Task InitializeApplicationAsync(this IServiceProvider hostServices, CancellationToken cancellationToken)
 {
     await using (var scope = DisposableAdapter.From(hostServices.CreateScope()))
         foreach (var initializer in scope.Value.ServiceProvider.GetRequiredService <IEnumerable <IApplicationInitializer> >())
         {
             await initializer.InitializeAsync(cancellationToken).ConfigureAwait(false);
         }
 }
예제 #2
0
        public async ValueTask DisposeAsync()
        {
            await DisposableAdapter.From(_scope).DisposeAsync();

            if (_services is IDisposable servicesDisposable)
            {
                await DisposableAdapter.From(servicesDisposable).DisposeAsync();
            }
        }
 public async ValueTask DisposeAsync()
 {
     foreach (var serviceProvider in _internalServiceProviders.Values)
     {
         if (serviceProvider is IDisposable disposable)
         {
             await DisposableAdapter.From(disposable).DisposeAsync().ConfigureAwait(false);
         }
     }
 }
예제 #4
0
        public void ValueTest()
        {
            // Given
            var sut = new DisposableAdapter <int>(1, _ => { });

            // When
            var result = sut.Value;

            // Then
            result.Should().Be(1);
        }
예제 #5
0
        public void DisposeTest()
        {
            // Given
            int?disposedValue = null;
            var sut           = new DisposableAdapter <int>(1, v => disposedValue = v);

            // When
            sut.Dispose();

            // Then
            disposedValue.Should().Be(1);
        }
예제 #6
0
        public void Using()
        {
            var v = 0;
            DisposableAdapter adapter;

            using (adapter = new DisposableAdapter(() => v = Int32.MaxValue))
            {
                adapter.Should().NotBeNull();
                adapter.IsDisposed.Should().BeFalse();
                v.Should().Be(0);
            }

            adapter.Should().NotBeNull();
            adapter.IsDisposed.Should().BeTrue();
            v.Should().Be(Int32.MaxValue);
        }
예제 #7
0
        public async Task <string> RenderAsync <TModel>(string templateName, TModel model, CultureInfo?culture = null, CultureInfo?uiCulture = null, CancellationToken cancellationToken = default)
        {
            await using (var scope = DisposableAdapter.From(_serviceScopeFactory.CreateScope()))
            {
                culture ??= CultureInfo.CurrentCulture;
                uiCulture ??= CultureInfo.CurrentUICulture;

                var templateView = FindTemplateView(templateName);

                var httpContext = new DefaultHttpContext {
                    RequestServices = scope.Value.ServiceProvider
                };
                var actionContext = new ActionContext(httpContext, s_routeData, s_actionDescriptor);

                using (var writer = new StringWriter())
                {
                    var viewData = new ViewDataDictionary <TModel>(s_modelMetadataProvider, new ModelStateDictionary())
                    {
                        Model = model
                    };

                    var tempData = new TempDataDictionary(actionContext.HttpContext, _tempDataProvider);

                    var viewContext = new ViewContext(actionContext, templateView, viewData, tempData, writer, s_htmlHelperOptions);

                    (culture, CultureInfo.CurrentCulture) = (CultureInfo.CurrentCulture, culture);
                    try
                    {
                        (uiCulture, CultureInfo.CurrentUICulture) = (CultureInfo.CurrentUICulture, uiCulture);
                        try
                        {
                            await templateView.RenderAsync(viewContext).AsCancelable(cancellationToken).ConfigureAwait(false);
                        }
                        finally { CultureInfo.CurrentUICulture = uiCulture; }
                    }
                    finally { CultureInfo.CurrentCulture = culture; }

                    return(writer.ToString());
                }
            }
        }
예제 #8
0
        public DisposableAdapter <TResource> Create(TKey oringalKey, bool forceNew = false)
        {
            RcInfo info;
            string usingId = (forceNew) ? Guid.NewGuid().ToString() : TakeUsingId(oringalKey);

            lock (this)
            {
                if (!workingMap.TryGetValue(usingId, out info))
                {
                    info = TakeOneResource(oringalKey);
                    workingMap[usingId] = info;
                }
                info.RefCount += 1;
            }

            var adapter = new DisposableAdapter <TResource>(info.Resource);

            adapter.UsingId           = usingId;
            adapter.ResourceId        = info.ResourceId;
            adapter.DisposeEvent     += OnDisposeEvent;
            adapter.RenewEntityEvent += OnRenewEntityEvent;
            return(adapter);
        }
예제 #9
0
        private async Task ProcessAsync(CancellationToken cancellationToken)
        {
            await using (var scope = DisposableAdapter.From(_serviceScopeFactory.CreateScope()))
            {
                var dbContext = scope.Value.ServiceProvider.GetRequiredService <WritableDataContext>();

                IReadOnlyList <Task <Mail> > produceMailTasks;
                do
                {
                    var queueItems = PeekItems(dbContext);

                    produceMailTasks = await ProduceMailsAsync(queueItems, scope.Value.ServiceProvider, cancellationToken).ConfigureAwait(false);

                    if (produceMailTasks.Count > 0)
                    {
                        await SendMailsAsync(produceMailTasks, dbContext, cancellationToken).ConfigureAwait(false);
                    }
                    else
                    {
                        return;
                    }
                }while (_batchSize > 0 && produceMailTasks.Count >= _batchSize);
            }
        }
예제 #10
0
 public ValueTask DisposeAsync()
 {
     return(DisposableAdapter.From(_serviceScope).DisposeAsync());
 }