There is a problem with loading Assemblys in the ApplicationDomain of NCrunch while using NCrunch:
Assembly A - *.cs file:
Code:
namespace ClassLibrary2
{
public interface IInterface
{
int I { get; set; }
}
public class ClassInterface : IInterface
{
public int I { get; set; }
}
public class ClassFactory
{
public static object GetClassInterface()
{
var context = new Spring.Context.Support.XmlApplicationContext("WeavingInfo.xml");
return context.GetObject("abc");
}
}
}
Assembly B (Tests) - WeavingInfo.xml file:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net" default-lazy-init="true">
<object id="abc" type="ClassLibrary2.ClassInterface"/>
</objects>
Assembly B (Tests) - *.cs file:
Code:
namespace Tests
{
[TestFixture]
public class TestClass
{
[Test]
public void test()
{ //////////
IInterface x = null;
//object y = new ClassABC();
x = (IInterface)ClassFactory.GetClassInterface();
}
}
}
If you activate the Flag "CopyReferencedAssembliesToWorkspace" for the Test-Assembly than the test will crash because NChrunch has the Assembly A 2 Times:
DirectView:
?System.AppDomain.CurrentDomain.GetAssemblies()
{System.Reflection.RuntimeAssembly[28]}
[0]: {mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[1]: {nCrunch.TaskRunner, Version=1.35.0.16, Culture=neutral, PublicKeyToken=01d101bf6f3e0aea}
[2]: {System.Runtime.Remoting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[3]: {System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[4]: {nCrunch.Common, Version=1.35.0.16, Culture=neutral, PublicKeyToken=01d101bf6f3e0aea}
[5]: {nCrunch.TestExecution, Version=1.35.0.16, Culture=neutral, PublicKeyToken=01d101bf6f3e0aea}
[6]: {System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[7]: {Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}
[8]: {Common.Logging, Version=1.2.0.0, Culture=neutral, PublicKeyToken=af08829b84f0328e}
[9]: {Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a}
[10]: {nunit.framework, Version=2.5.7.10213, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77}
[11]: {Spring.Aop, Version=1.3.1.40711, Culture=neutral, PublicKeyToken=65e474d141e25e07}
[12]: {Spring.Core, Version=1.3.1.40711, Culture=neutral, PublicKeyToken=65e474d141e25e07}
[13]: {System.Data.DataSetExtensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[14]: {System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[15]: {System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[16]: {System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[17]: {ClassLibrary2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null} [18]: {System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a}
[19]: {nunit.core.interfaces, Version=2.5.10.11092, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77}
[20]: {nunit.core, Version=2.5.10.11092, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77}
[21]: {System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a}
[22]: {nunit.framework, Version=2.5.7.10213, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77}
[23]: {nCrunch.TestRuntime, Version=1.35.0.16, Culture=neutral, PublicKeyToken=01d101bf6f3e0aea}
[24]: {nCrunch.Reflection, Version=1.35.0.16, Culture=neutral, PublicKeyToken=01d101bf6f3e0aea}
[25]: {ClassLibrary2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null} [26]: {Spring.Core, Version=1.3.1.40711, Culture=neutral, PublicKeyToken=65e474d141e25e07}
[27]: {Common.Logging, Version=1.2.0.0, Culture=neutral, PublicKeyToken=af08829b84f0328e}
?System.AppDomain.CurrentDomain.GetAssemblies()[17].CodeBase
"file:///C:/Users/mk/AppData/Local/NCrunch/4564/53/ClassLibrary2/bin/Debug/ClassLibrary2.dll"?System.AppDomain.CurrentDomain.GetAssemblies()[25].CodeBase
"file:///C:/Users/mk/AppData/Local/NCrunch/4564/59/Tests/bin/Debug/ClassLibrary2.DLL"
It throws a InvalidCastException because it has the Class from the one loaded Assembly (25) and the Interface from the other loaded Assembly (17):
ExceptionText:
Das Objekt des Typs "ClassLibrary2.ClassInterface" kann nicht in Typ "ClassLibrary2.IInterface" umgewandelt werden.Translated: The object can't cast from the Type "ClassLibrary2.ClassInterface" to the Type "ClassLibrary2.IInterface".
If you deactivate the Flag "CopyReferencedAssembliesToWorkspace" for the Test-Assembly than the test will run perfectly.
But in my case I need the Assemblies in the Bin-Folder for other operations e.g. DynamicCodeCompilation.
So, to deactivate is no solution for me.
I Think there is a problem with the order of dynamic loading of assemblies over Activator or something like this.
If you need the complete VS-Solution than Mail me and I send it to you.
Best regards Heiko