How to use Transactions in ASP.NET MVC identity 2? - asp.net

In My ASP.NET MVC5 Identity 2 Application trying to use transactions but it is not working.please see the below code the transactions not working.If var saveteacher = _teacherService.Create(aTeacher); not insert successfully then AspNetUsers not rollback from database.
Code:
using (var dataContext = new SchoolMSDbContext())
{
using (var trans = dataContext.Database.BeginTransaction(IsolationLevel.ReadCommitted))
{
try
{
var adminresult =await UserManager.CreateAsync(user, teacherViewModel.Password);
if (adminresult.Succeeded)
{
aTeacher.Id = user.Id;
var saveteacher = _teacherService.Create(aTeacher);
}
else
{
trans.Rollback();
ModelState.AddModelError("", adminresult.Errors.First());
return View();
}
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
Console.WriteLine(ex.InnerException);
}
}
}

I think the problem could be with async stuff.
Try creating the transaction like this:
TransactionScope transaction = new TransactionScope(System.Transactions.TransactionScopeAsyncFlowOption.Enabled);
(you'll have to add System.Transactions) to references.
To commit transaction go transaction.Complete() to rollback do transaction.Dispose().

The problem is that you are creating a new instance of SchoolMSDbContext when you should be acquiring the existing one from the HttpContext.
Example:
using (var dataContext = HttpContext.GetOwinContext().Get<ApplicationDbContext>())
{
//...
}

Make sure _teacherService and UserManager uses same DB Context. No need to create a TransactionScope.

Related

How to use Dapper asynchronously with multiple stored procedure calls?

I'm using the Dapper ORM in an ASP.Net Core 2.1 CRUD application, with a SQL Server database. For a new form I'm developing, I want to retrieve all reference data (to populate SELECT field options) in one go when the screen is invoked. This involves a series of stored procedure calls, which I want to do asynchronously for best performance.
This Dapper tutorial suggests I should look at using QueryMultipleAsync, but I can't find an example of its use with stored procedures, only hard-coded SQL statements.
My C# code currently looks like this:
public async Task<ContactReferenceData> Get()
{
ContactReferenceData refData = new ContactReferenceData();
try
{
using (IDbConnection dbConnection = _connection)
{
dbConnection.Open();
var countryData = await dbConnection.QueryAsync<Country>(sql: "usp_GetCountries", commandType: CommandType.StoredProcedure);
refData.CountryDetails = countryData.AsList();
var companyData = await dbConnection.QueryAsync<Company>(sql: "usp_GetCompanies", commandType: CommandType.StoredProcedure);
refData.CompanyDetails = companyData.AsList();
var groupData = await dbConnection.QueryAsync<Group>(sql: "usp_GetGroups", commandType: CommandType.StoredProcedure);
refData.GroupDetails = groupData.AsList();
var groupPositionData = await dbConnection.QueryAsync<GroupPosition>(sql: "usp_GetGroupPositions", commandType: CommandType.StoredProcedure);
refData.GroupPositionDetails = groupPositionData.AsList();
}
return refData;
}
catch (Exception ex)
{
_logger.LogError(ex.ToString());
throw;
}
}
This works OK in my test environment, but I'm not sure it's the correct way to execute async queries. In particular, I have the following concerns:
is it robust enough to be trusted in live operation?
in its current form, is it maximising the benefits (if any) of asynchronous operation, or should I be using QueryMultipleAsync to properly achieve this?
Have you tried something like this?
public async Task<ContactReferenceData> Get()
{
var sql = "EXEC usp_GetCountries; EXEC usp_GetCompanies; EXEC usp_GetGroups; EXEC usp_GetGroupPositions";
ContactReferenceData refData = new ContactReferenceData();
try
{
using (IDbConnection dbConnection = _connection)
{
using (var multi = connection.QueryMultipleAsync(sql: sql, commandType: CommandType.StoredProcedure ).Result)
{
refData.CountryDetails = multi.Read<countryDetails>().ToList();
refData.CompanyDetails = multi.Read<CompanyDetails>().ToList();
refData.GroupData = multi.Read<Groups>().ToList();
refData.GroupPositionsData= multi.Read<GroupPositions>().ToList();
}
}
return refData;
}
catch (Exception ex)
{
_logger.LogError(ex.ToString());
throw;
}
}

Dapper, .NET Core and Transactions fail

We port our Dapper based application to .NET Core and we have a problem with our transaction code.
We use "actions" to execute stuff
public Action<IDbConnection> CreateAction(string statement, object values)
{
return (dbConnection) => dbConnection.Execute(statement, values);
}
And we use methods to execute those actions with
public void Execute(IEnumerable<Action<IDbConnection>> actions)
{
using (IDbConnection connection = OpenConnection())
using (IDbTransaction transaction = connection.BeginTransaction())
{
try
{
foreach (var action in actions)
{
action(transaction.Connection);
}
transaction.Commit();
}
catch
{
transaction.Rollback();
throw;
}
}
}
This works great with .NET Framework and Dapper 1.42, but it fails on .NET Core with Dapper 1.50.2.
System.InvalidOperationException: 'ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.'
If we remove the transaction using it also works fine.
What needs to change to make it work?
Ok, it seems we now have to pass the transaction explicit.
public void Execute(IEnumerable<Action<IDbConnection, IDbTransaction>> actions)
{
using (IDbConnection connection = OpenConnection())
using (IDbTransaction transaction = connection.BeginTransaction())
{
try
{
foreach (var action in actions)
{
action(transaction.Connection, transaction);
}
transaction.Commit();
}
catch
{
transaction.Rollback();
throw;
}
}
}

Transactions with ASP.NET Core 1.0 Identity UserManager

In one of my apps, I'm rewriting Asp Core Identity UserManager's CreateAsync, to in addition to creating new user in UserStore - create a new row in a separate statistics table (on a different dbcontext). Problem is, I would like both of those operations to be fired inside a transaction, so that if one fails - the other does not commit. Here's the code :
public override async Task<IdentityResult> CreateAsync(TUser user, string password)
{
// We need to use transactions here.
var result = await base.CreateAsync(user, password);
if (result.Succeeded)
{
var appUser = user as IdentityUser;
if (appUser != null)
{
// create user stats.
var userStats = new UserStats()
{
ActionsCount = 0,
EventsCount = 0,
FollowersCount = 0,
ProjectsCount = 0,
UserId = appUser.Id
};
_ctx.UserStats.Add(userStats);
await _ctx.SaveChangesAsync();
}
}
return result;
}
Thing is, I have no idea how to set up such a transaction, as it seems TransactionScope is not a part of .Net Core yet (?) and getting currently scoped DbContext for base.CreateAsync() with HttpContext.GetOwinContext() does not work as HttpContext seems to be missing this method (referencing Microsoft.Owin.Host.SystemWeb or Microsoft.AspNet.WebApi.Owin as hinted in some other stack overflow answers won't do - both are not compatible with .netcore). Any help ?
Firstly you need to setup the dbcontext you are using with identity with a scoped lifetime:
services.AddDbContext<MyDbContext>(ServiceLifetime.Scoped); // this is the important bit
services.AddIdentity<User, Role>(options =>
{
})
.AddEntityFrameworkStores<MyDbContext, int>()
.AddDefaultTokenProviders();
Then when you need to create a transaction you do the following:
using (var transaction = await _myDbContext.Database.BeginTransactionAsync())
{
var result = await _userManager.CreateAsync(newUser, password);
try
{
if (result.Succeeded)
{
DBItem newItem = new DBItem();
_myDbContext.Add(newItem);
await _myDbContext.SaveChangesAsync();
transaction.Commit();
}
}
catch (Exception e)
{
transaction.Rollback();
}
}

When calling RavenDb store.DatabaseCommands.GlobalAdmin.CreateDatabase() the call hangs

The project that I am currently working on is using RavenDb as an embedded datastore, while attempting to call out to make sure that the database exists in the store, I am finding that it hangs.
var docStore = new EmbeddableDocumentStore()
{
DataDirectory = "Data",
};
docStore.Initialize();
// Check to make sure that the database exists
bool bcDatabaseExists = docStore.DatabaseCommands.GlobalAdmin.GetDatabaseNames(1024).Contains(DatabaseName);
if (!bcDatabaseExists)
{
Dictionary<string, string> settings = new Dictionary<string, string>();
DatabaseDocument databaseDocument = new DatabaseDocument()
{
Id = DatabaseName,
Settings =
{
{ "Raven/DataDir", "~\\Data" }
}
};
try
{
docStore.DatabaseCommands.GlobalAdmin.CreateDatabase(databaseDocument);
}
catch (Exception ex)
{
log.Error(ex);
}
}
However when I hit the CreateDatabase call the process just hangs without any notification. I wanted to check to make sure I wasn't using the call incorrectly, or if there was a better call.
Any thoughts or suggestions that you can offer would be greatly appreciated.
Although the question has nothing to do with NancyFX, probably you need EnsureDatabaseExists method. It will create the database, if it's not there yet.

asp.net-identity transaction issue

I want to create a user with a role in the same transaction but i have an issue with the implementation. In order to use the userStore in the transaction and have it not save the changes automatically and ignore my transaction i had to turn off AutoSaveChanges. This makes it so it will wait until i call save changes. This works fine but because the userstore now does not return the userId when i call manager.Create due to this being off I dont have an Id to pass into userManager.AddToRole. Is there any way to add the user i am trying to create to a role within the same transaction?
If you start your transaction manually, then commit it, everything that was written to DB inside your transaction will be held inside your transaction. And you can rollback that if you want to.
Do something like that:
var dbContext = // get instance of your ApplicationDbContext
var userManager = // get instance of your ApplicationUserManager
using (var transaction = dbContext.Database.BeginTransaction(IsolationLevel.ReadCommitted))
{
try
{
var user = // crate your ApplicationUser
var userCreateResult = await userManger.CreateAsync(user, password);
if(!userCreateResult.Succeeded)
{
// list of errors in userCreateResult.Errors
transaction.Rollback();
return userCreateResult.Errors;
}
// new Guid for user now saved to user.Id property
var userId = user.Id;
var addToRoleresult = await userManager.AddToRoleAsync(user.Id, "My Role Name");
if(!addToRoleresult.Succeeded)
{
// deal with errors
transaction.Rollback();
return addToRoleresult.Errors;
}
// if we got here, everything worked fine, commit transaction
transaction.Commit();
}
catch (Exception exception)
{
transaction.Rollback();
// log your exception
throw;
}
}
Hope this helps.

Resources