Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6557405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:04:10+00:00 2026-05-25T13:04:10+00:00

I am rather new to c# and .NET, and trying to create an asp.net

  • 0

I am rather new to c# and .NET, and trying to create an asp.net web application that dynamically loads assembly.

Initially, I’ve used Activator.CreateInstance 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.

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.

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.

Edit: I’ve tried the following code as Davide Piras suggested, but the DLL file is still locked

    private static T CreateInstance<T>(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<Type> assemblyTypes = assembly.GetTypes().ToList();

                Type assemblyType =
                    assemblyTypes.FirstOrDefault(asmType => 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);
        }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:04:11+00:00Added an answer on May 25, 2026 at 1:04 pm

    to load the assemblies in the same AppDomain but not lock the files after load just use the LoadFrom method in this way:

    Assembly asm = Assembly.LoadFrom( “mydll.dll” );
    

    in this way you are done and Session will be available.

    there will be an issue with the debugger because symbols (*.pdb) will not get loaded so no breakpoint and no debugging available, to be able to debug you should really load the .pdb files in memory as well, for example using FileStream.

    Edit: The way you can do to also load symbols and not lock the files is to use proper overload of Assembly.Load, the one that gets two byte[] one for the assembly and the other for the assembly’ symbols file (.pdb file):

    public static Assembly Load(
        byte[] rawAssembly,
        byte[] rawSymbolStore
    )
    

    in fact you should first load the bytes with a stream then call Assembly.Load and pass the byte[]

    EDIT 2:

    here a full example to load the assemblies in your current domain, including the symbol file and not having the files locks.

    it’s a full example found online, it reflect everything you need including the handling of AppDomain.AssemblyResolve…

    public static void Main() {
          AppDomain currentDomain = AppDomain.CurrentDomain;
    
          currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolver);
       }
    
       static void InstantiateMyType(AppDomain domain) {
          try {
         // You must supply a valid fully qualified assembly name here.
             domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType");
          } catch (Exception e) {
             Console.WriteLine(e.Message);
          }
       }
    
       // Loads the content of a file to a byte array. 
       static byte[] loadFile(string filename) {
          FileStream fs = new FileStream(filename, FileMode.Open);
          byte[] buffer = new byte[(int) fs.Length];
          fs.Read(buffer, 0, buffer.Length);
          fs.Close();
    
          return buffer;
       }   
    
       static Assembly MyResolver(object sender, ResolveEventArgs args) {
          AppDomain domain = (AppDomain) sender;
    
          // Once the files are generated, this call is
          // actually no longer necessary.
          EmitAssembly(domain);
    
          byte[] rawAssembly = loadFile("temp.dll");
          byte[] rawSymbolStore = loadFile("temp.pdb");
          Assembly assembly = domain.Load(rawAssembly, rawSymbolStore);
    
          return assembly;
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a form in ASP.NET MVC2 RC 2 that is
Sorry for asking a rather n00b question, I am rather new to ASP.NET MVC.
I'm trying to create a simple Asp.Net page to read Perfmon counters from a
I'm working with c#.NET MVC2 and I'm trying to create an ajax form that
I have a Create New Employee ASP.Net MVC form. My complex object is an
I am trying to deploy a .NET Web application to IIS (7.5) without any
I'm trying to learn ASP.NET MVC 3 coming from a web forms background. I'm
Being rather new to ASP.NET MVC, I am already seeing some benefits of it
Being rather new to ASP.MVC I'm looking for a solution to the following routing
I'm rather new to C++ and I'm trying to understand the code over on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.