I've got an assembly of domain objects. It WAS called company.xpo.domain (it was named that by the contractor that initially named the project, and they are longer with us - we own the software). I did a global rename of the namespace to xpo.domain and I changed the default namespace in the designer to xpo.domain.
I also did a find in visual studio on the entire solution for "company" and found nothing.
My problem goes like this:
in the assembly there is a user object that has a static method "Login" which takes a username and a password, checks the db for the name/password and returns null if nothing was found.
IN THAT STATIC METHOD, it throws a FileNotFoundException for the company.xpo.domain assembly. This is confusing to me because, the code that's throwing the exception, exists in the assembly that it's looking for (but it's looking for the previous name of the assembly).
EDIT
Here is the stack trace. I've just done a rename on the name of the software and the name of the assembly for the purpose of security.
System.IO.FileNotFoundException occurred
Message="Could not load file or assembly 'company.XPO.domain' or one of its dependencies. The system cannot find the file specified."
Source="mscorlib"
FileName="company.XPO.domain"
FusionLog="=== Pre-bind state information ===
LOG: User = STOONP001\\Administrator
LOG: DisplayName = company.XPO.domain\n (Partial)
LOG: Appbase = file:///C:/Documents and Settings/Administrator/Desktop/softwareName/softwareName/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : DevExpress.Data.v9.2, Version=9.2.5.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\\Documents and Settings\\Administrator\\Desktop\\softwareName\\softwareName\\bin\\Debug\\softwareName.vshost.exe.config
LOG: Using machine configuration file from c:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\config\\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Documents and Settings/Administrator/Desktop/softwareName/softwareName/bin/Debug/company.XPO.domain.DLL.
LOG: Attempting download of new URL file:///C:/Documents and Settings/Administrator/Desktop/softwareName/softwareName/bin/Debug/company.XPO.domain/company.XPO.domain.DLL.
LOG: Attempting download of new URL file:///C:/Documents and Settings/Administrator/Desktop/softwareName/softwareName/bin/Debug/company.XPO.domain.EXE.
LOG: Attempting download of new URL file:///C:/Documents and Settings/Administrator/Desktop/softwareName/softwareName/bin/Debug/company.XPO.domain/company.XPO.domain.EXE."
StackTrace:
at System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
InnerException
EDIT 2
The call stack, if it helps, is here. It seems to appear that devexpress.data.utils is throwing the error, but I have no control over that class (if it is in fact a problem with that assembly).
mscorlib.dll!System.Reflection.Assembly.nLoad(System.Reflection.AssemblyName fileName, string codeBase, System.Security.Policy.Evidence assemblySecurity, System.Reflection.Assembly locationHint, ref System.Threading.StackCrawlMark stackMark, bool throwOnFileNotFound, bool forIntrospection) + 0x2c bytes
mscorlib.dll!System.Reflection.Assembly.LoadWithPartialNameInternal(string partialName, System.Security.Policy.Evidence securityEvidence = null, ref System.Threading.StackCrawlMark stackMark = LookForMyCaller) + 0x6b bytes
mscorlib.dll!System.Reflection.Assembly.LoadWithPartialName(string partialName) + 0x1b bytes
DevExpress.Data.v9.2.dll!DevExpress.Data.Utils.Helpers.LoadWithPartialName(string partialName = "company.XPO.domain") + 0x1f bytes
DevExpress.Data.v9.2.dll!DevExpress.Xpo.Helpers.XPTypeActivator.GetType(string assemblyName = "company.XPO.domain", string typeName = "company.XPO.domain.customer") + 0xd0 bytes
DevExpress.Xpo.v9.2.dll!DevExpress.Xpo.Metadata.ReflectionDictionary.ResolveClassInfoByName(string assemblyName = "company.XPO.domain", string typeName = "company.XPO.domain.customer") + 0x50 bytes
DevExpress.Xpo.v9.2.dll!DevExpress.Xpo.Metadata.ReflectionDictionary.QueryClassInfo(string assemblyName = "company.XPO.domain", string className = "company.XPO.domain.customer") + 0xde bytes
DevExpress.Xpo.v9.2.dll!DevExpress.Xpo.XPObjectType.TypeClassInfo.get() + 0x55 bytes
DevExpress.Xpo.v9.2.dll!DevExpress.Xpo.XPObjectType.IsValidType.get() + 0x1f bytes
DevExpress.Xpo.v9.2.dll!DevExpress.Xpo.Helpers.XPObjectTypesManager.FillLoadedTypes(System.Collections.ICollection objectTypesList = Count = 23) + 0xfa bytes
DevExpress.Xpo.v9.2.dll!DevExpress.Xpo.Helpers.XPObjectTypesManager.GetAllTypes() + 0x278 bytes
DevExpress.Xpo.v9.2.dll!DevExpress.Xpo.Helpers.XPObjectTypesManager.EnsureIsTypedObjectValid() + 0x1e bytes
DevExpress.Xpo.v9.2.dll!DevExpress.Xpo.Helpers.ObjectCollectionLoader.InternalLoadData(DevExpress.Xpo.ObjectsQuery[] queries = {DevExpress.Xpo.ObjectsQuery[1]}) + 0x5a bytes
DevExpress.Xpo.v9.2.dll!DevExpress.Xpo.Helpers.ObjectCollectionLoader.LoadObjects(DevExpress.Xpo.ObjectsQuery[] queries = {DevExpress.Xpo.ObjectsQuery[1]}) + 0xda bytes
DevExpress.Xpo.v9.2.dll!DevExpress.Xpo.Session.GetObjects(DevExpress.Xpo.ObjectsQuery[] queries = {DevExpress.Xpo.ObjectsQuery[1]}) + 0x58 bytes
DevExpress.Xpo.v9.2.dll!DevExpress.Xpo.Session.GetObjects(DevExpress.Xpo.ObjectsQuery query = {DevExpress.Xpo.ObjectsQuery}) + 0x55 bytes
DevExpress.Xpo.v9.2.dll!DevExpress.Xpo.Session.GetObjects(DevExpress.Xpo.Metadata.XPClassInfo classInfo = {XPO.domain.sys_user}, DevExpress.Data.Filtering.CriteriaOperator criteria = {[Username] = 'username' And [Password1] = 'password'}, DevExpress.Xpo.SortingCollection sorting = null, int topSelectedRecords = 1, bool selectDeleted = false, bool force = false) + 0x9b bytes
DevExpress.Xpo.v9.2.dll!DevExpress.Xpo.Session.InternalFindAndLoad(DevExpress.Xpo.Metadata.XPClassInfo baseType = {XPO.domain.sys_user}, DevExpress.Data.Filtering.CriteriaOperator criteria = {[Username] = 'username' And [Password1] = 'password'}, bool selectDeleted = false) + 0x38 bytes
DevExpress.Xpo.v9.2.dll!DevExpress.Xpo.Session.FindObject(DevExpress.Xpo.Metadata.XPClassInfo classInfo = {XPO.domain.sys_user}, DevExpress.Data.Filtering.CriteriaOperator criteria = {[Username] = 'username' And [Password1] = 'password'}, bool selectDeleted = false) + 0x2d bytes
DevExpress.Xpo.v9.2.dll!DevExpress.Xpo.Session.FindObject(System.Type classType = {Name = "sys_user" FullName = "XPO.domain.sys_user"}, DevExpress.Data.Filtering.CriteriaOperator criteria = {[Username] = 'username' And [Password1] = 'password'}, bool selectDeleted = false) + 0x52 bytes
DevExpress.Xpo.v9.2.dll!DevExpress.Xpo.Session.FindObject<XPO.domain.sys_user>(DevExpress.Data.Filtering.CriteriaOperator criteria = {[Username] = 'username' And [Password1] = 'password'}) + 0x57 bytes
Note: I have created a case with DevExpress as well, just in case. I'm totally stumped.
I would definitely need to see a stack trace and/or code to really know for sure, but it sounds to me like you might have some kind of circular reference in your assemblies and one of them is referencing your old assembly. But that's completely just a stab in the dark.
If you can and if applicable, I'd check the code associated with any Typed DataSets you might have; I've had had problems renaming namespaces within Typed DataSet classes. I've had to go in and had to do 'search and replaces' manually.
Again, this may not be revelant for you.
Edit--
Another possibility:
Any chance there's something in your GAC that references the old assembly by name?
Related
I tied to create simple program Console App C# .NET 4.0 to load DLL file remotely.
My code works as expected on load dll that on the local computer but I got problem resolve dll when trying to load remotely.
I have no idea to handle this since I can't find any code example that load dll remotely.
The error I got as following.
Could not load file or assembly 'http://codesanook.cloudapp.net/dll/ZupZip.Lib.I
nterfaces.dll' or one of its dependencies. Operation is not supported. (Exceptio
n from HRESULT: 0x80131515)
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String cod
eBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark&
stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntro
spection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String code
Base, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& s
tackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntros
pection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName as
semblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMar
k& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIn
trospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Ev
idence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm,
Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackM
ark)
at System.Reflection.Assembly.LoadFrom(String assemblyFile)
at LoadDllRemotely.Proxy.GetObject[T](String assemblyPath, String fullTypeNam
e, String[] referencedAssembliesPath) in C:\Projects\LoadDllRemotely\LoadDllRemo
tely\Program.cs:line 102
Unhandled Exception: System.NullReferenceException: Object reference not set to
an instance of an object.
at LoadDllRemotely.Program.LoadRemotely() in C:\Projects\LoadDllRemotely\Load
DllRemotely\Program.cs:line 74
at LoadDllRemotely.Program.Main(String[] args) in C:\Projects\LoadDllRemotely
\LoadDllRemotely\Program.cs:line 28
Press any key to continue . . .
This is my implementation that load dll remotely with new AppDomain
using System;
using System.IO;
using System.Reflection;
using ZupZip.Lib.Interfaces;
namespace LoadDllRemotely
{
public class Program
{
private const string ASSEMBLY_PATH =
#"C:\Projects\LoadDllRemotely\ZupZip.Lib\bin\Debug\ZupZip.Lib.dll";
private const string ASSEMBLY_URL =
#"http://codesanook.cloudapp.net/dll/ZupZip.Lib.dll";
private const string REFERENCED_ASSEMBLY3_URL =
#"http://codesanook.cloudapp.net/dll/ZupZip.Lib.Interfaces.dll";
private const string REFERENCED_ASSEMBLY2_URL =
#"http://codesanook.cloudapp.net/dll/system.core.dll";
private const string REFERENCED_ASSEMBLY1_URL =
#"http://codesanook.cloudapp.net/dll/mscorlib.dll";
public static void Main(string[] args)
{
//LoadLocally();
LoadRemotely();
}
public static void appDomain_DomainUnload(object sender, EventArgs e)
{
Console.WriteLine("domain loaded sender: {0}",
sender.GetType().FullName);
}
public static void LoadLocally()
{
var appDomain = AppDomain.CreateDomain("dynamicDll");
appDomain.DomainUnload += new EventHandler(appDomain_DomainUnload);
var proxy = (Proxy)appDomain.CreateInstanceAndUnwrap(
typeof(Proxy).Assembly.FullName,
typeof(Proxy).FullName);
var calculator = proxy.GetObject<IGradeCalculator>(
ASSEMBLY_PATH,
"ZupZip.Lib.GradeCalculator");
var score = 80;
Console.WriteLine("you got grad: {0} from score: {1}", calculator.GetGradeForScore(score), score);
AppDomain.Unload(appDomain);
File.Delete(ASSEMBLY_PATH);//you can delete referece dll after remove
}
public static void LoadRemotely()
{
//app domain setup
//load as byte array
var appDomain = AppDomain.CreateDomain("dynamicDll");
appDomain.DomainUnload += new EventHandler(appDomain_DomainUnload);
var proxy = (Proxy)appDomain.CreateInstanceAndUnwrap(
typeof(Proxy).Assembly.FullName,
typeof(Proxy).FullName);
var calculator = proxy.GetObject<IGradeCalculator>(
ASSEMBLY_URL,
"ZupZip.Lib.GradeCalculator",
REFERENCED_ASSEMBLY3_URL);
var score = 80;
Console.WriteLine("you got grad: {0} from score: {1}", calculator.GetGradeForScore(score), score);
AppDomain.Unload(appDomain);
}
}
public class Proxy : MarshalByRefObject
{
public T GetObject<T>(
string assemblyPath,
string fullTypeName,
params string[] referencedAssembliesPath) where T : class
{
try
{
//find reference
var assemblyToLoad = Assembly.ReflectionOnlyLoadFrom(assemblyPath);
AssemblyName[] referencedAssemblies = assemblyToLoad.GetReferencedAssemblies();
//foreach (var referencedAssembly in referencedAssemblies)
//{
// Console.WriteLine("referenceAssemblyName: {0}", referencedAssembly.Name);
// Assembly.Load(referencedAssembly.Name);
//}
//solve reference assembly
foreach (var referencedAssemblyPath in referencedAssembliesPath)
{
Assembly.LoadFrom(referencedAssemblyPath);
}
//this dll will load in new domain
var assembly = Assembly.LoadFrom(assemblyPath);
var type = assembly.GetType(fullTypeName);
var obj = (T)Activator.CreateInstance(type);
return obj;
}
catch (Exception ex)
{
// throw new InvalidOperationException(ex);
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
return null;
}
}
}
}
I also add this source code to my open source on Bitbuckget
enter link description here
Fixed
just add this configuration to App.Config to run in full trust
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<loadFromRemoteSources enabled="true"/>
</runtime>
</configuration>
and I also update this code at
https://bitbucket.org/theeranitp/loaddllremotely
I do web. service that load assembly file by input parameter.
Then in assembly will try to find a specific type (inherited from specific interface), create an instance and returns the result of the method.
I need to make the call to the assembly was again released.
From the input parameters of the method I find the path to the the assembly in web.config. and try to load it.
This is the code that works:
[WebMethod]
public String[] GetData(String confKey)
{
var assemblyPath = ConfigurationManager.AppSettings[confKey];
var assembly = Assembly.LoadFrom(assemblyPath);
List<String> retVals = new List<String>();
foreach (var t in assembly.GetTypes())
{
if (t.ImplementsInterface(typeof(IMyServiceProvider)))
{
IMyServiceProvider objectInstance = Activator.CreateInstance(t) as IMyServiceProvider;
retVals.Add(objectInstance.GetData());
}
}
return retVals.ToArray();
}
But this way I can delete the loaded assembly or replace it because the file is "locked".
So I tried to go a different way and load the assembly into own AppDomain like this:
[WebMethod]
public String[] GetData(String confKey)
{
var assemblyPath = ConfigurationManager.AppSettings[confKey];
var tmp = String.Concat("AppDomain", Guid.NewGuid().ToString("N"));
AppDomain dom = AppDomain.CreateDomain(tmp, null, AppDomain.CurrentDomain.SetupInformation);
AssemblyName assemblyName = new AssemblyName();
assemblyName.CodeBase = assemblyPath;
Assembly assembly = dom.Load(assemblyPath);
List<String> retVals = new List<String>();
foreach (var t in assembly.GetTypes())
{
if (t.ImplementsInterface(typeof(IMyServiceProvider)))
{
IMyServiceProvider objectInstance = Activator.CreateInstance(t) as IMyServiceProvider;
retVals.Add(objectInstance.GetData());
}
}
AppDomain.Unload(dom);
return retVals.ToArray();
}
But this solution is thrown Exception:
Could not load file or assembly 'NameOfMyAssembly' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)- at System.Reflection.AssemblyName.nInit(RuntimeAssembly& assembly, Boolean forIntrospection, Boolean raiseResolveEvent)
at System.Reflection.RuntimeAssembly.CreateAssemblyName(String assemblyString, Boolean forIntrospection, RuntimeAssembly& assemblyFromResolveEvent)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
at System.AppDomain.Load(String assemblyString)
at System.AppDomain.Load(String assemblyString)
Why the first solution assembly loads without problem, and the second throws an error ?
Thanks
dom.Load(string) method tries to load the assembly by its full name which consists of assembly name, version, culture info and public key. CLR search the assemby in GAC and application's directory. Also you could set additional searching paths with .config file (app.config or ) and probing element. In your case as I understand assemblyPath is a path to the assembly you whant to load and it isn't what need to pass as parameter. I used next code to load assemblies in created domain:
var domain = AppDomain.CreateDomain("Plugin domain"); // Domain for plugins
domain.Load(typeof(IPlugin).Assembly.FullName); // Load assembly containing plugin interface to domain
// ...
var asm = Assembly.LoadFrom(pluginFile);
foreach (var exportedType in asm.GetExportedTypes())
{
if (!typeof (IPlugin).IsAssignableFrom(exportedType))
continue; // Check if exportedType implement IPlugin interface
domain.Load(asm.FullName); // If so load this dll into domain
// ...
}
As all plugins lies in "\Plugins" directory relatively application directory I also added .config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="Plugins"/>
</assemblyBinding>
</runtime>
</configuration>
Pay attantion that if you whant to use dom.Load(AssemblyName) with specified only CodeBase property you must set it in Uri format (i.e. "file:///D:/Dir/...") and I think these assemblies have to be strongly named if they are not in the current application’s folder.
Also as I see you are usig Activator.CreateInstance() to create objects. In this case although you load assembly in created domain you create objects in current domain and all code of this objects be executed in current domain too. To create and execute code in a separate domain you should use appDomain.CreateInstanceAndUnwrap() method (link)
I have a solution with several projects, a main project, a globalization project and a test project.
When code in the main project retreives a message from the Messages.de.resx file of the globalization project everything works fine.
But when I copy the same code to the test project, I get a MissingManifestResourceException telling me no resources were found for the specified or neutral culture:
System.Resources.MissingManifestResourceException ist aufgetreten.
Message=Für die angegebene Kultur oder die neutrale Kultur konnten
keine Ressourcen gefunden werden. Stellen Sie sicher, dass
EGR_IQone_Globalization.Messages.resources beim Kompilieren richtig in
die Assembly EGR_IQone_Globalization eingebettet wurde, oder dass die
erforderlichen Satellitenassemblys geladen werden können und
vollständig signiert sind. Source=mscorlib StackTrace:
bei System.Resources.ManifestBasedResourceGroveler.HandleResourceStreamMissing(String
fileName)
bei System.Resources.ManifestBasedResourceGroveler.GrovelForResourceSet(CultureInfo
culture, Dictionary`2 localResourceSets, Boolean tryParents, Boolean
createIfNotExists, StackCrawlMark& stackMark)
bei System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo
requestedCulture, Boolean createIfNotExists, Boolean tryParents,
StackCrawlMark& stackMark)
bei System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo
culture, Boolean createIfNotExists, Boolean tryParents)
bei System.Resources.ResourceManager.GetString(String name, CultureInfo culture)
bei System.Resources.ResourceManager.GetString(String name)
bei EGR_IQone_Globalization.UserMessage.GetMessage(String msgID, String[] arguments) in
C:\projects\EGR_IQoneH\EGR_IQone\EGR_IQone_Globalization\UserMessage.cs:Zeile
28. InnerException:
Normally, the code works just using the .resx file and even using resgen to compile it into a .resources file changes nothing.
I thought it might have to do with the ResourceManager or the specified Assembly, but I could not see any difference between the call from the main project and the call from the test project.
This is the code:
public static class UserMessage
{
private static ResourceManager _resourceManager;
static UserMessage()
{
string baseName = Assembly.GetAssembly(typeof(UserMessage)).GetName().Name + ".Messages.de";
Console.WriteLine(baseName);
_resourceManager = new ResourceManager(baseName, Properties.GlobalizationAssembly);
}
public static string GetMessage(string msgID, params string[] arguments)
{
string msg = "";
string error = "[Message Error] cannot read Message " + msgID;
try
{
//DefaultLanguage = 'de'
//using the GetString overload with or without CultureInfo paramter makes no difference
msg = _resourceManager.GetString(msgID, new CultureInfo(Properties.DefaultLanguage));
for (int i = 0; i < arguments.Length; i++)
{
msg = msg.Replace("{" + i.ToString() + "}", arguments[i]);
}
}
catch (Exception ex)
{
Console.WriteLine(error + "\r\n" + ex.ToString());
return error;
}
return msg;
}
}
http://pastebin.com/L0YNxyfK
Thanks!
I've had the same error - it suddenly occurred even though the application had been running for a while.
It helped to set the Thread.CurrentThread.CurrentUICulture before getting the resource.
Try the following or something similar:
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("de-DE");
msg = _resourceManager.GetString(msgID);
I am trying to load a strongly named assembly that is located at a user-defined location into an AppDomain using this code:
AppDomainSetup ads1 = new AppDomainSetup();
if (ads1.PrivateBinPath != null)
{
ads1.PrivateBinPath += ads1.PrivateBinPath.Length == 0 ? "" : Path.PathSeparator.ToString();
}
ads1.PrivateBinPath += #"c:\work\abc1";
sandbox1 = AppDomain.CreateDomain("sandbox1", null, ads1);
Object o1 = sandbox1.CreateInstanceFromAndUnwrap(#"abc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f10808f3df0adc34", "Abc.Def");
When I run I get this error:
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'file:///C:\Users\gel\Documents\Visual Studio 2012\Projects\ConsoleApplicat
ion1\ConsoleApplication1\bin\Debug\abc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f10808f3df0adc34' or one of its dependencies. The system cannot find the fil
e specified.
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stack
Mark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& st
ackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& st
ackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boole
an forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
at System.Reflection.Assembly.LoadFrom(String assemblyFile, Evidence securityEvidence)
at System.Activator.CreateInstanceFromInternal(String assemblyFile, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, C
ultureInfo culture, Object[] activationAttributes, Evidence securityInfo)
at System.AppDomain.CreateInstanceFrom(String assemblyFile, String typeName)
at System.AppDomain.CreateInstanceFromAndUnwrap(String assemblyName, String typeName)
at System.AppDomain.CreateInstanceFromAndUnwrap(String assemblyName, String typeName)
at ConsoleApplication1.Program.Main(String[] args) in c:\Users\gel\Documents\Visual Studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:li
ne 69
It is as though the AppDomain is not looking in the PrivateBinPath at all. What am I not doing right? I've double checked in an Explorer window and I can see the assembly at the location that I specified in the PrivateBinPath. It works fine if I just do Assembly.LoadFile and specify the right path, but that'd defeating the purpose of trying to load it in another AppDomain.
I've modified your code and this works just great. See the changes you needs to be done.
AppDomainSetup ads1 = new AppDomainSetup();
if (ads1.PrivateBinPath != null)
{
ads1.PrivateBinPath += ads1.PrivateBinPath.Length == 0 ? "" : Path.PathSeparator.ToString();
}
ads1.PrivateBinPath += #"D:\ConsoleApplication1\ClassLibrary1\bin\debug";
AppDomain sandbox1 = AppDomain.CreateDomain("sandbox1", null, ads1);
Object o1 = sandbox1.CreateInstanceFromAndUnwrap(#"ClassLibrary1.dll", "ClassLibrary1.Class1");
I belive it fails to load that assembly into your current AppDomain, not to the new one (where it likley loaded ok).
The behavior sometimes called "leaking types to aother domain" and casued by attempt to use the created object directly. To work around it one need to create class completely in the other AppDomain (i.e. by running some custom code that deal with creation) and than exposing only interface/classes known to both domain. Check out Unloadable plugins for details.
Short portion of code adopted from the article - create RemoteLoader class in other AppDomain with CreateInstanceFromAndUnwrap and use that object to load/manage other types in the new AppDomain.
public class RemoteLoader : MarshalByRefObject
{
public void Load(string assemblyName, string typeName)
{
object instance = Activator.CreateInstance(assemblyName, typeName);
//Do something with instance, or return shared interface of it.
}
}
Side note: unless you doing it for education purposes consider using existing plugin frameworks like MEF.
I'm trying to create an assembly dynamically in .Net. I can't seem to figure out how to get the CodeBase property to return a value, however. Here's an example:
var assemblyName = new AssemblyName
{
Name = "Whatever",
CodeBase = Directory.GetCurrentDirectory()
};
var assemblyBuilder = AppDomain.CurrentDomain
.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
var moduleBuilder = assemblyBuilder.DefineDynamicModule("WhateverModule", "Whatever.dll");
var typeBuilder = moduleBuilder.DefineType("WhateverType", TypeAttributes.Public);
var type = typeBuilder.CreateType();
assemblyBuilder.Save("Whatever.dll");
var codeBase = type.Assembly.CodeBase; // throws the below exception
System.NotSupportedException was unhandled
Message=The invoked member is not supported in a dynamic assembly.
Source=mscorlib
StackTrace:
at System.Reflection.Emit.InternalAssemblyBuilder.get_CodeBase()
at Stupid.Program.Main(String[] args) in C:\Users\Walking Disaster\Documents\Visual Studio 10\Projects\Lingual.Proxy\Stupid\Program.cs:line 25
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Can anyone see what I'm doing wrong?
Explanation: I'm trying to extend NUnit with an addin that generates test methods at runtime. It does this by taking an array of Action delegates. Unfortunately, Reflection is baked pretty deep into the framework, so I have had to emit classes with a method for each Action delegate. This works fine when I'm using the NUnitTestMethod classes only, but when I use the NUnitTestFixture, it fails because it tries to read the CodeBase from the assembly. I didn't want to create a physical assembly, but this project has been one compromise after another.
I think the exception cause is that CodeBase is meaningless on dynamic assembly. From MSDN:
The location of the assembly as specified originally
If you'll reload the assembly it won't throw the exception:
var assemblyName = new AssemblyName
{
Name = "Whatever",
CodeBase = Directory.GetCurrentDirectory()
};
var assemblyBuilder = AppDomain.CurrentDomain
.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
var moduleBuilder = assemblyBuilder.DefineDynamicModule(
"WhateverModule", "Whatever.dll");
var typeBuilder =
moduleBuilder.DefineType("WhateverType", TypeAttributes.Public);
var type = typeBuilder.CreateType();
assemblyBuilder.Save("Whatever.dll");
var assembly = Assembly.LoadFrom("Whatever.dll");
var codeBase = assembly.CodeBase; // this won't throw exception
Is the Assembly's Location valid? Could you fork NUnit and use that instead given your reference to being damaged :P
I've spent some hours being subjected to NUnit's 'extensibility' story and can't imagine ever choosing it over xUnit.net - but I guess the decider of that will be how many tests you have...