Browse Source

stable
change all to dapper
add dapper transaction

Robert 2 years ago
parent
commit
0716d39b7c

+ 2 - 0
Dockerfile

@@ -1,6 +1,8 @@
 #See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
 
 FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
+RUN sed -i 's/TLSv1.2/TLSv1/g' /etc/ssl/openssl.cnf
+RUN sed -i 's/DEFAULT@SECLEVEL=2/DEFAULT@SECLEVEL=1/g' /etc/ssl/openssl.cnf
 WORKDIR /app
 
 FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build

+ 1 - 0
EVCB_OCPP.TaskScheduler/EVCB_OCPP.TaskScheduler.csproj

@@ -12,6 +12,7 @@
   </ItemGroup>
   <ItemGroup>
     <PackageReference Include="Dapper" Version="2.0.123" />
+    <PackageReference Include="Dapper.Transaction" Version="2.0.123" />
     <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
     <PackageReference Include="Microsoft.Data.SqlClient" Version="5.0.1" />
     <PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />

+ 39 - 23
EVCB_OCPP.TaskScheduler/Jobs/CheckEVSEOnlineJob.cs

@@ -7,6 +7,7 @@ using Quartz;
 using System;
 using System.Collections.Generic;
 using System.Configuration;
+using System.Data;
 using System.Diagnostics;
 using System.Linq;
 using System.Text;
