public ServiceManager(BaseDbContext context, IHttpContextAccessor httpContextAccessor, IMapper mapper, ILookupNormalizer lookupNormalizer) { serviceContext = new ServiceContext(); serviceContext.AddItem("baseDbContext", context); serviceContext.AddItem("IHttpContextAccessor", httpContextAccessor); serviceContext.AddItem("IMapper", mapper); serviceContext.AddItem("ILookupNormalizer", lookupNormalizer); string userid = ""; if (httpContextAccessor != null && httpContextAccessor.HttpContext != null && httpContextAccessor.HttpContext.User != null) { userid = httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.Name); if (string.IsNullOrEmpty(userid)) { userid = httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); } } serviceContext.AddItem("CurrentUserId", userid); string token = ""; if (httpContextAccessor != null && httpContextAccessor.HttpContext != null && httpContextAccessor.HttpContext.Request != null) { token = httpContextAccessor.HttpContext.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last(); if (string.IsNullOrEmpty(token)) { token = ""; } } serviceContext.AddItem("Token", token); repositoryManager = new RepositoryManager(context); }
public async Task Invoke(InvokeContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } object[] customAttributes = (object[])context.Request.Properties["CustomAttributes"]; DatabaseConnection databaseConnection = customAttributes.FirstOrDefault(t => t.GetType() == typeof(DatabaseConnection)) as DatabaseConnection; bool isDatabaseConnectionEnabled = databaseConnection == null ? true : databaseConnection.IsEnabled; if (!isDatabaseConnectionEnabled) { await _next.Invoke(context); } else { var appContext = Application.Current.Context; Transactional transactional = customAttributes.FirstOrDefault(t => t.GetType() == typeof(Transactional)) as Transactional; bool isTransactionEnabled = transactional == null ? false : transactional.IsEnabled; DbConnection connection = GetDbConnection(DbContextOptions.DbProviderName); DatabaseContext ctx = null; if (isTransactionEnabled) { connection.ConnectionString = DbContextOptions.WriteConnectionString; using (ctx = new DatabaseContext(true, connection)) { ctx.DbProviderName = DbContextOptions.DbProviderName; ServiceContext serviceContext = (ServiceContext)context.Request.Properties["ServiceContext"]; serviceContext.AddItem("DatabaseContext", ctx); await _next.Invoke(context); InvokeResult invokeResult = context.Result; if (invokeResult?.Value != null) { dynamic result = invokeResult.Value; if (result.GetType().Name == typeof(ServiceResponse <>).Name) { if (result?.IsSuccess == true) { ctx.Commit(); } else { ctx.Transaction.Rollback(); } } else { ctx.Commit(); } } else { ctx.Transaction.Rollback(); } } } else { connection.ConnectionString = DbContextOptions.ReadConnectionString; using (ctx = new DatabaseContext(false, connection)) { ctx.DbProviderName = DbContextOptions.DbProviderName; ServiceContext serviceContext = (ServiceContext)context.Request.Properties["ServiceContext"]; serviceContext.AddItem("DatabaseContext", ctx); await _next.Invoke(context); } } } }