0
0
Kaynağa Gözat

修复SqlServer版前端调用异常及其事物异常

xiaoxue 5 yıl önce
ebeveyn
işleme
0f7c2a3d30

+ 5 - 8
Admin.Core/Aop/TransactionInterceptor.cs

@@ -32,6 +32,7 @@ namespace Admin.Core.Aop
                         if (invocation.Method.ReturnType == typeof(Task))
                         {
                             await (Task)invocation.ReturnValue;
+                            _unitOfWork.Commit();
                         }
                         else
                         {
@@ -49,6 +50,7 @@ namespace Admin.Core.Aop
                                 {
                                     _unitOfWork.Rollback();
                                 }
+                                _unitOfWork.Commit();
                             },
                             ex =>
                             {
@@ -65,19 +67,14 @@ namespace Admin.Core.Aop
                             {
                                 _unitOfWork.Rollback();
                             }
+                            _unitOfWork.Commit();
                         }
                     }
-
-                    _unitOfWork.Commit();
                 }
-                catch (Exception)
+                catch (Exception ex)
                 {
                     _unitOfWork.Rollback();
-                    throw;
-                }
-                finally
-                {
-                    _unitOfWork.Close();
+                    throw ex;
                 }
             }
             else

+ 5 - 5
Admin.Core/Db/ServiceCollectionExtensions.cs

@@ -47,8 +47,12 @@ namespace Admin.Core.Db
 
             var fsql = freeSqlBuilder.Build();
 
+            services.AddFreeRepository(filter => filter.Apply<IEntitySoftDelete>("SoftDelete", a => a.IsDeleted == false));
+            services.AddScoped<IUnitOfWork>(sp => fsql.CreateUnitOfWork());
+            services.AddSingleton(fsql);
+
             #region 初始化数据库
-            //同步结构,需要内部配置自增长
+            //同步结构
             if (dbConfig.SyncStructure)
             {
                 DbHelper.SyncStructure(fsql, dbConfig: dbConfig);
@@ -121,10 +125,6 @@ namespace Admin.Core.Db
                 }
             };
             #endregion
-            
-            services.AddSingleton(fsql);
-            services.AddFreeRepository(filter => filter.Apply<IEntitySoftDelete>("SoftDelete", a => a.IsDeleted == false));
-            services.AddScoped<IUnitOfWork>(sp => fsql.CreateUnitOfWork());
             #endregion
 
             Console.WriteLine($"{appConfig.Urls}\r\n");