Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. multiple loading of assemblies - does it have any associated problems?

multiple loading of assemblies - does it have any associated problems?

Scheduled Pinned Locked Moved C#
helpquestion
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Danzy83
    wrote on last edited by
    #1

    I want to keep some of my classes in library files and load them during runtime. I can load the assemblies and everything works fine. However, I don't find any function that will close or unload the assembly after a method is done using the assembly. If the function is called several times, the assembly loading will be done multiple times. Without any function to unload it, can it cause any problem in application some times? I'm now beginning the project so I would like to know the right approach and follow it. I would like to know before I start my project as I do not want to face any problems in the middle of the project. Please help and thanks in advance.

    L J D G B 5 Replies Last reply
    0
    • D Danzy83

      I want to keep some of my classes in library files and load them during runtime. I can load the assemblies and everything works fine. However, I don't find any function that will close or unload the assembly after a method is done using the assembly. If the function is called several times, the assembly loading will be done multiple times. Without any function to unload it, can it cause any problem in application some times? I'm now beginning the project so I would like to know the right approach and follow it. I would like to know before I start my project as I do not want to face any problems in the middle of the project. Please help and thanks in advance.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Dan_K wrote:

      If the function is called several times, the assembly loading will be done multiple times.

      Why? Just add in a boolean that's set when it's loaded the first time, and check it before you load the assembly. That way the logic is executed only once, eliminating the need to know how often the assembly is in memory. FWIW; it's loaded into memory once, and remains there. There's no unloading, unless you put everything in a separate AppDomain.

      Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

      D 1 Reply Last reply
      0
      • L Lost User

        Dan_K wrote:

        If the function is called several times, the assembly loading will be done multiple times.

        Why? Just add in a boolean that's set when it's loaded the first time, and check it before you load the assembly. That way the logic is executed only once, eliminating the need to know how often the assembly is in memory. FWIW; it's loaded into memory once, and remains there. There's no unloading, unless you put everything in a separate AppDomain.

        Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

        D Offline
        D Offline
        Danzy83
        wrote on last edited by
        #3

        Thanks! But does the AppDomain check whether it has already been loaded so that it does not load again for consecutive calls? I'm using .net framework 4.0.

        L 1 Reply Last reply
        0
        • D Danzy83

          Thanks! But does the AppDomain check whether it has already been loaded so that it does not load again for consecutive calls? I'm using .net framework 4.0.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Dan_K wrote:

          But does the AppDomain check whether it has already been loaded so that it does not load again for consecutive calls?

          Dunno, you'd have to check the documentation for that. Just add in the check yourself, it's simply a matter of setting and checking a boolean to avoid the question in the first place - I wouldn't waste time on finding out.

          Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

          1 Reply Last reply
          0
          • D Danzy83

            I want to keep some of my classes in library files and load them during runtime. I can load the assemblies and everything works fine. However, I don't find any function that will close or unload the assembly after a method is done using the assembly. If the function is called several times, the assembly loading will be done multiple times. Without any function to unload it, can it cause any problem in application some times? I'm now beginning the project so I would like to know the right approach and follow it. I would like to know before I start my project as I do not want to face any problems in the middle of the project. Please help and thanks in advance.

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            Presumably you are referring to reflection in that you are loading the type yourself. Per your basic description you should structure your code such that the assembly, and even the class is only loaded once. Once loaded you create instances from that class.

            Dan_K wrote:

            I do not want to face any problems in the middle of the project

            Better make sure that you do not need to unload it while the app is running since that is a different problem.

            1 Reply Last reply
            0
            • D Danzy83

              I want to keep some of my classes in library files and load them during runtime. I can load the assemblies and everything works fine. However, I don't find any function that will close or unload the assembly after a method is done using the assembly. If the function is called several times, the assembly loading will be done multiple times. Without any function to unload it, can it cause any problem in application some times? I'm now beginning the project so I would like to know the right approach and follow it. I would like to know before I start my project as I do not want to face any problems in the middle of the project. Please help and thanks in advance.

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              Dan_K wrote:

              If the function is called several times, the assembly loading will be done multiple times

              An assembly is only ever loaded once. There is no such thing as unloading it unless 1) you tear down the entire process into which it was loaded or 2) it was loaded into an AppDomain and you tear down the AppDomain. Really, by trying to load and unload assemblies, you're creating FAR more complexity for your application. What if you unloaded an assembly that your code still needed?? BOOM! Exception! Good luck with this.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak

              1 Reply Last reply
              0
              • D Danzy83

                I want to keep some of my classes in library files and load them during runtime. I can load the assemblies and everything works fine. However, I don't find any function that will close or unload the assembly after a method is done using the assembly. If the function is called several times, the assembly loading will be done multiple times. Without any function to unload it, can it cause any problem in application some times? I'm now beginning the project so I would like to know the right approach and follow it. I would like to know before I start my project as I do not want to face any problems in the middle of the project. Please help and thanks in advance.

                G Offline
                G Offline
                Guirec
                wrote on last edited by
                #7

                If you have the need for unloading assemblies then you have to load them in a dedicated AppDomain you will create first. The advantages is that you can kill the newly created AppDomain (unloading assemblies) and other advantage is that if one assembly you load is "rotten" then you are not damaging the primary AppDomain. The drawback is the only basic option to communicate between appdomain is remoting. If you are looking for some kind of plug-in mechanism then have a look at ms Managed Extensibility Framework (MEF). You'll find good articles on CP about it.

                J 1 Reply Last reply
                0
                • D Danzy83

                  I want to keep some of my classes in library files and load them during runtime. I can load the assemblies and everything works fine. However, I don't find any function that will close or unload the assembly after a method is done using the assembly. If the function is called several times, the assembly loading will be done multiple times. Without any function to unload it, can it cause any problem in application some times? I'm now beginning the project so I would like to know the right approach and follow it. I would like to know before I start my project as I do not want to face any problems in the middle of the project. Please help and thanks in advance.

                  B Offline
                  B Offline
                  BobJanova
                  wrote on last edited by
                  #8

                  Do you really need to 'load them at runtime' (i.e. dynamically using Assembly.Load)? You only need to do that if you want a plugin based architecture. Simply linking to the DLLs at compile time should be sufficient in most cases. If you do, you should implement some sort of lazy loading wrapper which loads an assembly only if it needs it. Though I think Assembly.Load probably already does that for you internally (don't quote me on that though). You can only unload assemblies if they're loaded into separate AppDomains, which has serious implications for communication between modules.

                  1 Reply Last reply
                  0
                  • G Guirec

                    If you have the need for unloading assemblies then you have to load them in a dedicated AppDomain you will create first. The advantages is that you can kill the newly created AppDomain (unloading assemblies) and other advantage is that if one assembly you load is "rotten" then you are not damaging the primary AppDomain. The drawback is the only basic option to communicate between appdomain is remoting. If you are looking for some kind of plug-in mechanism then have a look at ms Managed Extensibility Framework (MEF). You'll find good articles on CP about it.

                    J Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #9

                    Guirec Le Bars wrote:

                    that if one assembly you load is "rotten" then you are not damaging the primary AppDomain.

                    That is true only to a limited extent. For example a bad assembly could still cause a out of memory error or a VM exit.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups