mr mann's mishmash

it's just a game of give and take…

Loading and resolving assemblies

Comments off

We’ve been writing a tool on our project that requires late-binding to classes/types in our service layer. It’s typical plugin style coding, similar to how much of the various Microsoft Enterprise Library modules are meant to be loaded when needed.

One of the questions raised was what happens if the assemblies containing our classes/types are not in the execution path? This usually occurs when the requested assembly has a dependency on another assembly.

After some playing about with explicitly loading assemblies into the current AppDomain, I found a couple of useful events; AssemblyLoad and AssemblyResolve. The real saviour is AssemblyResolve – hook this up up in your plugin loading code and they it gives you a chance to handle when the requested assembly fails to load. Both these events hang off the AppDomain.CurrentDomain and so catch any assembly load/failure that occurs within execution scope.

In the sample below, I’ve assumed that we know what the path to the assemblies are by setting a constant (if you don’t know this you’ll either have to set some predetermined hint paths or ask for user input). It’s here that the AssemblyResolve event can piece together the missing assembly path, load it and continue.

   1: const string assemblyPath = @"C:\LoadingAssemblies\Model\ParentAssembly\bin\Debug\";
   2:  
   3: private void LoadTest()
   4: {
   5:  
   6:     AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(CurrentDomain_AssemblyLoad);
   7:     AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
   8:  
   9:     Assembly pluginAssembly = Assembly.LoadFrom(string.Format(@"{0}\{1}", assemblyPath, "PluginAssembly.dll"));
  10:  
  11:     Type myType = pluginAssembly.GetType("PluginAssembly.ParentClass");
  12:     if (myType != null)
  13:         Debug.WriteLine(string.Format("{0} -- {1}", myType.FullName, myType.AssemblyQualifiedName));
  14:  
  15:     //fails since it is not defined in this assembly and does not have a full assembly definition
  16:     myType = Type.GetType("ChildAssembly.ChildClass");
  17:     if (myType != null)
  18:         Debug.WriteLine(string.Format("{0} -- {1}", myType.FullName, myType.AssemblyQualifiedName));
  19:  
  20:     //succeeds since we have the full definition - will fire the AssemblyResolve event which points to the assembly location
  21:     myType = Type.GetType("ChildAssembly.ChildClass, ChildAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
  22:     if (myType != null)
  23:         Debug.WriteLine(string.Format("{0} -- {1}", myType.FullName, myType.AssemblyQualifiedName));
  24:  
  25:     //fails since it is not defined in this assembly and does not have a full assembly definition
  26:     myType = Type.GetType("SubAssembly.SubClass");
  27:     if (myType != null)
  28:         Debug.WriteLine(string.Format("{0} -- {1}", myType.FullName, myType.AssemblyQualifiedName));
  29:  
  30:     //succeeds since we have the full definition - since the assembly is in the app path it will automatically load
  31:     myType = Type.GetType("SubAssembly.SubClass, SubAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
  32:     if (myType != null)
  33:         Debug.WriteLine(string.Format("{0} -- {1}", myType.FullName, myType.AssemblyQualifiedName));
  34:  
  35:     //fails because we need to fully qualify the namespace of the class
  36:     myType = Type.GetType("SubClass, SubAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
  37:     if (myType != null)
  38:         Debug.WriteLine(string.Format("{0} -- {1}", myType.FullName, myType.AssemblyQualifiedName));
  39:  
  40: }
  41:  
  42: static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
  43: {
  44:     Debug.WriteLine(string.Format("CurrentDomain_AssemblyResolve reports that '{0}' was unresolved.", args.Name));
  45:  
  46:     // args.Name == "ChildAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
  47:     string[] parts = args.Name.Split(',');
  48:     if (parts.Length > 0)
  49:     {
  50:         Assembly assembly = Assembly.LoadFrom(string.Format(@"{0}\{1}.dll", assemblyPath, parts[0].Trim()));
  51:         return assembly;
  52:     }
  53:  
  54:     return null;
  55: }
  56:  
  57: static void CurrentDomain_AssemblyLoad(object sender, AssemblyLoadEventArgs args)
  58: {
  59:     Debug.WriteLine(string.Format("CurrentDomain_AssemblyLoad reports that '{0}' was loaded.", args.LoadedAssembly.FullName));
  60: }

 

This is a cross post from my EMC blog, mainly for backup duplicity and to aggregate some of my past postings. My EMC blog used to be under the Conchango brand but was acquired by EMC so I’ve also retrospectively refreshed some of the old links and maybe a tweak a bit of content too.

permalink to the original post here

We recently hosted the second meeting of the Silverlight UK User Group at Conchango in London. It was a pretty packed agenda with two excellent presentations.

Mike Taulty‘s usually busy being a Microsoft Development Evangelist, presenting at various conferences or recording Silverlight demos on MSDN’s Channel9. He gave us a sneak preview of a presentation that he was preparing for ReMix08 about hooking up Silverlight applications to services and content beyond those that are embedded within the host application. The coding demos got deep, detailed and highlighted the many different options available of Silverlight’s ability to get connected.

Update: Michelle Flynn has posted up the video recording of this session here.

Update: I’ve embedded the session video below.

 

The second session got at lot of attention, especially from the developers in the audience who where in awe over "designer elements" of putting together a slickly styled Silverlight application. It was no wonder Tim Hustler and Simon Sturgess recently won a Microsoft competition (the Web Harmony ‘Be all you can be challenge’).

Update: Michelle Flynn has posted up the video recording of this session here.

Update: I’ve embedded the session video below.

 

The attendance at the meeting remained at a good ratio of developers and designers and the interaction throughout the evening was excellent – Feedback afterwards was good to hear that we are able to get good presentations and the discussion flowing at these meetings. If you have comments or suggestions then please let me and the team know by dropping us a message here.

Finally a grateful thanks to those that presented, those that attended and the team that works behind the scenes getting it all organised (Michelle Flynn, Richard Griffin, Jamie Thomson, Felix Corke). 


Want to join the discussion?
The event is geared to please/interest/inform both developers and designers alike, so if you are interested in coming along then please contact either myself or Michelle Flynn (here) and we’ll be glad to take your details.

Want to present or showcase?
We are always on the lookout for presenters for future sessions – whether it be a high or low level coding walk through, a designer’s perspective, working practices or showcase demo. If you think that you have a topic/presentation that ought to be shared with the community then please contact either me here and I’ll see if we can schedule you in!

This is a cross post from my EMC blog, mainly for backup duplicity and to aggregate some of my past postings. My EMC blog used to be under the Conchango brand but was acquired by EMC so I’ve also retrospectively refreshed some of the old links and maybe a tweak a bit of content too.
permalink to the original post here

It’s been a while but the second session of the Silverlight UK User Group is happening on Thursday, 14th August 2008! The event starts at 18:00 and will be hosted by Conchango at Notcutt House (nr Southwark Bridge).

Full Address: Conchango, Notcutt House, 36 Southwark Bridge Road, London, SE1 9EU.


AGENDA

Welcome/Kick off

No Silverlight (2) Application is an Island (of Richness)
Mike Taulty (Microsoft Developer Evangelist)
You have a Silverlight 2 application running on your web page and it’s beginning to dawn on you that this is much more like building a client application than it is like building a web page. How does the Silverlight application get data? Can it grab files? Can it call services? Can it download images? Can it talk to the browser? In a nutshell – where can it get "stuff" and where can it put "stuff"? In this session we’ll explore all the options.

Pizza and beer.. a chance to recharge and discuss in the interlude.

Creating Silverlight apps using Blend & VS’08
Silverlight developer Tim Hustler and Expressions Blend designer Simon Sturgess plan to do a how-to on creating Silverlight applications using Blend and Visual Studio 2008. Simon will create some custom controls and Tim will demonstrate some DataBinding from a back-end datasource written in WCF. They both recently won a competition ran by Microsoft, the Web Harmony ‘Be all you can be challenge’ from which this session is based upon.

Showcase (TBD) or Open discussion to finish.


Want to join in?
The event is geared to please/interest/inform both developers and designers alike, so if you are interested there are some spaces still available and Michelle Flynn will be glad to take your details.

Want to present or showcase?
We are always on the lookout for presenters for future sessions – whether it be a high or low level coding walk through, a workflow perspective or showcase demo. If you think that you have a topic/presentation that ought to be shared with the community then let myself or Richard Griffin know and we’ll see if we can schedule you in!

Hope to see you there!

mark.

This is a cross post from my EMC blog, mainly for backup duplicity and to aggregate some of my past postings. My EMC blog used to be under the Conchango brand but was acquired by EMC so I’ve also retrospectively refreshed some of the old links and maybe a tweak a bit of content too.
permalink to the original post here