8
8
9
9
namespace Test
10
10
{
11
- public static class Util
11
+ public class Util : IDisposable
12
12
{
13
- private static readonly ManagementClient s_managementClient ;
13
+ private readonly ManagementClient _managementClient ;
14
14
private static readonly bool s_isWindows = false ;
15
15
16
16
static Util ( )
17
17
{
18
- var managementUri = new Uri ( "http://localhost:15672" ) ;
19
- s_managementClient = new ManagementClient ( managementUri , "guest" , "guest" ) ;
20
18
s_isWindows = InitIsWindows ( ) ;
21
19
}
22
20
23
- public static string Now => DateTime . UtcNow . ToString ( "o ", CultureInfo . InvariantCulture ) ;
24
-
25
- public static bool IsWindows => s_isWindows ;
21
+ public Util ( ) : this ( "guest ", "guest" )
22
+ {
23
+ }
26
24
27
- private static bool InitIsWindows ( )
25
+ public Util ( string managementUsername , string managementPassword )
28
26
{
29
- PlatformID platform = Environment . OSVersion . Platform ;
30
- if ( platform == PlatformID . Win32NT )
27
+ if ( string . IsNullOrEmpty ( managementUsername ) )
31
28
{
32
- return true ;
29
+ throw new ArgumentNullException ( nameof ( managementUsername ) ) ;
33
30
}
34
31
35
- string os = Environment . GetEnvironmentVariable ( "OS" ) ;
36
- if ( os != null )
32
+ if ( string . IsNullOrEmpty ( managementPassword ) )
37
33
{
38
- os = os . Trim ( ) ;
39
- return os == "Windows_NT" ;
34
+ throw new ArgumentNullException ( nameof ( managementPassword ) ) ;
40
35
}
41
36
42
- return false ;
37
+ var managementUri = new Uri ( "http://localhost:15672" ) ;
38
+ _managementClient = new ManagementClient ( managementUri , managementUsername , managementPassword ) ;
43
39
}
44
40
45
- public static async Task CloseConnectionAsync ( IConnection conn )
41
+ public static string Now => DateTime . UtcNow . ToString ( "o" , CultureInfo . InvariantCulture ) ;
42
+
43
+ public static bool IsWindows => s_isWindows ;
44
+
45
+ public async Task CloseConnectionAsync ( IConnection conn )
46
46
{
47
47
ushort tries = 1 ;
48
48
EasyNetQ . Management . Client . Model . Connection connectionToClose = null ;
@@ -61,7 +61,7 @@ public static async Task CloseConnectionAsync(IConnection conn)
61
61
62
62
await Task . Delay ( TimeSpan . FromSeconds ( delaySeconds ) ) ;
63
63
64
- connections = await s_managementClient . GetConnectionsAsync ( ) ;
64
+ connections = await _managementClient . GetConnectionsAsync ( ) ;
65
65
} while ( connections . Count == 0 ) ;
66
66
67
67
connectionToClose = connections . Where ( c0 =>
@@ -79,7 +79,7 @@ public static async Task CloseConnectionAsync(IConnection conn)
79
79
{
80
80
try
81
81
{
82
- await s_managementClient . CloseConnectionAsync ( connectionToClose ) ;
82
+ await _managementClient . CloseConnectionAsync ( connectionToClose ) ;
83
83
return ;
84
84
}
85
85
catch ( UnexpectedHttpStatusCodeException )
@@ -94,5 +94,25 @@ public static async Task CloseConnectionAsync(IConnection conn)
94
94
throw new InvalidOperationException ( $ "Could not delete connection: '{ conn . ClientProvidedName } '") ;
95
95
}
96
96
}
97
+
98
+ public void Dispose ( ) => _managementClient . Dispose ( ) ;
99
+
100
+ private static bool InitIsWindows ( )
101
+ {
102
+ PlatformID platform = Environment . OSVersion . Platform ;
103
+ if ( platform == PlatformID . Win32NT )
104
+ {
105
+ return true ;
106
+ }
107
+
108
+ string os = Environment . GetEnvironmentVariable ( "OS" ) ;
109
+ if ( os != null )
110
+ {
111
+ os = os . Trim ( ) ;
112
+ return os == "Windows_NT" ;
113
+ }
114
+
115
+ return false ;
116
+ }
97
117
}
98
118
}
0 commit comments