This my first ever question on stack overflow. I tried to search for a solution but could not find one or similar anywhere.
I have an MVC Website project which has different DLLs (Plugins) and since these DLLs don't need to be deployed in all environments I am loading these dynamically on Application_Start in the Global.asax from a folder and deploy only what I need per environment. So I don't what to reference them in the project so I can have one build for all.
My problem is that Type.GetType("FullClassName, AssemblyName") always returns null on objects which are in these Plugins.
On Application_Start I basically load an assembly register it to GAC, loading it in current AppDomain, Initializing its metadata and running it's bootstrapper with reflections to register components in a global Unity container.
foreach (string file in fileList) {
string fileName = (Path.GetFileName(file));
Assembly asm = Assembly.LoadFrom(file);
foreach (Type assemblyType in asm.GetTypes()) {
new System.EnterpriseServices.Internal.Publish().GacInstall(file);
AppDomain.CurrentDomain.Load(asm.GetName());
MetaDataHelper.InitialiseMetadataForAssembly(asm);
IBootstrapper bootsrapper = (IBootstrapper)Activator.CreateInstance(assemblyType, container);
bootsrapper.Initialise();
}
}
I have also tried this approach by Strong Naming my assemblies and adding them in the web.config with no avail.
<dependentAssembly>
<assemblyIdentity name="MyAssembly" culture="neutral" publicKeyToken="123456789"/>
<codeBase version="1.0.1524.23149" href="FILE://C:/Myassemblies/MyAssembly2.dll"/>
</dependentAssembly>
Can anyone help me please am I missing something?
Thanks in advance.
I figured it out. I cannot believe how simple the solution was. After some probing in the AppDomain.CurrentDomain and some MSDN documentation I just needed to implement and add a custom assembly resolve event handler to the current domain.
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(currentDomain_AssemblyResolve);
static Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
--args.Name is the assembly name passed to the GetType("FullClassName,AssemblyName");
--Search in LoadedAssemblies by name and return it.
}