Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly unload cleanup helper app domain after an isolated service is disposed #493

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ void domain_DomainUnload(object sender, EventArgs e)
}
}
// We are not needed anymore.
//AppDomain.Unload(AppDomain.CurrentDomain);
AppDomain.Unload(AppDomain.CurrentDomain);
});
t.IsBackground = false;
t.Start();
Expand Down
57 changes: 57 additions & 0 deletions src/test/Test.RazorEngine.Core/RazorEngineCleanupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Test.RazorEngine
Expand Down Expand Up @@ -102,5 +104,60 @@ public void RazorEngineService_TestDisableTempFileLocking()
data.DeleteAll();
Assert.IsFalse(Directory.Exists(folder));
}

// The test below requires a COM object to run that is not available on the CI server.
// To run, add the following as a COM reference to the project: - C:\WINDOWS\Microsoft.NET\Framework\vXXXXXX\mscoree.tlb

//[Serializable]
//public class TemplateModel
//{
// public string Name { get; set; }
//}

//[Test]
//public void RazorEngineService_CleanupDomainIsUnloaded()
//{
// Func<bool> CleanupDomainExists = () =>
// GetAppDomains()
// .Any(x => x.FriendlyName.StartsWith("CleanupHelperDomain_"));

// Assert.False(CleanupDomainExists(), "Cleanup helper app domain should not exist before test is run.");
// using (var service = IsolatedRazorEngineService.Create())
// {
// service.AddTemplate("TestTemplate",
// "Hello @Model.Name " + new String(Enumerable.Repeat('A', 100000).ToArray()));
// service.Compile("TestTemplate", typeof(TemplateModel));

// Assert.True(CleanupDomainExists(), "Cleanup helper app domain should exist after a template is compiled");
// }

// Thread.Sleep(300);
// Assert.False(CleanupDomainExists(), "Cleanup helper app domain must be properly unloaded after service is disposed");
//}

//private static IList<AppDomain> GetAppDomains()
//{
// IList<AppDomain> _IList = new List<AppDomain>();
// IntPtr enumHandle = IntPtr.Zero;
// mscoree.ICorRuntimeHost host = new mscoree.CorRuntimeHost();
// try
// {
// host.EnumDomains(out enumHandle);
// object domain = null;
// while (true)
// {
// host.NextDomain(enumHandle, out domain);
// if (domain == null) break;
// AppDomain appDomain = (AppDomain)domain;
// _IList.Add(appDomain);
// }
// return _IList;
// }
// finally
// {
// host.CloseEnum(enumHandle);
// Marshal.ReleaseComObject(host);
// }
//}
}
}