assemblies not resolving while creating instance from AppDomain - c#

var processAssembly = Assembly.LoadFile(baseLocationCCDebugFolder+"\\CC.dll");
var processType = processAssembly.GetType("CC.Executor.CCProcess",true,true);
AppDomainSetup appDSetup = new AppDomainSetup()
{
ApplicationBase = baseLocationCCDebugFolder,
//PrivateBinPath = processAssembly.CodeBase
};
StrongName fullTrustAssembly = processAssembly.Evidence.GetHostEvidence<StrongName>();
AppDomain executionDomain = AppDomain.CreateDomain("ExecutionDomain",null,appDSetup,internetPS,fullTrustAssembly);
CCProcess process =(CCProcess)executionDomain.CreateInstanceAndUnwrap(processAssembly.ManifestModule.FullyQualifiedName, processType.FullName);
im encountering an error on the last line of this code which goes like this.
Could not load file or assembly 'D:\\work\\compilerCom\\CompileCom_Build_4_newArchitecture\\CompilerCom\\CC\\bin\\Debug\\CC.dll' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)<br/>
im still figuring my way in app domain.
what am i doing wrong?
EDIT:-
the value of baseLocationCCDebugFolder variable is D:\\work\\compilerCom\\CompileCom_Build_4_newArchitecture\\CompilerCom\\CC\\bin\\Debug

My guess is that it is not the Assembly not being found but one of the referenced assemblies not loading correctly. You may need to handle the resolve assembly event in the parent appdomain. There is a good post here describing it.
http://social.msdn.microsoft.com/forums/en-US/clr/thread/0a18ed66-6995-4e7c-baab-61c1e528fb82/

Related

Load assembly to temporary Appdomain from another directory

As a part of BaaS project, I need to dynamically execute a code from "guest" assembly. I've tried all the examples and answers in other similar questions including AppDomainToolKit approach, but got no luck.
Similar with the plugin approach, I got positive result by copying related assemblies into host application's bin path. However, this is not possible for the current scenario:
permissions and restrictions needs to be applied per request
each request should be evaluated in a temporary appdomain
load the required assembles and referenced types from a path to temporary domain
So far, my latest piece of code is below
// ClassLibrary1.dll and ClassLibrary2.dll are in the same directory, both are marked as Serializable
var binPath = #"C:\AssemblyDemo\ClassLibrary1\bin\Debug\";
var lib1Path = binPath + "ClassLibrary1.dll";
var lib2Path = binPath + "ClassLibrary2.dll";
var setup = new AppDomainSetup();
setup.ApplicationBase = binPath;
AppDomain domain = AppDomain.CreateDomain("domainname", null, setup);
ObjectHandle handle = domain.CreateInstanceFrom(lib1Path, "ClassLibrary1.Class1");
var unwrap = handle.Unwrap();
var m1 = unwrap.GetType().GetMethod("Method1");
var result = m1.Invoke(unwrap, null);
handle.Unwrap() throws exception Type is not resolved for member 'ClassLibrary1.Class1,ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
If you want to load the assembly only in the temporary AppDomain you cannot call Unwrap in the main domain. Unwrap loads the type there as well. You must move all access to objects from the temporary assembly to the temporary AppDomain. You can do this using AppDomain.DoCallBack or a custom MarshalByRef class that you instantiate in the child domain and call from the parent domain.

How can I dynamically reference an assembly that looks for another assembly?

Apologies for the dodgy question - happy to rephrase if someone has a better suggestion.
I'm trying to create an object by dynamically invoking an assembly belonging to another application.
The following PowerShell code is working nicely for me:
[Reflection.Assembly]::LoadFrom("C:\Program Files\Vendor\Product\ProductAPI.dll")
$bobject = new-object ProductAPI.BasicObject
$bobject.AddName("Some Name")
I'm struggling to do the same thing in C#. Based on other posts on StackOverflow I currently have this:
System.Reflection.Assembly myDllAssembly =
System.Reflection.Assembly.LoadFile("C:\\Program Files\\Vendor\\Product\\ProductAPI.dll");
System.Type BasicObjectType = myDllAssembly.GetType("ProductAPI.BasicObject");
var basicObjectInstance = Activator.CreateInstance(BasicObjectType);
The final line results in a TargetInvocationException.
{"Could not load file or assembly 'AnotherObject, Version=1.2.345.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified."
It appears that the BasicObject constructor is trying to invoke AnotherObject (from AnotherObject.dll in the same folder) but can't find it.
Any tips on how to get around this?
If it can't find a dependent assembly in the usual places, you'll need to manually specify how to find them.
The two easiest ways I'm aware of for doing this:
manually load the dependent assemblies in advance with
Assembly.Load.
handle the AssemblyResolve event for the domain which is loading the
assembly with additional assembly dependencies.
Both essentially require you to know the dependencies for the assembly you're trying to load in advance but I don't think that's such a big ask.
If you go with the first option, it would also be worthwhile looking into the difference between a full Load and a reflection-only Load.
If you would rather go with 2 (which I'd recommend), you can try something like this which has the added benefit of working with nested dependency chains (eg MyLib.dll references LocalStorage.dll references Raven.Client.dll references NewtonSoft.Json.dll) and will additionally give you information about what dependencies it can't find:
AppDomain.CurrentDomain.AssemblyResolve += (sender,args) => {
// Change this to wherever the additional dependencies are located
var dllPath = #"C:\Program Files\Vendor\Product\lib";
var assemblyPath = Path.Combine(dllPath,args.Name.Split(',').First() + ".dll");
if(!File.Exists(assemblyPath))
throw new ReflectionTypeLoadException(new[] {args.GetType()},
new[] {new FileNotFoundException(assemblyPath) });
return Assembly.LoadFrom(assemblyPath);
};

Why assembly is still visible? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Unloading the Assembly loaded with Assembly.LoadFrom()
I use custom AppDomain to load/unload assembly. But when assembly is unloaded I am able to see it under the AppDomain.CurrentDomain.
How it could be? Is this normal behavior or I am missing something?
Thank you for any clue!
string assemblyPath = #"C:\MyFile.dll";
var assemblyName = AssemblyName.GetAssemblyName(assemblyPath);
var ads = new AppDomainSetup
{
ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
DisallowCodeDownload = true
};
AppDomain newDomainName = AppDomain.CreateDomain("newDomainName", null, ads);
try
{
Assembly testLibrary = newDomainName.Load(assemblyName);
var c1 = AppDomain.CurrentDomain.GetAssemblies();
var c2 = newDomainName.GetAssemblies();
}
finally
{
AppDomain.Unload(newDomainName);
var c3 = AppDomain.CurrentDomain.GetAssemblies();
// The assembly is still visible here!!!
}
You are calling the Load() method of an AppDomain, which according to the documentation: "should be used only to load an assembly into the current application domain. This method is provided as a convenience for interoperability callers who cannot call the static Assembly.Load method. To load assemblies into other application domains, use a method such as CreateInstanceAndUnwrap."
In other words, you're loading the assembly into the primary AppDomain because you're calling Load() from the primary AppDomain (even though you're using calling it on an instance of your secondary AppDomain), and this is why it is appearing even after you unload your secondary AppDomain.
As indicated in the extract from the documentation above, you probably want to use AppDomain.CreateInstanceAndUnwrap.
You can't remove a loaded assembly from an app domain.
http://blogs.msdn.com/b/jasonz/archive/2004/05/31/145105.aspx
http://msdn.microsoft.com/en-us/library/ms173101(v=vs.80).aspx
There is no way to unload an individual assembly without unloading all
of the application domains that contain it. Even if the assembly goes
out of scope, the actual assembly file will remain loaded until all
application domains that contain it are unloaded.
http://blogs.msdn.com/b/suzcook/archive/2003/07/08/unloading-an-assembly.aspx
There's no way to unload an individual assembly without unloading all
of the appdomains containing it.

how to load in new appdomain from filepath?

İ am sorry it is too classic problem loading dll. But i can not solve this basic problem: i have a plugins file path i want to load these assembly into new domain but i can not do this. Firstly; it is a web application my code below: thanks your help :
foreach (var filePath in arryFiles)
{
Assembly asm = Assembly.LoadFile(filePath.ToString());
AppDomainSetup domainSetup = new AppDomainSetup();
domainSetup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
domainSetup.DisallowBindingRedirects = false;
domainSetup.DisallowCodeDownload = true;
domainSetup.LoaderOptimization = LoaderOptimization.SingleDomain;
domainSetup.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
ApplicationHost = AppDomain.CreateDomain("Ops.Mgr.Service", null, domainSetup);
// how to load into domain my assembly...
}
Thanks your helps...
Do you want to load assembly in to same App Domain or different App Domain?
Here is the solution to load assembly in different App Domain: Loading DLLs into a separate AppDomain
For loading assembly in same App Domain: Can I load a .NET assembly at runtime and instantiate a type knowing only the name?

Error while loading an assembly into a new AppDomain

I tried the below code to load and unload managed assemblies.
AppDomain dom = AppDomain.CreateDomain("some");
AssemblyName assemblyName = new AssemblyName();
assemblyName.CodeBase = pathToAssembly;
Assembly assembly = dom.Load(assemblyName);
Type [] types = assembly.GetTypes();
AppDomain.Unload(dom);
however, i got an exception "Friendlyname or appdomainbase invalid
", while loading the assembly into the appdomain.
Can anyone help me
As the exception implies - it's just that it can't find the assembly.
You need to need to use the AppDomainSetup type when creating the AppDomain and set the ApplicationBase to the folder where you want it to probe for assemblies when using the Load method. You can also set the PrivateBinPath as well for additional paths.
This other MSDN topic (linked from the previous one) gives an example.
Providing a CodeBase in the AssemblyName will not work.

Categories