@@ -46,7 +47,7 @@ namespace EVCB_OCPP.TaskScheduler.Jobs
             logger.LogDebug(this.ToString() + " :Starting........");
             try
             {
-                List<EVSECurrentStatus> _EVSEs = GetEVSEs();
+                List<EVSECurrentStatus> _EVSEs = await GetEVSEs();
                 var checktime = DateTime.UtcNow.AddDays(-3);
                 _EVSEs = _EVSEs.Where(x => x.HeartbeatUpdatedOn > checktime).ToList();
 
@@ -146,7 +147,7 @@ namespace EVCB_OCPP.TaskScheduler.Jobs
                         else
                         {
                             //off - on 
-                            UpdateEVSECurrentStatus(evse.CustomerId.ToString(), evse.ChargeBoxId, true, DefaultSetting.DefaultNullTime);
+                            await UpdateEVSECurrentStatus(evse.CustomerId.ToString(), evse.ChargeBoxId, true, DefaultSetting.DefaultNullTime);
 
                             insertData.Add(new EVSEOnlineRecord()
                             {
@@ -168,7 +169,7 @@ namespace EVCB_OCPP.TaskScheduler.Jobs
                         {
 
                             //on -off  
-                            UpdateEVSECurrentStatus(evse.CustomerId.ToString(), evse.ChargeBoxId, false, evse.HeartbeatUpdatedOn);
+                            await UpdateEVSECurrentStatus(evse.CustomerId.ToString(), evse.ChargeBoxId, false, evse.HeartbeatUpdatedOn);
 
                             var _pickDate = evse.HeartbeatUpdatedOn.Date;
                             var _picks = await GetOnlineRecords(_pickDate, evse.CustomerId.ToString(), evse.ChargeBoxId, _pickDate.Hour, evse.HeartbeatUpdatedOn.Hour);
@@ -242,18 +243,18 @@ namespace EVCB_OCPP.TaskScheduler.Jobs
         }
 
 
-        private List<EVSECurrentStatus> GetEVSEs()
+        private async Task<List<EVSECurrentStatus>> GetEVSEs()
         {
             List<EVSECurrentStatus> result = new List<EVSECurrentStatus>();
             try
             {
                 using (var dbConn = new SqlConnection(mainDBConnectString))
                 {
-                    dbConn.Open();
+                    //dbConn.Open();
                     string sqlstring = "SELECT m.CustomerId,m.Id,m.ChargeBoxId,m.Online,m.HeartbeatUpdatedOn,MachineConfigurations.ConfigureSetting HeartbeatInterval"
                    + "  FROM [dbo].[Machine]  m  left join [dbo].[MachineConfigurations]  MachineConfigurations  on m.ChargeBoxId = MachineConfigurations.ChargeBoxId"
                    + " where MachineConfigurations.ConfigureName = 'HeartbeatInterval'  and MachineConfigurations.ConfigureSetting!=''";
-                    result = dbConn.Query<EVSECurrentStatus>(sqlstring).ToList();
+                    result = (await dbConn.QueryAsync<EVSECurrentStatus>(sqlstring)).ToList();
                 }
 
             }
@@ -266,26 +267,26 @@ namespace EVCB_OCPP.TaskScheduler.Jobs
             return result;
         }
 
-        private void UpdateEVSECurrentStatus(string customerId, string ChargeBoxId, bool turnOn, DateTime offlineTime)
+        private async Task UpdateEVSECurrentStatus(string customerId, string ChargeBoxId, bool turnOn, DateTime offlineTime)
         {
             try
             {
                 string sqlString = string.Format("UPDATE [dbo].[Machine] SET Online=@Online {0} WHERE chargeBoxId=@chargeBoxId and customerId=@customerId", turnOn ? "" : " ,OfflineOn=@OfflineOn");
                 using (var dbConn = new SqlConnection(mainDBConnectString))
                 {
-                    dbConn.Open();
+                    //dbConn.Open();
                     var parameters = new DynamicParameters();
-                    parameters.Add("@Online", turnOn, System.Data.DbType.Boolean);
-                    parameters.Add("@chargeBoxId", ChargeBoxId, System.Data.DbType.String);
-                    parameters.Add("@customerId", customerId, System.Data.DbType.String);
+                    parameters.Add("@Online", turnOn, DbType.Boolean);
+                    parameters.Add("@chargeBoxId", ChargeBoxId, DbType.String, ParameterDirection.Input, 50);
+                    parameters.Add("@customerId", customerId, DbType.String , ParameterDirection.Input, 36);
                     if (!turnOn)
                     {
-                        parameters.Add("@OfflineOn", offlineTime, System.Data.DbType.DateTime);
+                        parameters.Add("@OfflineOn", offlineTime, DbType.DateTime);
                     }
 
 
 
-                    dbConn.Execute(sqlString, parameters);
+                    await dbConn.ExecuteAsync(sqlString, parameters);
                 }
 
             }
@@ -303,12 +304,27 @@ namespace EVCB_OCPP.TaskScheduler.Jobs
             {
                 for (int i = 0; i < updateItems.Count; i++)
                 {
+                    var updateItem = updateItems[i];
                     string sqlString = string.Format("UPDATE [dbo].[EVSEOnlineRecord_{0}] SET OfflineTime=@OfflineTime , TotalMinute=@TotalMinute  WHERE customerId=@customerId and chargeBoxId=@chargeBoxId and " +
-                       "OnlineTime=@OnlineTime", updateItems[i].OnlineTime.Date.ToString("yyMMdd"));
+                       "OnlineTime=@OnlineTime", updateItem.OnlineTime.Date.ToString("yyMMdd"));
                     using (var dbConn = new SqlConnection(onlineDBConnectString))
                     {
-                        dbConn.Open();
-                        await dbConn.ExecuteAsync(sqlString, updateItems[i]);
+                        //dbConn.Open();
+
+                        //var cmd = new SqlCommand(sqlString,dbConn);
+                        //cmd.Parameters.AddWithValue("@OfflineTime", updateItem.OfflineTime);
+                        //cmd.Parameters.AddWithValue("@TotalMinute", updateItem.TotalMinute);
+                        //cmd.Parameters.AddWithValue("@customerId", updateItem.CustomerId);
+                        //cmd.Parameters.AddWithValue("@chargeBoxId", updateItem.ChargeBoxId);
+                        //cmd.Parameters.AddWithValue("@OnlineTime", updateItem.OnlineTime);
+                        //await cmd.ExecuteNonQueryAsync();
+                        var param = new DynamicParameters();
+                        param.Add("OfflineTime", updateItem.OfflineTime, DbType.Date);
+                        param.Add("TotalMinute", updateItem.TotalMinute, DbType.Int32);
+                        param.Add("customerId", updateItem.CustomerId, DbType.Guid);
+                        param.Add("chargeBoxId", updateItem.ChargeBoxId, DbType.String, ParameterDirection.Input, 36);
+                        param.Add("OnlineTime", updateItem.OnlineTime, DbType.Date);
+                        await dbConn.ExecuteAsync(sqlString, param);
                     }
 
                 }
@@ -319,7 +335,7 @@ namespace EVCB_OCPP.TaskScheduler.Jobs
                     "VALUES(@CustomerId,@StationId, @ChargeBoxId,@HourIndex,@TotalMinute,@OnlineTime,@OfflineTime); ", insertItems[i].OnlineTime.Date.ToString("yyMMdd"));
                     using (var dbConn = new SqlConnection(onlineDBConnectString))
                     {
-                        dbConn.Open();
+                        //dbConn.Open();
                         await dbConn.ExecuteAsync(sqlString, insertItems[i]);
                     }
 
@@ -342,13 +358,13 @@ namespace EVCB_OCPP.TaskScheduler.Jobs
                     "HourIndex >= @startHourCondition and HourIndex <= @stopHourCondition and OfflineTime=@OfflineTime", pickDate.ToString("yyMMdd"));
                 using (var dbConn = new SqlConnection(onlineDBConnectString))
                 {
-                    dbConn.Open();
+                    //dbConn.Open();
                     var parameters = new DynamicParameters();
-                    parameters.Add("@customerId", new Guid(customerId), System.Data.DbType.Guid);
-                    parameters.Add("@chargeBoxId", chargeBoxId, System.Data.DbType.String);
-                    parameters.Add("@startHourCondition", startHourCondition, System.Data.DbType.Int32);
-                    parameters.Add("@stopHourCondition", stopHourCondition, System.Data.DbType.Int32);
-                    parameters.Add("@OfflineTime", DefaultSetting.DefaultNullTime, System.Data.DbType.Date);
+                    parameters.Add("customerId", new Guid(customerId), DbType.Guid);
+                    parameters.Add("chargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 36);
+                    parameters.Add("startHourCondition", startHourCondition, DbType.Int32);
+                    parameters.Add("stopHourCondition", stopHourCondition, DbType.Int32);
+                    parameters.Add("OfflineTime", DefaultSetting.DefaultNullTime, DbType.Date);
 
                     var result = await dbConn.QueryAsync<EVSEOnlineRecord>(sqlString, parameters);
                     records = result.ToList();

+ 2 - 2
EVCB_OCPP.TaskScheduler/Jobs/CheckExecutionCmdJob.cs

@@ -36,7 +36,7 @@ namespace EVCB_OCPP.TaskScheduler.Jobs
             foreach (var customerId in cList)
             {
                 ICustomerService s = serviceProvider.GetService<ICustomerService>();//.Create(customerId);
-                s.SetCustomerId(customerId);
+                await s.SetCustomerId(customerId);
 
 
                 tList.Add(Task.Run(() => DoMainTask(customerId)));
@@ -50,7 +50,7 @@ namespace EVCB_OCPP.TaskScheduler.Jobs
         private void DoMainTask(Guid customerId)
         {
             ICustomerService _service = serviceProvider.GetService<ICustomerService>();//.Create(customerId);
-            _service.SetCustomerId(customerId);
+            _service.SetCustomerId(customerId).Wait();
             _service.MonitorRemoteCommand().Wait();
         }
     }

+ 2 - 2
EVCB_OCPP.TaskScheduler/Jobs/ExecutionCmdReportJob.cs

@@ -36,7 +36,7 @@ namespace EVCB_OCPP.TaskScheduler.Jobs
             foreach (var customerId in cList)
             {
                 ICustomerService s = serviceProvider.GetService<ICustomerService>();
-                s.SetCustomerId(customerId);
+                await s.SetCustomerId(customerId);
 
 
                 tList.Add(Task.Run(() => DoMainTask(customerId)));
@@ -50,7 +50,7 @@ namespace EVCB_OCPP.TaskScheduler.Jobs
         private void DoMainTask(Guid customerId)
         {
             ICustomerService _service = serviceProvider.GetService<ICustomerService>();
-            _service.SetCustomerId(customerId);
+            _service.SetCustomerId(customerId).Wait();
             _service.ReportExecutionofRemoteCommand().Wait();
         }
     }

+ 2 - 2
EVCB_OCPP.TaskScheduler/Jobs/StartTransacionReportJob.cs

@@ -37,7 +37,7 @@ namespace EVCB_OCPP.TaskScheduler.Jobs
             foreach (var customerId in cList)
             {
                 ICustomerService s = serviceProvider.GetService<ICustomerService>();//.Create(customerId);
-                s.SetCustomerId(customerId);
+                await s.SetCustomerId(customerId);
 
                tList.Add(Task.Run(() => DoMainTask(customerId)));
             }
@@ -53,7 +53,7 @@ namespace EVCB_OCPP.TaskScheduler.Jobs
 
 
             ICustomerService _service = serviceProvider.GetService<ICustomerService>();
-            _service.SetCustomerId(customerId);
+            _service.SetCustomerId(customerId).Wait();
             _service.ReportStartTransaction().Wait();
         }
     }

+ 2 - 2
EVCB_OCPP.TaskScheduler/Jobs/StopTransacionReportJob.cs

@@ -36,7 +36,7 @@ namespace EVCB_OCPP.TaskScheduler.Jobs
             foreach (var customerId in cList)
             {
                 ICustomerService s = serviceProvider.GetService<ICustomerService>();//.Create(customerId);
-                s.SetCustomerId(customerId);
+                await s.SetCustomerId(customerId);
 
 
                 tList.Add(Task.Run(() => DoMainTask(customerId)));
@@ -51,7 +51,7 @@ namespace EVCB_OCPP.TaskScheduler.Jobs
         private void DoMainTask(Guid customerId)
         {
             ICustomerService _service = serviceProvider.GetService<ICustomerService>();//.Create(customerId);
-            _service.SetCustomerId(customerId);
+            _service.SetCustomerId(customerId).Wait();
             _service.ReportStopTransaction().Wait();
         }
     }

+ 9 - 9
EVCB_OCPP.TaskScheduler/Services/CommonCustomerService.cs

@@ -32,12 +32,12 @@ namespace EVCB_OCPP.TaskScheduler.Services
             this.httpClient = serviceProvider.GetService<OuterHttpClient>();
         }
 
-        public void SetCustomerId(Guid customerId)
+        public async Task SetCustomerId(Guid customerId)
         {
             this.customerId = customerId;
 
-            customerName = _dbService.GetCustomerName(this.customerId);
-            _dbService.GetCustomerName(this.customerId);
+            customerName = await _dbService.GetCustomerName(this.customerId);
+            //_dbService.GetCustomerName(this.customerId);
             var connectionInfo = _dbService.GetAPIConnectionInfo(customerId);
             _saltkey = connectionInfo.ApiKey;
             _partnerAPIRoot = connectionInfo.ApiUrl;
@@ -55,7 +55,7 @@ namespace EVCB_OCPP.TaskScheduler.Services
 
         async public Task ReportStartTransaction()
         {
-            var items = _dbService.GetNeedReportSession(customerId, true, 1000);
+            var items = await _dbService.GetNeedReportSession(customerId, true, 1000);
 
             Stopwatch watch = new Stopwatch();
             watch.Start();
@@ -147,7 +147,7 @@ namespace EVCB_OCPP.TaskScheduler.Services
                             await Task.Delay(10);
                         }
 
-                        _dbService.ReportStartTx(sendBack);
+                        await _dbService.ReportStartTx(sendBack);
 
                     }
                 }
@@ -165,7 +165,7 @@ namespace EVCB_OCPP.TaskScheduler.Services
         async public Task ReportStopTransaction()
         {
 
-            var items = _dbService.GetNeedReportSession(customerId, false, 1000);
+            var items = await _dbService.GetNeedReportSession(customerId, false, 1000);
 
             Stopwatch watch = new Stopwatch();
             watch.Start();
@@ -282,7 +282,7 @@ namespace EVCB_OCPP.TaskScheduler.Services
                             await Task.Delay(10000);
                         }
 
-                        _dbService.ReportStopTx(sendBack);
+                        await _dbService.ReportStopTx(sendBack);
 
 
 
@@ -307,7 +307,7 @@ namespace EVCB_OCPP.TaskScheduler.Services
         {
             Stopwatch watch = new Stopwatch();
             watch.Start();
-            _dbService.TurntoTimeoutMachineOperateCommands(60);
+            await _dbService.TurntoTimeoutMachineOperateCommands(60);
             await Task.Delay(10);
             watch.Stop();
             logger.LogDebug("ReportExecutionofRemoteCommand Task : It takes  " + watch.ElapsedMilliseconds / 1000 + " Seconds");
@@ -408,7 +408,7 @@ namespace EVCB_OCPP.TaskScheduler.Services
                             await Task.Delay(10);
                         }
 
-                        _dbService.ReportExecution(sendBack);
+                        await _dbService.ReportExecution(sendBack);
 
 
 

+ 44 - 46
EVCB_OCPP.TaskScheduler/Services/DatabaseService.cs

@@ -1,4 +1,5 @@
 using Dapper;
+using Dapper.Transaction;
 using EVCB_OCPP.TaskScheduler.Models;
 using Microsoft.Data.SqlClient;
 using Microsoft.Extensions.Configuration;
@@ -7,6 +8,7 @@ using System;
 using System.Collections.Generic;
 using System.Configuration;
 using System.Data;
+using System.Data.Common;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
@@ -29,17 +31,17 @@ namespace EVCB_OCPP.TaskScheduler.Services
             onlineDBConnectString = configuration.GetConnectionString("OnlineLogDBContext");
         }
 
