5,154,487 members and growing! (19,102 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

Using NetZ with Dynamically Loaded Assemblies

By Marc Clifton

How to use dynamically loaded assemblies with your NetZ-built application.
C#, Windows, .NET, Visual Studio, Arch, Dev

Posted: 21 Jul 2006
Updated: 21 Jul 2006
Views: 9,671
Announcements



Search    
Advanced Search
Sitemap
7 votes for this Article.
Popularity: 3.83 Rating: 4.53 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 14.3%
3
1 vote, 14.3%
4
5 votes, 71.4%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

NetZ is a fantastic tool for merging your executable and dependent assemblies into a single, zipped, executable.  Using NetZ makes distribution of an application very easy, and it provides a layer of protection from programs like Reflector without having to buy into an obfuscation approach.  However, a NetZ application will not be able to resolve dynamically loaded assemblies, such as business rule plug-ins, extenders, or assemblies referenced in MyXaml-based applications.

The Assembly Resolver

I wrote this small static class to sneak into NetZ's assembly loading mechanism so that a dynamically loaded assembly can be acquired from the NetZ assembly collection.  This library can be added to your application and used during development when you don't need the resolver.  When you're ready to build your NetZ executable, the resolver will automatically figure out that the application is being run from the NetZ loader and will take the appropriate action.

The first step is to detect NetZ, which uses a bit of reflection to see if the NetzStarter class is in the executable:

/// <summary>

/// Checks whether the application has been started from a NetZ loader.

/// If so, then the current domain's AssemblyResolve event is hooked so 

/// that we can get dynamically loaded assemblies from the NetZ assembly

/// collection.

/// </summary>

protected static void CheckForNetZ()
{
  string appName = AppDomain.CurrentDomain.FriendlyName;
  appName = StringHelpers.LeftOfRightmostOf(appName, '.');
  Trace.WriteLine(appName);
  Type t = Type.GetType("netz.NetzStarter, " + appName);

  // t will be valid if the app is started from a NetZ loader.

  if (t != null)
  {
    Trace.WriteLine("Found NetZ. Hooking assembly resolver.");
    netzAssemblyLoader = t.GetMethod("GetAssemblyByName", 
BindingFlags.Static | BindingFlags.NonPublic); AppDomain.CurrentDomain.AssemblyResolve +=
new ResolveEventHandler(OnAssemblyResolve); } }

I put in some trace messages so you can see what the application is doing in debug mode.

The next step is to resolve the assembly:

/// <summary>

/// Resolve dynamically loaded assemblies.

/// </summary>

private static Assembly OnAssemblyResolve(object sender, 
ResolveEventArgs args) { Assembly assy = null; string assyName = args.Name; // If this is a short name format... if (assyName.Split(',').Length == 1) { // Look up the fully qualified name. assyName = qualifiedNameMap[assyName.ToLower()]; } Trace.WriteLine("(NetZResolver) Attempting to resolve: " + assyName); assy = (Assembly)netzAssemblyLoader.Invoke(null, new object[] { assyName }); return assy; }

Usage

Using the class is very simple.  For example, here are the calls to resolve the MyXaml.WinForms extension, which is dynamically loaded:

NetZResolver.Initialize();
NetZResolver.RegisterShortName("MyXaml.WinForms", "MyXaml.WinForms, 
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
);

Note that the fully qualified assembly name is required, including the version.  This means that if you update an assembly version number, you will need to also update the fully qualified name.  This is only necessary if the resolver is given a short name.  If you have dynamic assemblies that are being loaded by only the short name, then you will have to register the short name and the fully qualified assembly name with the resolver, as the above example illustrates.

Conclusion

Using this class, you shouldn't have any problems resolving dynamically loaded assemblies.  Please note that I will not answer any NetZ support questions--NetZ is well documented and if you have any problems, please contact the author of NetZ.  Feel free to change the namespaces for the two source files to more suit your needs, but please keep the copyright in the source code.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Marc Clifton


Mvp, Protector, Supporter
Marc is the creator of MyXaml, an open source, declarative, XML instantiation engine. He is an industry consultant working primarily with companies interested in utilizing declarative programming concepts to add flexibility to n-tier architectures on web, CE, and desktop platforms. His other major open source project is the Advanced Unit Testing framework.  He operates his own website, www.marcclifton.com, where you will find many of his articles.

Marc lives in Hudson, NY with his girlfriend Karen and his son Ian, who attends the Hawthorne Valley School. To contact Marc, email him at marc.clifton@gmail.com.

Occupation: Architect
Company: Interacx
Location: United States United States

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
Subject  Author Date 
Questionexe @ network location [modified]membermdcobra5:59 12 Oct '07  
QuestionReflector Protection?memberPhilip Laureano11:34 22 Jul '06  
AnswerRe: Reflector Protection?supporterMarc Clifton13:26 22 Jul '06  
GeneralNetZ Rocks!memberPaulC197214:02 21 Jul '06  
GeneralRe: NetZ Rocks!supporterMarc Clifton14:08 21 Jul '06  
GeneralRe: NetZ Rocks!memberPaulC197214:20 21 Jul '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 Jul 2006
Editor:
Copyright 2006 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2008
Web08 | Advertise on the Code Project