When I use application service inside a try catch in the controller even if the app service method throw an exception the unit of work commit changes to the DB because I am using try catch block for example
[HttpPost]
[UnitOfWork]
//[ValidateAntiForgeryToken()]
public virtual async Task<ActionResult> Register(SignUpUserInput model)
{
var ttt = _unitOfWorkManager.Current;
//await _userAppService.SignUpUser(model);
//throw new UserFriendlyException("Test UI Exception");
try
{
CheckModelState();
//var validateCaptchaResult = ValidateCaptcha();
//if (!validateCaptchaResult.Succeeded)
//{
// throw new Exception(validateCaptchaResult.Errors.FirstOrDefault() ?? string.Empty);
//}
await _userAppService.SignUpUser(model);
return RedirectToAction("RegistrationCompleted");
}
catch (Exception ex)
{
ViewBag.ErrorMessage = ex.Message;
if (ex.InnerException != null)
{
ViewBag.ErrorMessage += "<br>" + ex.InnerException.Message;
}
return View(model);
}
}
The above code even if _userAppService.SignUpUser(model) throw an exception the unit of work will commit the changes. If I removed the try catch the unit if work rollback the changes. Is there is any way to rollabck the changes manually ?
This Topic related to #170 Hello Halil I was reading about unit of work and How aspnetboilerplate automatically start a transaction on calling any Application Service method here <a class="postlink" href="http://aspnetboilerplate.com/Pages/Docu">http://aspnetboilerplate.com/Pages/Docu</a> ... it-Of-Work Do you mean that Starting the MSDTC on the server will fix the issues regarding starting Two transaction ? Dose aspnetboilerplate unit of work deal with this scenarios where multi tenant application should use different database for each tenant ? Please help because I am new to aspnetboilerplate and I also should use Single Application/Multi Database approach.
My scenario is Single Deployment - Multiple Database in the below link I just want to make sure that aspnetboilerplate work with Single Deployment - Multiple Database or How I should implement my Application Service classes in order to get aspnetboilerplate works with Single Deployment - Multiple Database <a class="postlink" href="http://aspnetboilerplate.com/Pages/Docu">http://aspnetboilerplate.com/Pages/Docu</a> ... ti-Tenancy
My scenario workflow is like this
<cite>hikalkan: </cite> Hi,
Since this topic is asked by also other people, I decided to prepare a simple running application. Download it from here: <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate-samples/tree/master/Others">https://github.com/aspnetboilerplate/as ... ter/Others</a>
I created 2 different DbContext and 2 different migrations in same project. See migration command reference: <a class="postlink" href="http://coding.abel.nu/2012/03/ef-migrations-command-reference/">http://coding.abel.nu/2012/03/ef-migrat ... reference/</a>
I added a new connection string named 'Second':
<add name="Default" connectionString="Server=localhost; Database=MultipleDbContextDemo; Trusted_Connection=True;" providerName="System.Data.SqlClient" /> <add name="Second" connectionString="Server=localhost; Database=MultipleDbContextDemoSecondDb; Trusted_Connection=True;" providerName="System.Data.SqlClient" />
Then Added second db context:
public class MySecondDbContext : AbpDbContext { public virtual IDbSet<Course> Courses { get; set; } public MySecondDbContext() : base("Second") { } }
Then enabled migrations:
Enable-Migrations -MigrationsDirectory "MigrationsSecond" -ContextTypeName "MySecondDbContext"
When using add-migration and update database, we use configuration type name, like:
Update-Database -ConfigurationTypeName "MultipleDbContextDemo.MigrationsSecond.Configuration"
An application services both dbcontext (over 2 repositories):
public class TestAppService : MultipleDbContextDemoAppServiceBase, ITestAppService { private readonly IRepository<Person> _persons; private readonly IRepository<Course> _courseRepository; public TestAppService(IRepository<Person> persons, IRepository<Course> courseRepository) { _persons = persons; _courseRepository = courseRepository; } public List<string> GetFromFirstDb() { var peopleNames = _persons.GetAllList().Select(p => p.PersonName).ToList(); return peopleNames; } public List<string> GetFromSecondDb() { var courseNames = _courseRepository.GetAllList().Select(p => p.CourseName).ToList(); return courseNames; } public List<string> GetFromBothDbs() { List<string> names = new List<string>(); var peopleNames = _persons.GetAllList().Select(p => p.PersonName).ToList(); names.AddRange(peopleNames); var courseNames = _courseRepository.GetAllList().Select(p => p.CourseName).ToList(); names.AddRange(courseNames); return names; } }
Note that: GetFromBothDbs() may not work. You should start Distributed Transaction Coordinator windows service..
For details, see source codes. After downloading solution, first create 2 databases using following commands:
Update-Database -ConfigurationTypeName "MultipleDbContextDemo.Migrations.Configuration"
Update-Database -ConfigurationTypeName "MultipleDbContextDemo.MigrationsSecond.Configuration"
I hope this help you.
Hello Halil I was reading about unit of work and How aspnetboilerplate automatically start a transaction on calling any Application Service method here <a class="postlink" href="http://aspnetboilerplate.com/Pages/Documents/Unit-Of-Work">http://aspnetboilerplate.com/Pages/Docu ... it-Of-Work</a> Do you mean that Starting the MSDTC on the server will fix the issues regarding starting Two transaction ? Dose aspnetboilerplate unit of work deal with this scenarios where multi tenant application should use different database for each tenant ? Please help because I am new to aspnetboilerplate and I also should use Single Application/Multi Database approach.
My scenario is Single Deployment - Multiple Database in the below link I just want to make sure that aspnetboilerplate work with Single Deployment - Multiple Database or How I should implement my Application Service classes in order to get aspnetboilerplate works with Single Deployment - Multiple Database <a class="postlink" href="http://aspnetboilerplate.com/Pages/Documents/Multi-Tenancy">http://aspnetboilerplate.com/Pages/Docu ... ti-Tenancy</a>
Thank you