jeudi 30 juillet 2015

ASP.NET: CreateDomain(...).CreateInstanceAndUnwrap(...) OR Assembly.LoadFrom( ... ).GetExportedTypes() throws FileNotFoundException

Intro

I am trying to load some assemblies and get the types out of them... Paths of the assemblies are

bin/Plugin1

I am also trying to create an app domain for them and load them there. Basic steps are:

1. Create a domain:

domain = AppDomain.CreateDomain( "Plugins" )

2. Create a worker

worker = (Scan)domain.CreateInstanceAndUnwrap(
                           Assembly.GetExecutingAssembly().FullName, typeof(Scan ).FullName );

3. Load assemblies

worker.Scan( [PATH_TO_DLL] )

4. Load Types

 Assembly.LoadFrom( [PATH_TO_DLL] ).GetExportedTypes()

I tried anything i could think of, but i always get a FileNotFoundException somewhere in steps 2 or 4..

What I tried

1. ApplicationBase path:

AppDomain.CurrentDomain.SetData( "APPBASE", "C:\\...\\Project\\bin\\Plugin1" );
AppDomain.CreateDomain("Plugins").CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, ... );

Throws FileNotFoundException

2. new domain with AppDomainSetup

AppDomain.CurrentDomain.SetData( "APPBASE", "C:\\...\\Project\\bin\\Plugin1" );
AppDomain.CreateDomain( "Plugins", null, AppDomain.CurrentDomain.SetupInformation).CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, ... );

Throws FileNotFoundException

3. Used CreateInstanceFromAndUnwrap

var assembly = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, "Plugin1", Path.GetFileName( Assembly.GetExecutingAssembly().Location ) );
AppDomain....CreateInstanceFromAndUnwrap(assembly, ... );

Worked but...

4. Used Assembly.LoadFrom

var asm = Assembly.LoadFrom( [PATH_TO_ASSEMBLY] );//works
var types = asm.GetExportedTypes();

Throws FileNotFoundException

5. Web.Config probing

<probing privatePath="bin;bin/Plugin1;" />

Didn't helped anywhere...

That led me to believe that forcing to load an assembly from a path while not fixing the actual problem (assemblies seems to be loaded to iis paths) is not fixing anything, i am not on the right path towards solution...

Exceptions I got

1. Exception From CreateInstance

System.IO.FileNotFoundException was unhandled by user code
  HResult=-2147024894
  Message=Could not load file or assembly 'Plugin.Libs.Base, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
  Source=mscorlib
  FileName=Plugin.Libs.Base, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
  FusionLog== == Pre-bind state information == =
