Skip to content

Commit 72fe29c

Browse files
authored
chore: File-scoped namespace refactor, phase 9 (HostedWebCore, IntegrationTestHelpers, SharedApplications) (#3429)
1 parent 488a124 commit 72fe29c

148 files changed

Lines changed: 13032 additions & 13232 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tests/Agent/IntegrationTests/HostedWebCore/HostedWebCore.cs

Lines changed: 65 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -11,94 +11,93 @@
1111
using System.Runtime.InteropServices;
1212
using System.Threading;
1313

14-
namespace HostedWebCore
14+
namespace HostedWebCore;
15+
16+
public class HostedWebCore
1517
{
16-
public class HostedWebCore
17-
{
18-
private const int ServerTimeoutShutdownMinutes = 5;
18+
private const int ServerTimeoutShutdownMinutes = 5;
1919

20-
private readonly string _port;
20+
private readonly string _port;
2121

22-
private static string AssemblyDirectory
22+
private static string AssemblyDirectory
23+
{
24+
get
2325
{
24-
get
25-
{
26-
Contract.Ensures(Contract.Result<string>() != null);
26+
Contract.Ensures(Contract.Result<string>() != null);
2727

28-
var codeBase = Assembly.GetExecutingAssembly().CodeBase;
29-
var uri = new UriBuilder(codeBase);
30-
var path = Uri.UnescapeDataString(uri.Uri.LocalPath);
31-
return Path.GetDirectoryName(path);
32-
}
28+
var codeBase = Assembly.GetExecutingAssembly().CodeBase;
29+
var uri = new UriBuilder(codeBase);
30+
var path = Uri.UnescapeDataString(uri.Uri.LocalPath);
31+
return Path.GetDirectoryName(path);
3332
}
33+
}
3434

35-
private static string ApplicationHostConfigFilePath
35+
private static string ApplicationHostConfigFilePath
36+
{
37+
get
3638
{
37-
get
38-
{
39-
Contract.Ensures(Contract.Result<string>() != null);
40-
return AssemblyDirectory + @"\applicationHost.config";
41-
}
39+
Contract.Ensures(Contract.Result<string>() != null);
40+
return AssemblyDirectory + @"\applicationHost.config";
4241
}
42+
}
4343

44-
[ContractInvariantMethod]
45-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Required for code contracts.")]
46-
private void ObjectInvariant()
47-
{
48-
Contract.Invariant(_port != null);
49-
}
44+
[ContractInvariantMethod]
45+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Required for code contracts.")]
46+
private void ObjectInvariant()
47+
{
48+
Contract.Invariant(_port != null);
49+
}
5050

51-
public HostedWebCore(string port)
52-
{
53-
Contract.Requires(port != null);
51+
public HostedWebCore(string port)
52+
{
53+
Contract.Requires(port != null);
5454

55-
_port = port;
56-
}
55+
_port = port;
56+
}
5757

58-
public void Run()
58+
public void Run()
59+
{
60+
StartWebServer();
61+
//The HWC creates this shutdown event and waits for the test runner to set so that it can shutdown.
62+
using (var eventWaitHandle = new EventWaitHandle(false, EventResetMode.ManualReset,
63+
"app_server_wait_for_all_request_done_" + _port.ToString()))
5964
{
60-
StartWebServer();
61-
//The HWC creates this shutdown event and waits for the test runner to set so that it can shutdown.
62-
using (var eventWaitHandle = new EventWaitHandle(false, EventResetMode.ManualReset,
63-
"app_server_wait_for_all_request_done_" + _port.ToString()))
64-
{
65-
CreatePidFile();
66-
eventWaitHandle.WaitOne(TimeSpan.FromMinutes(ServerTimeoutShutdownMinutes));
67-
}
65+
CreatePidFile();
66+
eventWaitHandle.WaitOne(TimeSpan.FromMinutes(ServerTimeoutShutdownMinutes));
6867
}
68+
}
6969

70-
private void StartWebServer()
71-
{
72-
int maxRetries = 3;
73-
int curRetry = 0;
70+
private void StartWebServer()
71+
{
72+
int maxRetries = 3;
73+
int curRetry = 0;
7474

75-
while (true)
75+
while (true)
76+
{
77+
try
7678
{
77-
try
78-
{
79-
var hResult = NativeMethods.WebCoreActivate(ApplicationHostConfigFilePath, null, @".NET Agent Integration Test Web Host");
80-
Marshal.ThrowExceptionForHR(hResult);
79+
var hResult = NativeMethods.WebCoreActivate(ApplicationHostConfigFilePath, null, @".NET Agent Integration Test Web Host");
80+
Marshal.ThrowExceptionForHR(hResult);
8181

82-
return; // success
83-
}
84-
catch
82+
return; // success
83+
}
84+
catch
85+
{
86+
if (curRetry++ < maxRetries)
8587
{
86-
if (curRetry++ < maxRetries)
87-
{
88-
Thread.Sleep(500);
89-
}
90-
else
91-
throw; // all retries failed
88+
Thread.Sleep(500);
9289
}
90+
else
91+
throw; // all retries failed
9392
}
9493
}
94+
}
9595

96-
private static void CreatePidFile()
97-
{
98-
var pid = Process.GetCurrentProcess().Id;
99-
var thisAssemblyPath = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath;
100-
var pidFilePath = thisAssemblyPath + ".pid";
101-
File.WriteAllText(pidFilePath, pid.ToString(CultureInfo.InvariantCulture));
102-
}
96+
private static void CreatePidFile()
97+
{
98+
var pid = Process.GetCurrentProcess().Id;
99+
var thisAssemblyPath = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath;
100+
var pidFilePath = thisAssemblyPath + ".pid";
101+
File.WriteAllText(pidFilePath, pid.ToString(CultureInfo.InvariantCulture));
103102
}
104-
}
103+
}

tests/Agent/IntegrationTests/HostedWebCore/NativeMethods.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44

5-
using System;
65
using System.Runtime.InteropServices;
76

8-
namespace HostedWebCore
7+
namespace HostedWebCore;
8+
9+
public static class NativeMethods
910
{
10-
public static class NativeMethods
11-
{
12-
[DllImport(@"inetsrv\hwebcore.dll")]
13-
public static extern int WebCoreActivate(
14-
[In, MarshalAs(UnmanagedType.LPWStr)] string appHostConfigPath,
15-
[In, MarshalAs(UnmanagedType.LPWStr)] string rootWebConfigPath,
16-
[In, MarshalAs(UnmanagedType.LPWStr)] string instanceName);
11+
[DllImport(@"inetsrv\hwebcore.dll")]
12+
public static extern int WebCoreActivate(
13+
[In, MarshalAs(UnmanagedType.LPWStr)] string appHostConfigPath,
14+
[In, MarshalAs(UnmanagedType.LPWStr)] string rootWebConfigPath,
15+
[In, MarshalAs(UnmanagedType.LPWStr)] string instanceName);
1716

18-
[DllImport(@"inetsrv\hwebcore.dll")]
19-
public static extern int WebCoreShutdown(bool immediate);
20-
}
21-
}
17+
[DllImport(@"inetsrv\hwebcore.dll")]
18+
public static extern int WebCoreShutdown(bool immediate);
19+
}

tests/Agent/IntegrationTests/HostedWebCore/Options.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44

5-
using System;
65
using CommandLine;
76

8-
namespace HostedWebCore
7+
namespace HostedWebCore;
8+
9+
internal class Options
910
{
10-
internal class Options
11-
{
12-
[Option("port", Required = true)]
13-
public string Port { get; set; }
14-
}
15-
}
11+
[Option("port", Required = true)]
12+
public string Port { get; set; }
13+
}

