Merge pull request #14399 from abpframework/auto-merge/rel-6-0/1416

Merge branch dev with rel-6.0
pull/14130/head^2
maliming 3 years ago committed by GitHub
commit a8f0692a70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -98,7 +98,15 @@ public class AbpUowActionFilter : IAsyncActionFilter, ITransientDependency
var currentUow = unitOfWorkManager.Current;
if (currentUow != null)
{
await currentUow.SaveChangesAsync(context.HttpContext.RequestAborted);
try
{
await currentUow.SaveChangesAsync(context.HttpContext.RequestAborted);
}
catch (Exception e)
{
await currentUow.RollbackAsync(context.HttpContext.RequestAborted);
throw;
}
}
}

@ -104,7 +104,15 @@ public class AbpUowPageFilter : IAsyncPageFilter, ITransientDependency
var currentUow = unitOfWorkManager.Current;
if (currentUow != null)
{
await currentUow.SaveChangesAsync(context.HttpContext.RequestAborted);
try
{
await currentUow.SaveChangesAsync(context.HttpContext.RequestAborted);
}
catch (Exception e)
{
await currentUow.RollbackAsync(context.HttpContext.RequestAborted);
throw;
}
}
}

@ -0,0 +1,32 @@
using System;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Volo.Abp.Data;
using Volo.Abp.Uow;
using Volo.Abp.Users;
namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling;
public class ExceptionHandingUnitOfWork : UnitOfWork
{
public ExceptionHandingUnitOfWork(
IServiceProvider serviceProvider,
IUnitOfWorkEventPublisher unitOfWorkEventPublisher,
IOptions<AbpUnitOfWorkDefaultOptions> options)
: base(serviceProvider, unitOfWorkEventPublisher, options)
{
}
public async override Task SaveChangesAsync(CancellationToken cancellationToken = default)
{
if (ServiceProvider.GetRequiredService<ICurrentUser>().Id == Guid.Empty)
{
throw new AbpDbConcurrencyException();
}
await base.SaveChangesAsync(cancellationToken);
}
}

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Authorization;
namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling;
@ -26,4 +27,11 @@ public class ExceptionTestController : AbpController
{
throw new AbpAuthorizationException("This is a sample exception!");
}
[HttpGet]
[Route("ExceptionOnUowSaveChange")]
public Task<string> ExceptionOnUowSaveChangeAsync()
{
return Task.FromResult("OK");
}
}

@ -3,12 +3,14 @@ using System.Net;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using NSubstitute;
using Shouldly;
using Volo.Abp.ExceptionHandling;
using Volo.Abp.Http;
using Volo.Abp.Security.Claims;
using Volo.Abp.Uow;
using Xunit;
namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling;
@ -31,6 +33,8 @@ public class ExceptionTestController_Tests : AspNetCoreMvcTestBase
_fakeExceptionSubscriber = Substitute.For<IExceptionSubscriber>();
services.AddSingleton(_fakeExceptionSubscriber);
services.Replace(ServiceDescriptor.Transient<IUnitOfWork, ExceptionHandingUnitOfWork>());
}
[Fact]
@ -93,4 +97,24 @@ public class ExceptionTestController_Tests : AspNetCoreMvcTestBase
.HandleAsync(Arg.Any<ExceptionNotificationContext>());
#pragma warning restore 4014
}
[Fact]
public async Task Should_Handle_Exception_On_Uow_SaveChangeAsync()
{
FakeRequiredService.Claims.AddRange(new[]
{
new Claim(AbpClaimTypes.UserId, Guid.Empty.ToString())
});
var result = await GetResponseAsObjectAsync<RemoteServiceErrorResponse>("/api/exception-test/ExceptionOnUowSaveChange", HttpStatusCode.Conflict);
result.Error.ShouldNotBeNull();
result.Error.Message.ShouldBe("The data you have submitted has already changed by another user/client. Please discard the changes you've done and try from the beginning.");
#pragma warning disable 4014
_fakeExceptionSubscriber
.Received()
.HandleAsync(Arg.Any<ExceptionNotificationContext>());
#pragma warning restore 4014
}
}

@ -36,4 +36,9 @@ public class ExceptionTestPage : AbpPageModel
{
throw new AbpAuthorizationException("This is a sample exception!");
}
public Task<JsonResult> OnGetExceptionOnUowSaveChangeAsync()
{
return Task.FromResult(new JsonResult("OK"));
}
}

@ -3,12 +3,14 @@ using System.Net;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using NSubstitute;
using Shouldly;
using Volo.Abp.ExceptionHandling;
using Volo.Abp.Http;
using Volo.Abp.Security.Claims;
using Volo.Abp.Uow;
using Xunit;
namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling;
@ -31,6 +33,8 @@ public class ExceptionTestPage_Tests : AspNetCoreMvcTestBase
_fakeExceptionSubscriber = Substitute.For<IExceptionSubscriber>();
services.AddSingleton(_fakeExceptionSubscriber);
services.Replace(ServiceDescriptor.Transient<IUnitOfWork, ExceptionHandingUnitOfWork>());
}
[Fact]
@ -140,4 +144,25 @@ public class ExceptionTestPage_Tests : AspNetCoreMvcTestBase
.HandleAsync(Arg.Any<ExceptionNotificationContext>());
#pragma warning restore 4014
}
[Fact]
public async Task Should_Handle_Exception_On_Uow_SaveChangeAsync()
{
_fakeRequiredService.Claims.AddRange(new[]
{
new Claim(AbpClaimTypes.UserId, Guid.Empty.ToString())
});
var result = await GetResponseAsObjectAsync<RemoteServiceErrorResponse>("/api/exception-test/ExceptionOnUowSaveChange", HttpStatusCode.Conflict);
result.Error.ShouldNotBeNull();
result.Error.Message.ShouldBe("The data you have submitted has already changed by another user/client. Please discard the changes you've done and try from the beginning.");
#pragma warning disable 4014
_fakeExceptionSubscriber
.Received()
.HandleAsync(Arg.Any<ExceptionNotificationContext>());
#pragma warning restore 4014
}
}

@ -2,15 +2,23 @@
using System.Net;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Shouldly;
using Volo.Abp.Http;
using Volo.Abp.Json;
using Volo.Abp.Uow;
using Xunit;
namespace Volo.Abp.AspNetCore.Mvc.Uow;
public class UnitOfWorkMiddleware_Exception_Rollback_Tests : AspNetCoreMvcTestBase
{
protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services)
{
services.Replace(ServiceDescriptor.Transient<IUnitOfWork, TestUnitOfWork>());
}
[Fact]
public async Task Should_Rollback_Transaction_For_Handled_Exceptions()
{

@ -2,15 +2,23 @@
using System.Net;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Shouldly;
using Volo.Abp.Http;
using Volo.Abp.Json;
using Volo.Abp.Uow;
using Xunit;
namespace Volo.Abp.AspNetCore.Mvc.Uow;
public class UnitOfWorkPageFilter_Exception_Rollback_Tests : AspNetCoreMvcTestBase
{
protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services)
{
services.Replace(ServiceDescriptor.Transient<IUnitOfWork, TestUnitOfWork>());
}
[Fact]
public async Task Should_Rollback_Transaction_For_Handled_Exceptions()
{

Loading…
Cancel
Save