Note that there are some explanatory texts on larger screens.

plurals
  1. POc# .NET Loading/Unloading assembly while keeping the same session
    primarykey
    data
    text
    <p>I am rather new to c# and .NET, and trying to create an asp.net web application that dynamically loads assembly. </p> <p>Initially, I've used <code>Activator.CreateInstance</code> to dynamically load assemblies, but it seems to lock the assembly DLL file. Because I am making frequent changes to the assembly it's become quite a pain. I also need to share the assembly with other apps, so it may become a problem later.</p> <p>It appears that most people recommend creating a separate AppDomain and loading the assembly into it, then unload the appdomain after I'm done. However, my app and the assembly also depends on session context and all the session is lost once I send it over to the app domain; the assembly crashes because it cannot find session context.</p> <p>Is there any way to pass on my session context to the AppDomain? Or is there any way for me to load the assembly without locking the DLL file? I've tried streaming the file as some suggested, but it still locks the DLL.</p> <p>Edit: I've tried the following code as Davide Piras suggested, but the DLL file is still locked</p> <pre><code> private static T CreateInstance&lt;T&gt;(string fileName, string typeName) { if (!File.Exists(fileName)) throw new FileNotFoundException(string.Format("Cannot find assembly '{0}'.", fileName)); try { Assembly assembly = Assembly.LoadFrom(fileName); if (assembly != null) { List&lt;Type&gt; assemblyTypes = assembly.GetTypes().ToList(); Type assemblyType = assemblyTypes.FirstOrDefault(asmType =&gt; typeof(T).IsAssignableFrom(asmType)); T instance = (T) Activator.CreateInstance(assemblyType); if (instance != null) return instance; } // Trouble if the above doesn't work! throw new NullReferenceException(string.Format("Could not create type '{0}'.", typeName)); } catch (Exception exp1) { throw new Exception("Cannot create instance from " + fileName + ", with type " + typeName + ": " + exp1.Message + exp1.Source, exp1.InnerException); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload