diff --git a/src/System.Private.ServiceModel/tools/test/Bridge/Bridge/AppDomainManager.cs b/src/System.Private.ServiceModel/tools/test/Bridge/Bridge/AppDomainManager.cs index 00bb8ec00b2..f00fb68b88c 100644 --- a/src/System.Private.ServiceModel/tools/test/Bridge/Bridge/AppDomainManager.cs +++ b/src/System.Private.ServiceModel/tools/test/Bridge/Bridge/AppDomainManager.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Linq; using System.Runtime.CompilerServices; using WcfTestBridgeCommon; @@ -59,6 +60,14 @@ public static string CreateAppDomain(string path) return friendlyName; } + public static void ShutdownAllAppDomains() + { + foreach (string domainName in TypeCache.AppDomains.Keys.ToArray()) + { + ShutdownAppDomain(domainName); + } + } + public static void ShutdownAppDomain(string appDomainName) { if (String.IsNullOrWhiteSpace(appDomainName)) diff --git a/src/System.Private.ServiceModel/tools/test/Bridge/Bridge/BridgeController.cs b/src/System.Private.ServiceModel/tools/test/Bridge/Bridge/BridgeController.cs index bfcf965d558..a9c7c079155 100644 --- a/src/System.Private.ServiceModel/tools/test/Bridge/Bridge/BridgeController.cs +++ b/src/System.Private.ServiceModel/tools/test/Bridge/Bridge/BridgeController.cs @@ -82,6 +82,18 @@ public HttpResponseMessage Delete(HttpRequestMessage request) public static void ReleaseAllResources() { + // Cleanly shutdown all AppDomains we own so they have + // the chance to release resources they've acquired or installed + AppDomainManager.ShutdownAllAppDomains(); + + // Force the removal of the SSL cert that may have been added + // by another AppDomain or left from a prior run + int httpsPort = ConfigController.BridgeConfiguration.BridgeHttpsPort; + if (httpsPort != 0) + { + CertificateManager.UninstallSslPortCertificate(httpsPort); + } + CertificateManager.UninstallAllCertificates(); PortManager.RemoveAllBridgeFirewallRules(); } diff --git a/src/System.Private.ServiceModel/tools/test/Bridge/WcfTestBridgeCommon/AssemblyLoader.cs b/src/System.Private.ServiceModel/tools/test/Bridge/WcfTestBridgeCommon/AssemblyLoader.cs index 3d916efc7df..88298c9f07e 100644 --- a/src/System.Private.ServiceModel/tools/test/Bridge/WcfTestBridgeCommon/AssemblyLoader.cs +++ b/src/System.Private.ServiceModel/tools/test/Bridge/WcfTestBridgeCommon/AssemblyLoader.cs @@ -13,6 +13,17 @@ public class AssemblyLoader : MarshalByRefObject { private const string TypeList = "TypeList"; + public AssemblyLoader() + { + AppDomain.CurrentDomain.DomainUnload += (s, e) => + { + // Uninstall all certificates we explicitly added within this AppDomain + CertificateManager.UninstallAllSslPortCertificates(); + CertificateManager.UninstallAllMyCertificates(); + CertificateManager.UninstallAllRootCertificates(); + }; + } + public List GetTypes() { return (AppDomain.CurrentDomain.GetData(TypeList) as Dictionary).Keys.ToList(); diff --git a/src/System.Private.ServiceModel/tools/test/Bridge/WcfTestBridgeCommon/CertificateManager.cs b/src/System.Private.ServiceModel/tools/test/Bridge/WcfTestBridgeCommon/CertificateManager.cs index b9b1341c012..0fa6f94e7d8 100644 --- a/src/System.Private.ServiceModel/tools/test/Bridge/WcfTestBridgeCommon/CertificateManager.cs +++ b/src/System.Private.ServiceModel/tools/test/Bridge/WcfTestBridgeCommon/CertificateManager.cs @@ -168,7 +168,7 @@ public static string InstallMyCertificate(BridgeConfiguration configuration, str } } - private static void UninstallAllRootCertificates() + public static void UninstallAllRootCertificates() { lock (s_certificateLock) { @@ -179,7 +179,7 @@ private static void UninstallAllRootCertificates() } } - private static void UninstallAllMyCertificates() + public static void UninstallAllMyCertificates() { lock (s_certificateLock) { @@ -237,15 +237,24 @@ public static void InstallSSLPortCertificate(string certThumbprint, int port) { process.WaitForExit(); Console.WriteLine("Process exit code was {0}", process.ExitCode); - Console.WriteLine("stdout was: {0}", process.StandardOutput.ReadToEnd()); - Console.WriteLine("stderr was: {0}", process.StandardError.ReadToEnd()); + string output = process.StandardOutput.ReadToEnd(); + if (!String.IsNullOrWhiteSpace(output)) + { + Console.WriteLine("stdout was: {0}", output); + } + + output = process.StandardError.ReadToEnd(); + if (!String.IsNullOrWhiteSpace(output)) + { + Console.WriteLine("stderr was: {0}", output); + } } s_sslPorts[port] = certThumbprint; } } - private static void UninstallAllSslPortCertificates() + public static void UninstallAllSslPortCertificates() { foreach (int port in s_sslPorts.Keys.ToArray()) { @@ -253,25 +262,37 @@ private static void UninstallAllSslPortCertificates() } } - private static void UninstallSslPortCertificate(int port) + public static void UninstallSslPortCertificate(int port) { - ProcessStartInfo startInfo = new ProcessStartInfo(); - startInfo.Arguments = String.Format("http delete sslcert ipport=0.0.0.0:{0}", - port); - startInfo.FileName = "netsh"; - Console.WriteLine("Executing: {0} {1}", startInfo.FileName, startInfo.Arguments); - startInfo.UseShellExecute = false; - startInfo.RedirectStandardOutput = true; - startInfo.RedirectStandardError = true; - using (Process process = Process.Start(startInfo)) + lock (s_certificateLock) { - process.WaitForExit(); - Console.WriteLine("Process exit code was {0}", process.ExitCode); - Console.WriteLine("stdout was: {0}", process.StandardOutput.ReadToEnd()); - Console.WriteLine("stderr was: {0}", process.StandardError.ReadToEnd()); - } + ProcessStartInfo startInfo = new ProcessStartInfo(); + startInfo.Arguments = String.Format("http delete sslcert ipport=0.0.0.0:{0}", + port); + startInfo.FileName = "netsh"; + Console.WriteLine("Executing: {0} {1}", startInfo.FileName, startInfo.Arguments); + startInfo.UseShellExecute = false; + startInfo.RedirectStandardOutput = true; + startInfo.RedirectStandardError = true; + using (Process process = Process.Start(startInfo)) + { + process.WaitForExit(); + Console.WriteLine("Process exit code was {0}", process.ExitCode); + string output = process.StandardOutput.ReadToEnd(); + if (!String.IsNullOrWhiteSpace(output)) + { + Console.WriteLine("stdout was: {0}", output); + } + + output = process.StandardError.ReadToEnd(); + if (!String.IsNullOrWhiteSpace(output)) + { + Console.WriteLine("stderr was: {0}", output); + } + } - s_sslPorts.Remove(port); + s_sslPorts.Remove(port); + } } } }