Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="invocation"></param>
        /// <returns></returns>
        public async Task InterceptAsync(IMethodInvocation invocation)
        {
            var options = CreateOptions();

            if (invocation.Method.AttributeExists <DisableUnitOfWorkAttribute>())
            {
                options.Scope = System.Transactions.TransactionScopeOption.Suppress;
            }
            using (var uow = _unitOfWorkManager.Begin(options))
            {
                await invocation.ProceedAsync();

                if (invocation.IsAsync())
                {
                    await uow.CompleteAsync();
                }
                else
                {
                    uow.Complete();
                }
            }
        }
Example #2
0
        public void IsAsync()
        {
            IMethodInvocation methodInvocation = null;

            Should.Throw <ArgumentNullException>(() => methodInvocation.IsAsync());
            methodInvocation = Substitute.For <IMethodInvocation>();
            methodInvocation.Configure().Method.Returns(((Func <Task <int> >)(() => Task.FromResult(1))).Method);
            Should.NotThrow(() => methodInvocation.IsAsync()).ShouldBeTrue();
            methodInvocation.Configure().Method.Returns(((Func <int>)(() => 1)).Method);
            methodInvocation.Configure().ReturnValue.Returns(Task.FromResult(1));
            Should.NotThrow(() => methodInvocation.IsAsync()).ShouldBeTrue();
            methodInvocation.Configure().ReturnValue.Returns(1);
            Should.NotThrow(() => methodInvocation.IsAsync()).ShouldBeFalse();
            methodInvocation.Configure().ReturnValue.Returns(Task.CompletedTask);
            Should.NotThrow(() => methodInvocation.IsAsync()).ShouldBeTrue();
            methodInvocation.Configure().ReturnValue.Returns(new ValueTask <int>(1));
            Should.NotThrow(() => methodInvocation.IsAsync()).ShouldBeTrue();
            methodInvocation.Configure().ReturnValue.Returns(new ValueTask());
            Should.NotThrow(() => methodInvocation.IsAsync()).ShouldBeTrue();
        }