tests/Agent/IntegrationTests/HostedWebCore/Program.cs

Lines changed: 56 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,73 +5,72 @@
55
using System;
66
using System.Collections;
77
using System.Diagnostics.Contracts;
8-
using CommandLine;
98
using System.IO;
109
using System.Text;
10+
using CommandLine;
11+
12+
namespace HostedWebCore;
1113

12-
namespace HostedWebCore
14+
static class Program
1315
{
14-
static class Program
16+
private static void Log(string format)
1517
{
16-
private static void Log(string format)
17-
{
18-
string prefix = string.Format("[{0} {1}-{2}] HostedWebCore: ", DateTime.Now,
19-
System.Diagnostics.Process.GetCurrentProcess().Id,
20-
System.Threading.Thread.CurrentThread.ManagedThreadId);
21-
Console.WriteLine(prefix + format);
22-
}
18+
string prefix = string.Format("[{0} {1}-{2}] HostedWebCore: ", DateTime.Now,
19+
System.Diagnostics.Process.GetCurrentProcess().Id,
20+
System.Threading.Thread.CurrentThread.ManagedThreadId);
21+
Console.WriteLine(prefix + format);
22+
}
2323

24-
private static void Main(string[] args)
25-
{
26-
string msg = "Firing up...args: " + string.Join(", ", args);
27-
Log("Firing up...args: " + string.Join(", ", args));
28-
Log("Starting directory: " + Directory.GetCurrentDirectory());
24+
private static void Main(string[] args)
25+
{
26+
string msg = "Firing up...args: " + string.Join(", ", args);
27+
Log("Firing up...args: " + string.Join(", ", args));
28+
Log("Starting directory: " + Directory.GetCurrentDirectory());
2929

30-
var environmentVariables = new StringBuilder();
31-
foreach (DictionaryEntry de in Environment.GetEnvironmentVariables())
32-
{
33-
// strip newlines in the environment variable value - otherwise our log parsing may fail
34-
var valueStr = de.Value?.ToString().Replace("\r", string.Empty).Replace("\n", string.Empty);
35-
environmentVariables.Append($" {de.Key} = {valueStr}; ");
36-
}
37-
Log("Environment Variables: " + environmentVariables.ToString());
30+
var environmentVariables = new StringBuilder();
31+
foreach (DictionaryEntry de in Environment.GetEnvironmentVariables())
32+
{
33+
// strip newlines in the environment variable value - otherwise our log parsing may fail
34+
var valueStr = de.Value?.ToString().Replace("\r", string.Empty).Replace("\n", string.Empty);
35+
environmentVariables.Append($" {de.Key} = {valueStr}; ");
36+
}
37+
Log("Environment Variables: " + environmentVariables.ToString());
3838

39-
if (Parser.Default == null)
40-
throw new NullReferenceException("CommandLine.Parser.Default");
39+
if (Parser.Default == null)
40+
throw new NullReferenceException("CommandLine.Parser.Default");
4141

42-
var options = new Options();
43-
if (!Parser.Default.ParseArgumentsStrict(args, options))
44-
return;
42+
var options = new Options();
43+
if (!Parser.Default.ParseArgumentsStrict(args, options))
44+
return;
4545

46-
Contract.Assume(options.Port != null);
46+
Contract.Assume(options.Port != null);
4747

48-
try
49-
{
50-
var hostedWebCore = new HostedWebCore(options.Port);
51-
Log("Starting server...");
52-
hostedWebCore.Run();
53-
Log("Done.");
54-
}
55-
catch (DllNotFoundException ex)
56-
{
57-
Log($"HostedWebCore.exe failed: Check that the Hostable Web Core Windows feature is installed.: {ex}");
58-
Environment.Exit(4);
59-
}
60-
catch (FileNotFoundException ex)
61-
{
62-
Log($"HostedWebCore.exe failed: Running HostedWebCore.exe requires certain IIS components to be installed.: {ex}");
63-
Environment.Exit(3);
64-
}
65-
catch (UnauthorizedAccessException ex)
66-
{
67-
Log($"HostedWebCore.exe failed: This application must be run with administrator privileges.: {ex}");
68-
Environment.Exit(2);
69-
}
70-
catch (Exception ex)
71-
{
72-
Log($"HostedWebCore.exe failed: Reason unknown.: {ex}");
73-
Environment.Exit(1);
74-
}
48+
try
49+
{
50+
var hostedWebCore = new HostedWebCore(options.Port);
51+
Log("Starting server...");
52+
hostedWebCore.Run();
53+
Log("Done.");
54+
}
55+
catch (DllNotFoundException ex)
56+
{
57+
Log($"HostedWebCore.exe failed: Check that the Hostable Web Core Windows feature is installed.: {ex}");
58+
Environment.Exit(4);
59+
}
60+
catch (FileNotFoundException ex)
61+
{
62+
Log($"HostedWebCore.exe failed: Running HostedWebCore.exe requires certain IIS components to be installed.: {ex}");
63+
Environment.Exit(3);
64+
}
65+
catch (UnauthorizedAccessException ex)
66+
{
67+
Log($"HostedWebCore.exe failed: This application must be run with administrator privileges.: {ex}");
68+
Environment.Exit(2);
69+
}
70+
catch (Exception ex)
71+
{
72+
Log($"HostedWebCore.exe failed: Reason unknown.: {ex}");
73+
Environment.Exit(1);
7574
}
7675
}
77-
}
76+
}

0 commit comments

Comments
 (0)