/
Extensions.cs
166 lines (150 loc) · 5.74 KB
/
Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Data;
using System.Data.Common;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using NServiceBus.Extensibility;
using NServiceBus.Settings;
using NServiceBus.Transport;
static class Extensions
{
public static void AddParameter(this DbCommand command, string name, object value)
{
var parameter = command.CreateParameter();
parameter.ParameterName = name;
parameter.Value = value;
command.Parameters.Add(parameter);
}
public static void AddParameter(this DbCommand command, string name, DateTime value)
{
var parameter = command.CreateParameter();
parameter.ParameterName = name;
parameter.Value = value;
parameter.DbType = DbType.DateTime;
command.Parameters.Add(parameter);
}
public static Task<DbConnection> OpenNonContextualConnection(this IConnectionManager connectionManager, CancellationToken cancellationToken = default)
{
var connection = connectionManager.BuildNonContextual();
return OpenConnection(connection, cancellationToken);
}
public static Task<DbConnection> OpenConnection(this IConnectionManager connectionManager, IncomingMessage incomingMessage, CancellationToken cancellationToken = default)
{
var connection = connectionManager.Build(incomingMessage);
return OpenConnection(connection, cancellationToken);
}
static async Task<DbConnection> OpenConnection(DbConnection connection, CancellationToken cancellationToken)
{
try
{
await connection.OpenAsync(cancellationToken).ConfigureAwait(false);
return connection;
}
catch (Exception ex) when (ex.IsCausedBy(cancellationToken))
{
// copy the general catch but don't let another exception mask the OCE
try
{
connection?.Dispose();
}
catch { }
throw;
}
catch (Exception)
{
connection?.Dispose();
throw;
}
}
public static void AddParameter(this DbCommand command, string name, Version value)
{
command.AddParameter(name, value.ToString());
}
public static async Task<bool> GetBoolAsync(this DbDataReader reader, int position, CancellationToken cancellationToken = default)
{
var type = reader.GetFieldType(position);
// MySql stores bools as longs (ulong)
if (type == typeof(ulong))
{
return Convert.ToBoolean(await reader.GetFieldValueAsync<ulong>(position, cancellationToken).ConfigureAwait(false));
}
// In Oracle default driver store bools as NUMBER(1,0) (short).
if (type == typeof(short))
{
return Convert.ToBoolean(await reader.GetFieldValueAsync<short>(position, cancellationToken).ConfigureAwait(false));
}
// or it might be stored as ints.
if (type == typeof(int))
{
return Convert.ToBoolean(await reader.GetFieldValueAsync<int>(position, cancellationToken).ConfigureAwait(false));
}
return await reader.GetFieldValueAsync<bool>(position, cancellationToken).ConfigureAwait(false);
}
public static async Task<Guid> GetGuidAsync(this DbDataReader reader, int position, CancellationToken cancellationToken = default)
{
var type = reader.GetFieldType(position);
// MySql stores Guids as strings
if (type == typeof(string))
{
return new Guid(await reader.GetFieldValueAsync<string>(position, cancellationToken).ConfigureAwait(false));
}
return await reader.GetFieldValueAsync<Guid>(position, cancellationToken).ConfigureAwait(false);
}
internal static Func<T, object> GetPropertyAccessor<T>(this Type sagaDataType, string propertyName)
{
var propertyInfo = sagaDataType.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
?? throw new Exception($"Expected '{sagaDataType.FullName}' to contain a gettable property named '{propertyName}'.");
return data => propertyInfo.GetValue(data);
}
public static async Task<int> ExecuteNonQueryEx(this DbCommand command, CancellationToken cancellationToken = default)
{
try
{
return await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
}
catch (Exception ex) when (!ex.IsCausedBy(cancellationToken))
{
var message = $"Failed to ExecuteNonQuery. CommandText:{Environment.NewLine}{command.CommandText}";
throw new Exception(message, ex);
}
}
public static bool IsSubclassOfRawGeneric(this Type toCheck, Type generic)
{
while (toCheck != null && toCheck != typeof(object))
{
Type current;
if (toCheck.IsGenericType)
{
current = toCheck.GetGenericTypeDefinition();
}
else
{
current = toCheck;
}
if (generic == current)
{
return true;
}
toCheck = toCheck.BaseType;
}
return false;
}
public static IncomingMessage GetIncomingMessage(this ContextBag context)
{
if (context == null)
{
// Tests pass a null context
return null;
}
if (context.TryGet(out IncomingMessage message))
{
return message;
}
throw new Exception("Can't find message headers from context.");
}
public static bool EndpointIsMultiTenant(this IReadOnlySettings settings)
{
return settings.GetOrDefault<bool>("SqlPersistence.MultiTenant");
}
}