Using ApplicationHost.CreateApplicationHost () to create an Asp.Net message when loading assemblies from another location

I am trying to use CreateApplicationHost to create an Asp.Net application host for a simple web server, however I am getting a System.IO.FileNotFoundException when it tries to create an instance of my marshalling object:

 host = (MyMarshaller)ApplicationHost.CreateApplicationHost((MyMarshaller), virtualDir, physicalDir); 

I believe that this error is due to the fact that it cannot find the assembly containing the MyMarshaller class - if I put this assembly in the binFilDir directory, then everything will be fine, but I do not want to do this (I also do not know, I want post your assembly to the GAC).

Is there a way I can control where CreateApplicationHost is looking for my assembly? (Note that the MyMarshaller class is in the same assembly as the above line of code)

+4
source share
2 answers

The short answer is that this cannot be done, however, the Cassini source showed an alternative solution.


Update

The above link no longer works, however, the CassiniDev project on CodePlex is quite close (possibly derived) and contains the same solution.

The solution is to use CreateApplicationHost and "do it yourself" - in this case, use reflection to create an instance of the internal type "BuildManagerHost" and call "RegisterAssembly", for example:

 /// <remarks> /// This is Dmitry hack to enable running outside of GAC. /// There are some errors being thrown when running in proc /// </remarks> private object CreateWorkerAppDomainWithHost(string virtualPath, string physicalPath, Type hostType,int port) { // create BuildManagerHost in the worker app domain //ApplicationManager appManager = ApplicationManager.GetApplicationManager(); Type buildManagerHostType = typeof(HttpRuntime).Assembly.GetType("System.Web.Compilation.BuildManagerHost"); IRegisteredObject buildManagerHost = ApplicationManager.CreateObject(_appId, buildManagerHostType, virtualPath, physicalPath, false); // call BuildManagerHost.RegisterAssembly to make Host type loadable in the worker app domain buildManagerHostType.InvokeMember("RegisterAssembly", BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.NonPublic, null, buildManagerHost, new object[] { hostType.Assembly.FullName, hostType.Assembly.Location }); // create Host in the worker app domain // FIXME: getting FileLoadException Could not load file or assembly 'WebDev.WebServer20, Version=4.0.1.6, Culture=neutral, PublicKeyToken=f7f6e0b4240c7c27' or one of its dependencies. Failed to grant permission to execute. (Exception from HRESULT: 0x80131418) // when running dnoa 3.4 samples - webdev is registering trust somewhere that we are not return ApplicationManager.CreateObject(_appId, hostType, virtualPath, physicalPath, false); } 
+4
source

How to load an assembly at runtime that is in a folder that is not the application bin folder

If it is not in the GAC or BIN, you will have to use Assembly.LoadFrom .

 Assembly SampleAssembly; SampleAssembly = Assembly.LoadFrom("c:\\Sample.Assembly.dll"); 
0
source

Source: https://habr.com/ru/post/1315514/


All Articles