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. Dynamically Load Referenced Assembly

Dynamically Load Referenced Assembly

Scheduled Pinned Locked Moved C#
helpannouncementquestion
5 Posts 3 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.
  • A Offline
    A Offline
    Adam R Harris
    wrote on last edited by
    #1

    Hey All, I have a problem with dynamically loading and referenced assembly at run time. The issue is that i am coding a 3rd party app for a larger application that is constantly getting patched and upgraded. Every time a new patch or upgrade comes out i need to recompile and redistribute the app too all the clients. Now nothing really ever changes in the referenced assembly aside from version number which is causing me to have to remove the reference and add it again. I don't even have to change code, just update the reference. I know i can load this specific assembly when the reference isn’t found by attaching to the AssemblyResolve event; however this is where I’m having problems. I’m not exactly sure how i should implement this. All the documentation i can find from MS isn’t very helpful, as per usual. Really the end goal is to have it load this referenced assembly when it is called at run time. The referenced assemblies path will change from machine to machine so it needs to fully dynamic, i can work out finding the proper assembly to load i just need some insight on the best way to implement this. Also another quick question ... if i reference say v1.2 and then dynamically load v3.5 is the framework going to freak out about the version numbers or just accept that this is the assembly i want to load. TIA

    Don't comment your code - it was hard to write, it should be hard to read!

    N E 2 Replies Last reply
    0
    • A Adam R Harris

      Hey All, I have a problem with dynamically loading and referenced assembly at run time. The issue is that i am coding a 3rd party app for a larger application that is constantly getting patched and upgraded. Every time a new patch or upgrade comes out i need to recompile and redistribute the app too all the clients. Now nothing really ever changes in the referenced assembly aside from version number which is causing me to have to remove the reference and add it again. I don't even have to change code, just update the reference. I know i can load this specific assembly when the reference isn’t found by attaching to the AssemblyResolve event; however this is where I’m having problems. I’m not exactly sure how i should implement this. All the documentation i can find from MS isn’t very helpful, as per usual. Really the end goal is to have it load this referenced assembly when it is called at run time. The referenced assemblies path will change from machine to machine so it needs to fully dynamic, i can work out finding the proper assembly to load i just need some insight on the best way to implement this. Also another quick question ... if i reference say v1.2 and then dynamically load v3.5 is the framework going to freak out about the version numbers or just accept that this is the assembly i want to load. TIA

      Don't comment your code - it was hard to write, it should be hard to read!

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #2

      How are you loading them dynamically? You don't need to specify the version (assuming you are using Reflection) and instead can just load from a file path.

      Regards, Nish


      My technology blog: voidnish.wordpress.com Code Project Forums : New Posts Monitor This application monitors for new posts in the Code Project forums.

      A 1 Reply Last reply
      0
      • N Nish Nishant

        How are you loading them dynamically? You don't need to specify the version (assuming you are using Reflection) and instead can just load from a file path.

        Regards, Nish


        My technology blog: voidnish.wordpress.com Code Project Forums : New Posts Monitor This application monitors for new posts in the Code Project forums.

        A Offline
        A Offline
        Adam R Harris
        wrote on last edited by
        #3

        First, thanks for the reply.

        Nishant Sivakumar wrote:

        How are you loading them dynamically?

        Assembly.LoadFrom([path_to_assembly]) I will need to find the path to the assembly myself, which is not a problem. The problem is the proper implementation. I want to have a reference at design time, just set Copy To Local to false, and then at runtime actually go out and find the proper assembly on the client machine.

        Nishant Sivakumar wrote:

        You don't need to specify the version (assuming you are using Reflection) and instead can just load from a file path.

        Just what i thought, thanks for confirming this. Thanks again for the reply

        Don't comment your code - it was hard to write, it should be hard to read!

        1 Reply Last reply
        0
        • A Adam R Harris

          Hey All, I have a problem with dynamically loading and referenced assembly at run time. The issue is that i am coding a 3rd party app for a larger application that is constantly getting patched and upgraded. Every time a new patch or upgrade comes out i need to recompile and redistribute the app too all the clients. Now nothing really ever changes in the referenced assembly aside from version number which is causing me to have to remove the reference and add it again. I don't even have to change code, just update the reference. I know i can load this specific assembly when the reference isn’t found by attaching to the AssemblyResolve event; however this is where I’m having problems. I’m not exactly sure how i should implement this. All the documentation i can find from MS isn’t very helpful, as per usual. Really the end goal is to have it load this referenced assembly when it is called at run time. The referenced assemblies path will change from machine to machine so it needs to fully dynamic, i can work out finding the proper assembly to load i just need some insight on the best way to implement this. Also another quick question ... if i reference say v1.2 and then dynamically load v3.5 is the framework going to freak out about the version numbers or just accept that this is the assembly i want to load. TIA

          Don't comment your code - it was hard to write, it should be hard to read!

          E Offline
          E Offline
          Ennis Ray Lynch Jr
          wrote on last edited by
          #4

          Register the Assembly Resolve event before starting your applications message pump, or in the case of an ASP.NET application in the Application_Start event. Other than that, post a specific issue. I have never had a problem using Assembly resolve; it is fairly straight forward. Here is a copy and paste from another project with some namespaces removed.

          using System;
          using System.Data;
          using System.Configuration;
          using System.Reflection;
          using System.Web;
          using System.Web.Security;
          using System.Web.UI;
          using System.Web.UI.WebControls;
          using System.Web.UI.WebControls.WebParts;
          using System.Web.UI.HtmlControls;
          namespace _Website {
          public class AssemblyResolver {

          	private bool mLoading = false;
          
          	public AssemblyResolver(AppDomain appDomain) {
          		appDomain.AssemblyResolve += new ResolveEventHandler(OnAssemblyResolve);
          	}
          
          	/\*\*
          	 \* <summary>
          	 \* This method will attempt to result assemblies
          	 \* </summary>
          	 \*/
          	private Assembly OnAssemblyResolve(object sender, ResolveEventArgs args) {
          		Assembly result = null;
          		//Prevent Stack overflow, this method will recurse without this line
          		if(!mLoading) {
          			try {
          				mLoading = true;
          				IServiceBroker broker = ServiceRequests.CommonRequests.GetBroker();
          				byte\[\] assembly = broker.ResolveAssembly();
          				result = Assembly.Load(assembly);
          			}//end try
          			//catch(Exception e1) {
          			//	e1 = e1;  //Used for debugging
          			//}
          			finally {
          				mLoading = false;
          			}
          		}//end mLoading
          		return result;
          	}
          }
          

          }

          Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

          A 1 Reply Last reply
          0
          • E Ennis Ray Lynch Jr

            Register the Assembly Resolve event before starting your applications message pump, or in the case of an ASP.NET application in the Application_Start event. Other than that, post a specific issue. I have never had a problem using Assembly resolve; it is fairly straight forward. Here is a copy and paste from another project with some namespaces removed.

            using System;
            using System.Data;
            using System.Configuration;
            using System.Reflection;
            using System.Web;
            using System.Web.Security;
            using System.Web.UI;
            using System.Web.UI.WebControls;
            using System.Web.UI.WebControls.WebParts;
            using System.Web.UI.HtmlControls;
            namespace _Website {
            public class AssemblyResolver {

            	private bool mLoading = false;
            
            	public AssemblyResolver(AppDomain appDomain) {
            		appDomain.AssemblyResolve += new ResolveEventHandler(OnAssemblyResolve);
            	}
            
            	/\*\*
            	 \* <summary>
            	 \* This method will attempt to result assemblies
            	 \* </summary>
            	 \*/
            	private Assembly OnAssemblyResolve(object sender, ResolveEventArgs args) {
            		Assembly result = null;
            		//Prevent Stack overflow, this method will recurse without this line
            		if(!mLoading) {
            			try {
            				mLoading = true;
            				IServiceBroker broker = ServiceRequests.CommonRequests.GetBroker();
            				byte\[\] assembly = broker.ResolveAssembly();
            				result = Assembly.Load(assembly);
            			}//end try
            			//catch(Exception e1) {
            			//	e1 = e1;  //Used for debugging
            			//}
            			finally {
            				mLoading = false;
            			}
            		}//end mLoading
            		return result;
            	}
            }
            

            }

            Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

            A Offline
            A Offline
            Adam R Harris
            wrote on last edited by
            #5

            Fantastic, great implementation and pretty much exactly what i was looking for. Another thing, i am inheriting from classes inside the referenced assembly, is there a way to implement this for inheritence inside a class library? I tried throwing it inside the constructor, but it fails because when it goes to create the class it cant find the reference. I guess my question would be; is there a way to implement this globably for a class library? Thx for the reply

            Don't comment your code - it was hard to write, it should be hard to read!

            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