Skip to content

Commit e94c5ec

Browse files
Add async interfaces for http procedures (#987)
* Add async interfaces for http procedures * Define protected property AsyncEnabled and internal method GetAsyncEnabledInternal. * Improve performance on GetAjaxEncryptionKey. Don not print DumpHeaders if log is off
1 parent 3709a3a commit e94c5ec

File tree

14 files changed

+518
-28
lines changed

14 files changed

+518
-28
lines changed

dotnet/src/dotnetcore/GxClasses.Web/Middleware/HandlerFactory.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using GeneXus.Configuration;
99
using GeneXus.Http;
1010
using GeneXus.Mime;
11+
using GeneXus.Procedure;
1112
using GeneXus.Utils;
1213
using Microsoft.AspNetCore.Http;
1314

@@ -74,8 +75,16 @@ public async Task Invoke(HttpContext context)
7475
handler.sendAdditionalHeaders();
7576
return Task.CompletedTask;
7677
});
77-
handler.ProcessRequest(context);
78-
await Task.CompletedTask;
78+
GXWebProcedure gxWebProc = handler as GXWebProcedure;
79+
if (gxWebProc != null && gxWebProc.GetAsyncEnabledInternal())
80+
{
81+
await gxWebProc.ProcessRequestAsync(context);
82+
}
83+
else
84+
{
85+
handler.ProcessRequest(context);
86+
await Task.CompletedTask;
87+
}
7988
handler.ControlOutputWriter?.Flush();
8089
}
8190
else

dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ namespace GeneXus.Application
4949
using System.Security.Claims;
5050
using System.Security;
5151
using Microsoft.Net.Http.Headers;
52+
using System.Threading.Tasks;
53+
using GeneXus.Data.ADO;
5254

