Exemplo n.º 1
0
        public async Task <IList <IListBlockContext <T> > > GetListBlocksAsync <T>(Func <IFluentListBlockDescriptorBase <T>, object> fluentBlockRequest)
        {
            if (!IsExecutionContextActive)
            {
                throw new ExecutionException(NotActiveMessage);
            }

            var fluentDescriptor = fluentBlockRequest(new FluentListBlockDescriptorBase <T>());
            var settings         = (IBlockSettings)fluentDescriptor;

            if (settings.BlockType == BlockType.List)
            {
                var request = ConvertToListBlockRequest(settings);
                if (ShouldProtect(request))
                {
                    var csContext = CreateClientCriticalSection();
                    try
                    {
                        var csStarted = await csContext.TryStartAsync(new TimeSpan(0, 0, 20), 3).ConfigureAwait(false);

                        if (csStarted)
                        {
                            return(await _blockFactory.GenerateListBlocksAsync <T>(request).ConfigureAwait(false));
                        }
                        throw new CriticalSectionException("Could not start a critical section in the alloted time");
                    }
                    finally
                    {
                        await csContext.CompleteAsync().ConfigureAwait(false);
                    }
                }
                else
                {
                    return(await _blockFactory.GenerateListBlocksAsync <T>(request).ConfigureAwait(false));
                }
            }

            throw new NotSupportedException("BlockType not supported");
        }