-        internal string GetCustomerName(Guid customerId)
+        internal async Task<string> GetCustomerName(Guid customerId)
         {
             string name = string.Empty;
             try
             {
                 using (var dbConn = new SqlConnection(mainDBConnectString))
                 {
-                    dbConn.Open();
+                    //dbConn.Open();
                     var parameters = new DynamicParameters();
-                    parameters.Add("@Id", customerId.ToString(), System.Data.DbType.String);
-                    name = dbConn.Query<string>("SELECT Name FROM [dbo].[Customer] where Id=@Id  ", parameters).FirstOrDefault();
+                    parameters.Add("@Id", customerId, DbType.Guid);
+                    name = await dbConn.QueryFirstOrDefaultAsync<string>("SELECT Name FROM [dbo].[Customer] where Id=@Id  ", parameters);
                 }
 
             }
@@ -52,17 +54,17 @@ namespace EVCB_OCPP.TaskScheduler.Services
         }
 
 
-        internal bool IsCallParterAPI(Guid customerId)
+        internal async Task<bool> IsCallParterAPI(Guid customerId)
         {
             bool result = false;
             try
             {
                 using (var dbConn = new SqlConnection(mainDBConnectString))
                 {
-                    dbConn.Open();
+                    //dbConn.Open();
                     var parameters = new DynamicParameters();
-                    parameters.Add("@Id", customerId.ToString(), System.Data.DbType.String);
-                    result = dbConn.ExecuteScalar<bool>("SELECT count(*) FROM [dbo].[Customer] where Id=@Id and CallPartnerApiOnSchedule=1 ", parameters);
+                    parameters.Add("@Id", customerId, DbType.Guid);
+                    result = await dbConn.ExecuteScalarAsync<bool>("SELECT count(*) FROM [dbo].[Customer] where Id=@Id and CallPartnerApiOnSchedule=1 ", parameters);
                 }
 
             }
@@ -99,7 +101,7 @@ namespace EVCB_OCPP.TaskScheduler.Services
             {
                 using (var dbConn = new SqlConnection(mainDBConnectString))
                 {
-                    dbConn.Open();
+                    //dbConn.Open();
                     result = dbConn.Query<Guid>("SELECT Id FROM [dbo].[Customer] where CallPartnerApiOnSchedule=1").ToList();
                 }
 
@@ -119,7 +121,7 @@ namespace EVCB_OCPP.TaskScheduler.Services
             {
                 using (var dbConn = new SqlConnection(mainDBConnectString))
                 {
-                    dbConn.Open();
+                    //dbConn.Open();
                     result = dbConn.Query<Guid>("SELECT Id FROM [dbo].[Customer] where CallPartnerApiOnSchedule=1 and InstantStopTxReport=0").ToList();
                 }
 
@@ -133,16 +135,16 @@ namespace EVCB_OCPP.TaskScheduler.Services
         }
 
 
-        internal List<Transaction> GetNeedReportSession(Guid customerId, bool isgoing, int size)
+        internal async  Task<List<Transaction>> GetNeedReportSession(Guid customerId, bool isgoing, int size)
         {
             List<Transaction> result = new List<Transaction>();
             try
             {
                 using (var dbConn = new SqlConnection(mainDBConnectString))
                 {
-                    dbConn.Open();
+                    //dbConn.Open();
                     var parameters = new DynamicParameters();
-                    parameters.Add("@CustomerId", customerId.ToString(), System.Data.DbType.String);
+                    parameters.Add("@CustomerId", customerId, DbType.Guid);
                     string sqlString = string.Empty;
 
                     // 20220211 revised
@@ -162,7 +164,7 @@ namespace EVCB_OCPP.TaskScheduler.Services
                         }
                     }
 
-                    result = dbConn.Query<Transaction>(sqlString, parameters).ToList();
+                    result = (await dbConn.QueryAsync<Transaction>(sqlString, parameters)).ToList();
                 }
 
             }
@@ -181,9 +183,9 @@ namespace EVCB_OCPP.TaskScheduler.Services
             {
                 using (var dbConn = new SqlConnection(mainDBConnectString))
                 {
-                    dbConn.Open();
+                    //dbConn.Open();
                     var parameters = new DynamicParameters();                  
-                    parameters.Add("@CustomerId", customerId.ToString(), System.Data.DbType.String);
+                    parameters.Add("@CustomerId", customerId, DbType.Guid);
                     string sqlString = string.Empty;
 
                     sqlString = "SELECT Top(" + size + ") Machine.CustomerId, record.Id, record.ChargeBoxId,record.Action,record.SerialNo,record.Status,record.EVSE_Value,record.EVSE_Status FROM[dbo].[MachineOperateRecord] record" +
@@ -191,10 +193,6 @@ namespace EVCB_OCPP.TaskScheduler.Services
                         "  where Machine.CustomerId =@CustomerId and Status!= 0 and RequestType = 1 and ReportedOn = '1991/01/01'";
                     result = dbConn.Query<MachineOperateRecord>(sqlString, parameters).ToList();
                 }
-
-
-
-
             }
             catch (Exception ex)
             {
@@ -205,30 +203,30 @@ namespace EVCB_OCPP.TaskScheduler.Services
         }
 
 
-        internal void ReportStartTx(Dictionary<int, TransactionResponse> reportResults)
+        internal async Task ReportStartTx(Dictionary<int, TransactionResponse> reportResults)
         {
 
             try
             {
-                using (var tranScope = new TransactionScope())
+                using (var dbConn = new SqlConnection(mainDBConnectString))
                 {
-                    using (var dbConn = new SqlConnection(mainDBConnectString))
+                    await dbConn.OpenAsync();
+                    using (var trans = await dbConn.BeginTransactionAsync())
                     {
-                        dbConn.Open();
+                        //dbConn.Open();
 
                         foreach (var kv in reportResults)
                         {
                             var parameters = new DynamicParameters();
                             parameters.Add("@Id", kv.Key, DbType.Int32, ParameterDirection.Input);
                             parameters.Add("@StartTransactionReportedOn", kv.Value.StartTransactionReportedOn, DbType.DateTime, ParameterDirection.Input);
-                            parameters.Add("@ErrorMsg", kv.Value.ErrorMsg, DbType.String, ParameterDirection.Input);
-                            dbConn.Execute("UPDATE [dbo].[TransactionRecord] set StartTransactionReportedOn=@StartTransactionReportedOn, ErrorMsg=@ErrorMsg  where Id=@Id", parameters);
+                            parameters.Add("@ErrorMsg", kv.Value.ErrorMsg, DbType.String, ParameterDirection.Input, -1);
+                            await trans.ExecuteAsync("UPDATE [dbo].[TransactionRecord] set StartTransactionReportedOn=@StartTransactionReportedOn, ErrorMsg=@ErrorMsg  where Id=@Id", parameters);
                         }
 
-                        tranScope.Complete();
+                        await trans.CommitAsync();
                     }
                 }
-
             }
             catch (Exception ex)
             {
@@ -238,30 +236,30 @@ namespace EVCB_OCPP.TaskScheduler.Services
 
         }
 
-        internal void ReportStopTx(Dictionary<int, TransactionResponse> reportResults)
+        internal async Task ReportStopTx(Dictionary<int, TransactionResponse> reportResults)
         {
 
             try
             {
-                using (var tranScope = new TransactionScope())
+                using (var dbConn = new SqlConnection(mainDBConnectString))
                 {
-                    using (var dbConn = new SqlConnection(mainDBConnectString))
+                    await dbConn.OpenAsync();
+                    using (var trans = await dbConn.BeginTransactionAsync())
                     {
-                        dbConn.Open();
+                        //dbConn.Open();
 
                         foreach (var kv in reportResults)
                         {
                             var parameters = new DynamicParameters();
                             parameters.Add("@Id", kv.Key, DbType.Int32, ParameterDirection.Input);
                             parameters.Add("@StopTransactionReportedOn", kv.Value.StopTransactionReportedOn, DbType.DateTime, ParameterDirection.Input);
-                            parameters.Add("@ErrorMsg", kv.Value.ErrorMsg, DbType.String, ParameterDirection.Input);
-                            dbConn.Execute("UPDATE [dbo].[TransactionRecord] set StopTransactionReportedOn=@StopTransactionReportedOn, ErrorMsg=@ErrorMsg  where Id=@Id", parameters);
+                            parameters.Add("@ErrorMsg", kv.Value.ErrorMsg, DbType.String, ParameterDirection.Input, -1);
+                            await trans.ExecuteAsync("UPDATE [dbo].[TransactionRecord] set StopTransactionReportedOn=@StopTransactionReportedOn, ErrorMsg=@ErrorMsg  where Id=@Id", parameters);
                         }
 
-                        tranScope.Complete();
+                        await trans.CommitAsync();
                     }
                 }
-
             }
             catch (Exception ex)
             {
@@ -272,15 +270,15 @@ namespace EVCB_OCPP.TaskScheduler.Services
         }
 
 
-        internal void TurntoTimeoutMachineOperateCommands(int intervalSeconds)
+        internal async Task TurntoTimeoutMachineOperateCommands(int intervalSeconds)
         {
 
             try
             {
                 using (var dbConn = new SqlConnection(mainDBConnectString))
                 {
-                    dbConn.Open();
-                    dbConn.Execute("CheckUnCommitMachineOperateCommand", new { @TimeoutIntervalSeconds = intervalSeconds }, null, null, commandType: CommandType.StoredProcedure);
+                    //dbConn.Open();
+                    await dbConn.ExecuteAsync("CheckUnCommitMachineOperateCommand", new { @TimeoutIntervalSeconds = intervalSeconds }, null, null, commandType: CommandType.StoredProcedure);
                 }
 
             }
@@ -292,15 +290,16 @@ namespace EVCB_OCPP.TaskScheduler.Services
 
         }
 
-        internal void ReportExecution(Dictionary<int, BasicResponse> reportResults)
+        internal async Task ReportExecution(Dictionary<int, BasicResponse> reportResults)
         {
             try
             {
-                using (var tranScope = new TransactionScope())
+                using (var dbConn = new SqlConnection(mainDBConnectString))
                 {
-                    using (var dbConn = new SqlConnection(mainDBConnectString))
+                    await dbConn.OpenAsync();
+                    using (var trans = await dbConn.BeginTransactionAsync())
                     {
-                        dbConn.Open();
+                        //dbConn.Open();
 
                         foreach (var kv in reportResults)
                         {
@@ -308,13 +307,12 @@ namespace EVCB_OCPP.TaskScheduler.Services
                             parameters.Add("@Id", kv.Key, DbType.Int32, ParameterDirection.Input);
                             parameters.Add("@ReportedOn", kv.Value.ReportedOn, DbType.DateTime, ParameterDirection.Input);
 
-                            dbConn.Execute("UPDATE [dbo].[MachineOperateRecord] set ReportedOn=@ReportedOn where Id=@Id", parameters);
+                            await trans.ExecuteAsync("UPDATE [dbo].[MachineOperateRecord] set ReportedOn=@ReportedOn where Id=@Id", parameters);
                         }
 
-                        tranScope.Complete();
+                        await trans.CommitAsync();
                     }
                 }
-
             }
             catch (Exception ex)
             {

+ 1 - 1
EVCB_OCPP.TaskScheduler/Services/ICustomerService.cs

@@ -8,7 +8,7 @@ namespace EVCB_OCPP.TaskScheduler.Services
 {
     public interface ICustomerService
     {
-        void SetCustomerId(Guid customerId);
+        Task SetCustomerId(Guid customerId);
 
         List<Guid> GetNotifyStopTransactionCustomers();