5355
public interface IGxContext
5456
{
@@ -1407,12 +1409,27 @@ public IGxDataStore GetDataStore(string id)
14071409
return ds;
14081410
return null;
14091411
}
1412+
#if NETCORE
1413+
internal async Task CloseConnectionsAsync()
1414+
{
1415+
GxUserInfo.RemoveHandle(this.handle);
1416+
foreach (GxDataStore ds in _DataStores)
1417+
await ds.CloseConnectionsAsync();
1418+
1419+
CloseConnectionsResources();
1420+
}
1421+
#endif
14101422
public void CloseConnections()
14111423
{
14121424
GxUserInfo.RemoveHandle(this.handle);
14131425
foreach (IGxDataStore ds in _DataStores)
14141426
ds.CloseConnections();
14151427

1428+
CloseConnectionsResources();
1429+
}
1430+
1431+
private void CloseConnectionsResources()
1432+
{
14161433
if (_reportHandlerToClose != null)
14171434
{
14181435
for (int i = 0; i < _reportHandlerToClose.Count; i++)

dotnet/src/dotnetframework/GxClasses/Core/Web/HttpAjaxContext.cs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -685,29 +685,36 @@ internal static JArray GetParmsJArray(Object[] parms)
685685

686686
public string GetAjaxEncryptionKey()
687687
{
688-
if (context.ReadSessionKey<string>(CryptoImpl.AJAX_ENCRYPTION_KEY) == null)
688+
string ajaxKey = context.ReadSessionKey<string>(CryptoImpl.AJAX_ENCRYPTION_KEY);
689+
if (ajaxKey == null)
689690
{
690-
if(!RecoverEncryptionKey())
691-
context.WriteSessionKey(CryptoImpl.AJAX_ENCRYPTION_KEY,CryptoImpl.GetRijndaelKey());
691+
string sessionKey;
692+
if (!RecoverEncryptionKey(out sessionKey)) {
693+
ajaxKey = CryptoImpl.GetRijndaelKey();
694+
context.WriteSessionKey(CryptoImpl.AJAX_ENCRYPTION_KEY, ajaxKey);
695+
}
696+
else
697+
{
698+
ajaxKey = sessionKey;
699+
}
692700
}
693-
return context.ReadSessionKey<string>(CryptoImpl.AJAX_ENCRYPTION_KEY);
701+
return ajaxKey;
694702
}
695-
private bool RecoverEncryptionKey()
703+
private bool RecoverEncryptionKey(out string sessionKey)
696704
{
697-
if ( (context.ReadSessionKey<string>(CryptoImpl.AJAX_ENCRYPTION_KEY) == null))
705+
sessionKey = null;
706+
if (context.HttpContext != null)
698707
{
699-
if (context.HttpContext != null)
708+
String clientKey = context.HttpContext.Request.Headers[CryptoImpl.AJAX_SECURITY_TOKEN];
709+
if (!string.IsNullOrEmpty(clientKey))
700710
{
701-
String clientKey = context.HttpContext.Request.Headers[CryptoImpl.AJAX_SECURITY_TOKEN];
702-
if (!string.IsNullOrEmpty(clientKey))
711+
bool correctKey;
712+
clientKey = CryptoImpl.DecryptRijndael(CryptoImpl.GX_AJAX_PRIVATE_IV + clientKey, CryptoImpl.GX_AJAX_PRIVATE_KEY, out correctKey);
713+
if (correctKey)
703714
{
704-
bool correctKey = false;
705-
clientKey = CryptoImpl.DecryptRijndael(CryptoImpl.GX_AJAX_PRIVATE_IV + clientKey, CryptoImpl.GX_AJAX_PRIVATE_KEY, out correctKey);
706-
if (correctKey)
707-
{
708-
context.WriteSessionKey(CryptoImpl.AJAX_ENCRYPTION_KEY, clientKey);
709-
return true;
710-
}
715+
sessionKey = clientKey;
716+
context.WriteSessionKey(CryptoImpl.AJAX_ENCRYPTION_KEY, clientKey);
717+
return true;
711718
}
712719
}
713720
}

dotnet/src/dotnetframework/GxClasses/Data/GXDataADO.cs

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.IO;
99
using System.Reflection;
1010
using System.Threading;
11+
using System.Threading.Tasks;
1112
using GeneXus.Application;
1213
using GeneXus.Cache;
1314
using GeneXus.Configuration;
@@ -212,7 +213,22 @@ public void RemoveAllConnections(int handle)
212213
throw e;
213214
}
214215
}
216+
#if NETCORE
217+
internal async Task RemoveConnectionAsync(int handle, string dataSource)
218+
{
215219

220+
ServerUserInformation sui;
221+
if (userConnections.TryGetValue(handle, out sui))
222+
{
223+
GXLogging.Debug(log, "RemoveConnection handle " + handle + ",datasource:" + dataSource);
224+
GxConnection con = sui[dataSource];
225+
if (sui.TryRemove(dataSource, out con))
226+
await con.DisposeAsync();
227+
ServerUserInformation suiDeleted;
228+
if (sui.Count == 0) userConnections.TryRemove(handle, out suiDeleted);
229+
}
230+
}
231+
#endif
216232
public void RemoveConnection(int handle, string dataSource)
217233
{
218234

@@ -411,7 +427,12 @@ public void Dispose()
411427
{
412428
Close();
413429
}
414-
430+
#if NETCORE
431+
internal async Task DisposeAsync()
432+
{
433+
await CloseAsync();
434+
}
435+
#endif
415436
public IGxDataStore DataStore
416437
{
417438
get{ return dataStore;}
@@ -736,7 +757,61 @@ public void Close()
736757
wmiconnection.CleanUp();
737758
}
738759
}
760+
#if NETCORE
761+
internal async Task CloseAsync()
762+
{
763+
if (connection != null)
764+
{
765+
GXLogging.Debug(log, "GxConnection.Close Id " + " connection State '" + connection.State + "'" + " handle:" + handle + " datastore:" + DataStore.Id);
766+
}
767+
if (connection != null && ((connection.State & ConnectionState.Closed) == 0))
768+
{
769+
try
770+
{
771+
connectionCache.Clear();
772+
}
773+
catch (Exception e)
774+
{
775+
GXLogging.Warn(log, "GxConnection.Close can't close all prepared cursors", e);
776+
}
739777

778+
GXLogging.Debug(log, "UncommitedChanges before Close:" + UncommitedChanges);
779+
try
780+
{
781+
if (UncommitedChanges)
782+
{
783+
rollbackTransactionOnly();
784+
UncommitedChanges = false;
785+
}
786+
}
787+
catch (Exception e)
788+
{
789+
GXLogging.Warn(log, "GxConnection.Close can't rollback transaction", e);
790+
}
791+
try
792+
{
793+
await connection.CloseAsync();
794+
if (transaction != null)
795+
{
796+
transaction.Dispose();
797+
transaction = null;
798+
}
799+
800+
}
801+
catch (Exception e)
802+
{
803+
GXLogging.Warn(log, "GxConnection.Close can't close connection", e);
804+
}
805+
spid = 0;
806+
GXLogging.Debug(log, "GxConnection.Close connection is closed ");
807+
}
808+
m_opened = false;
809+
if (Preferences.Instrumented && wmiconnection != null)
810+
{
811+
wmiconnection.CleanUp();
812+
}
813+
}
814+
#endif
740815
public int OpenHandles
741816
{
742817
get{return openHandles;}
@@ -2842,6 +2917,12 @@ public void CloseConnections()
28422917
{
28432918
GxConnectionManager.Instance.RemoveConnection(handle, id);
28442919
}
2920+
#if NETCORE
2921+
internal async Task CloseConnectionsAsync()
2922+
{
2923+
await ((GxConnectionManager)GxConnectionManager.Instance).RemoveConnectionAsync(handle, id);
2924+
}
2925+
#endif
28452926
public void Release()
28462927
{
28472928
}

dotnet/src/dotnetframework/GxClasses/Data/GXDataCommon.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using GeneXus.Metadata;
2626
using System.Data.Common;
2727
using System.Linq;
28+
using System.Threading.Tasks;
2829

2930
namespace GeneXus.Data
3031
{
@@ -4343,7 +4344,28 @@ public virtual void Close()
43434344
{
43444345
InternalConnection.Close();
43454346
}
4347+
#if NETCORE
4348+
internal virtual async Task CloseAsync()
4349+
{
4350+
try
4351+
{
43464352

4353+
DbConnection dbConnection = InternalConnection as DbConnection;
4354+
if (dbConnection != null)
4355+
{
4356+
await dbConnection.CloseAsync();
4357+
}
4358+
else
4359+
{
4360+
InternalConnection.Close();
4361+
}
4362+
}
4363+
catch (Exception ex)
4364+
{
4365+
throw new DataException(ex.Message, ex);
4366+
}
4367+
}
4368+
#endif
43474369
public void ChangeDatabase(String database)
43484370
{
43494371
throw new NotSupportedException("NoChangeMsg00" + database);

dotnet/src/dotnetframework/GxClasses/Data/GXDataDb2.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Reflection;
55
using System.Text;
66
using System.Threading;
7+
using System.Threading.Tasks;
78
using GeneXus.Application;
89
using GeneXus.Cache;
910
using GeneXus.Metadata;
@@ -655,7 +656,20 @@ override public void Close()
655656
throw new DataException(ex.Message, ex);
656657
}
657658
}
658-
659+
#if NETCORE
660+
internal override async Task CloseAsync()
661+
{
662+
try
663+
{
664+
CheckState(false);
665+
await base.CloseAsync();
666+
}
667+
catch (Exception ex)
668+
{
669+
throw new DataException(ex.Message, ex);
670+
}
671+
}
672+
#endif
659673
override public IDbCommand CreateCommand()
660674
{
661675
return InternalConnection.CreateCommand();

dotnet/src/dotnetframework/GxClasses/Data/GXDataHana.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Reflection;
66
using System.Text;
7+
using System.Threading.Tasks;
78
using GeneXus.Cache;
89
using GeneXus.Metadata;
910
using GeneXus.Utils;
@@ -415,15 +416,27 @@ override public void Close()
415416
{
416417
try
417418
{
418-
CheckState(false);
419419
InternalConnection.Close();
420420
}
421421
catch (Exception ex)
422422
{
423423
throw new DataException(ex.Message, ex);
424424
}
425425
}
426-
426+
#if NETCORE
427+
internal override async Task CloseAsync()
428+
{
429+
try
430+
{
431+
CheckState(false);
432+
await base.CloseAsync();
433+
}
434+
catch (Exception ex)
435+
{
436+
throw new DataException(ex.Message, ex);
437+
}
438+
}
439+
#endif
427440
override public IDbCommand CreateCommand()
428441
{
429442
return InternalConnection.CreateCommand();

dotnet/src/dotnetframework/GxClasses/Data/GXDataNTierService.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,6 @@ override public void Close()
363363
{
364364
try
365365
{
366-
CheckState(false);
367366
InternalConnection.Close();
368367
}
369368
catch (Exception ex)

dotnet/src/dotnetframework/GxClasses/Helpers/GXLogging.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,15 @@ public static void Warn(ILog log, string msg, Exception ex)
429429
log.Warn(msg, ex);
430430
}
431431
}
432+
internal static void DebugSanitized(IGXLogger log, string startMsg, Func<string> buildMsg)
433+
{
434+
if (log.IsDebugEnabled)
435+
{
436+
string msg = buildMsg();
437+
DebugSanitized(log, startMsg + msg);
438+
}
439+
}
440+
432441
public static void DebugSanitized(ILog log, Exception ex, params string[] list)
433442
{
434443
if (log.IsDebugEnabled)

0 commit comments

Comments
 (0)