LOG: DisplayName = Plugin.Libs.Base, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///C:/.../Web/PluginWeb.heTest/PluginWeb.heTest/PluginWeb.heTest/bin/Plugin/
LOG: Initial PrivatePath = C:\...\Web\PluginWeb.heTest\PluginWeb.heTest\PluginWeb.heTest\bin
Calling assembly : (Unknown).
== =
LOG: This bind starts in default load context.
LOG: Configuration file C:\...\Web\PluginWeb.heTest\PluginWeb.heTest\PluginWeb.heTest\bin\Plugin\web.config does not exist.
LOG: No application configuration file found.
LOG: Using host configuration file: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\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:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/root/993f86a2/2dca2916/Plugin.Libs.Base.DLL.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/root/993f86a2/2dca2916/http://ift.tt/1IaPDNw.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/root/993f86a2/2dca2916/Plugin.Libs.Base.EXE.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/root/993f86a2/2dca2916/http://ift.tt/1DcTi0c.
StackTrace:
       at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
       at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
       at System.Activator.CreateInstance(String assemblyString, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo, StackCrawlMark& stackMark)
       at System.Activator.CreateInstance(String assemblyName, String typeName)
       at System.AppDomain.CreateInstance(String assemblyName, String typeName)
       at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName)
       at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName)
       at Plugin.Libs.Base.Schema.mySchemaManager.<>c__DisplayClass6.<Scan>b__3() in c:\...\Plugin.Libs.Base\Plugin.Libs.Base\Schema\Manager.cs:line 202
       at Plugin.Libs.Base.mySys.WriteLog(poSysLogScale scale, Action method, String message, Object[] args) in c:\...\Plugin.Libs.Base\Plugin.Libs.Base\Sys.cs:line 514
       at Plugin.Libs.Base.Schema.mySchemaManager.Scan(Action'1 processView) in c:\...\Plugin.Libs.Base\Plugin.Libs.Base\Schema\Manager.cs:line 184
       at Plugin.Libs.Base.Schema.mySchemaManager.<>c__DisplayClass1.<Load>b__0() in c:\...\Plugin.Libs.Base\Plugin.Libs.Base\Schema\Manager.cs:line 176
       at Plugin.Libs.Base.mySys.WriteLog(poSysLogScale scale, Action method, String message, Object[] args) in c:\...\Plugin.Libs.Base\Plugin.Libs.Base\Sys.cs:line 514
       at Plugin.Libs.Base.Schema.mySchemaManager.Load(Action'1 processView) in c:\...\Plugin.Libs.Base\Plugin.Libs.Base\Schema\Manager.cs:line 175
       at Plugin.Libs.Base.myApplication.Load(Action'1 processView) in c:\...\Plugin.Libs.Base\Plugin.Libs.Base\Application.cs:line 141
       at Plugin.Web.Forms.Fakes.FakeAppContext.Get(String aliasFileName, String userName, String password) in c:\...\PluginWeb.heTest\PluginWeb.heTest\Plugin.Web.Forms\Fakes\AppContext.cs:line 25
       at Plugin.Web.Forms.Controllers.BaseController..ctor() in c:\...\PluginWeb.heTest\PluginWeb.heTest\Plugin.Web.Forms\Controllers\BaseController.cs:line 30
       at PluginWeb.heTest.Classes.BaseController..ctor()
       at PluginWeb.heTest.Controllers.HomeController..ctor()
  InnerException:

2. Exception from .GetExportedTypes()

System.IO.FileNotFoundException was caught
  HResult=-2147024894
  Message=Could not load file or assembly 'PluginChild.Libs.Base, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
  Source=mscorlib
  FileName=PluginChild.Libs.Base, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
  FusionLog== == Pre-bind state information == =
LOG: DisplayName = PluginChild.Libs.Base, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///c:/windows/system32/inetsrv/
LOG: Initial PrivatePath = NULL
Calling assembly : PluginChild.Obj.MGM.DataObjects, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
== =
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: No application configuration file found.
LOG: Using host configuration file: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\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:/windows/system32/inetsrv/PluginChild.Libs.Base.DLL.
LOG: Attempting download of new URL file:///c:/windows/system32/inetsrv/PluginChild.Libs.Base/PluginChild.Libs.Base.DLL.
LOG: Attempting download of new URL file:///c:/windows/system32/inetsrv/PluginChild.Libs.Base.EXE.
LOG: Attempting download of new URL file:///c:/windows/system32/inetsrv/PluginChild.Libs.Base/PluginChild.Libs.Base.EXE.
LOG: Attempting download of new URL file:///C:/.../PluginWeb.heTest/PluginWeb.heTest/bin/Apps/Obj/PluginChild.Libs.Base.DLL.
LOG: Attempting download of new URL file:///C:/.../PluginWeb.heTest/PluginWeb.heTest/bin/Apps/Obj/PluginChild.Libs.Base/PluginChild.Libs.Base.DLL.
LOG: Attempting download of new URL file:///C:/.../PluginWeb.heTest/PluginWeb.heTest/bin/Apps/Obj/PluginChild.Libs.Base.EXE.
LOG: Attempting download of new URL file:///C:/.../PluginWeb.heTest/PluginWeb.heTest/bin/Apps/Obj/PluginChild.Libs.Base/PluginChild.Libs.Base.EXE.

  StackTrace:
       at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)
       at System.Reflection.RuntimeAssembly.GetExportedTypes()
       at Plugin.Libs.Base.Schema.SchemaManager.MarsalScan.ScanAssembly(ScanResult res, String item) in c:\...\Plugin.Libs.Base\Plugin.Libs.Base\Schema\Manager.cs:line 81
  InnerException: 

Any help will be really appreciated!





Aucun commentaire:

Enregistrer un commentaire