Oct 20 2009

.NET Plugin DLL Hell (aka assembly binding nightmares)

Category: Technology, WinFormsAdam Toth @ 6:08 pm

I’ve worked on a few .NET WinForms applications that utilized a “plugin” model. You’ve probably seen similar apps out there, where a main application searches a directory for DLLs, and then tries to load them into the AppDomain, using some code like below:

Assembly plugin = System.Reflection.Assembly.LoadFrom(pluginFileName);

object pluginClass = Activator.CreateInstance(plugin.GetType("Plugin.PluginClass"));

 

This kind of architecture is pretty standard, and is one of the few ways to accomplish this plugin model in .NET (other than some cutting-edge things like MEF). It works for the most part, but has some obvious limitations, such as:

  • Once a DLL is loaded in this manner, it cannot be unloaded from memory (unless you mess around with AppDomains for each plugin)
  • If your plugins want to talk to one another, they pretty much have to go through a lot of plumbing code in the main app

Where it really starts to have problems is when your plugin DLL needs to reference other DLLs used by the main application, or other plugin DLLs, and you find yourself in .NET Plugin DLL Hell.

Here is the crux of the problem:

When you add a reference to a DLL via Visual Studio, the framework will always try to load an exact matching version of that assembly, and will fail if it can’t find it. So, good luck if that DLL you depend on ever gets upgraded to a new version.

You can usually control this with a standalone application, because when you upgrade the app, you upgrade all the dependencies at the same time. In a plugin model, you can’t control when other dependencies are upgraded, and so your plugin will simply fail to load when this happens.

There is no way in the .NET framework to tell it to use a particular version of a DLL or any newer version it finds.

Here’s a scenario to demonstrate this:

  • You have a WinForms application, that loads plugin DLLs when the app starts.
  • The main application uses a third-party control library.
  • You write a plugin that also needs the controls, so you add a reference to the version that the main app uses.
  • At some point, someone working on the main app upgrades it to use a newer controls DLL, recompiles the app, and distributes it along with the newer controls DLL.
  • Since no work was actually done on your plugin, no one has recompiled it against the newer controls DLL.
  • When the upgraded application starts and loads your plugin, your plugin goes BOOM!

Another scenario:

  • Your plugin references another plugin
  • The other plugin gets upgraded without your plugin getting recompiled
  • Your plugin now goes BOOM!

When I say “BOOM!”, I mean you’ll probably see something like this from the Fusion log:

"System.IO.FileNotFoundException: Could not load file or assembly ‘YourDependentAssemblyNameHere’ or one of its dependencies. The system cannot find the file specified.” 

I am having a similar problem with my Windows LiveWriter plugin, xPollinate. Every time Microsoft releases a new version of LiveWriter, all of its DLLs that I reference have new version numbers, and so my plugin will fail to load unless I recompile my plugin against the latest versions of the DLLs.

Well, so what are our options to deal with this?

  • You can simply not add any references in your plugin project via Visual Studio, and just use reflection for everything (yuck). This is probably a little easier now with the dynamic keyword in C#, but not much.
  • You can try to get the main application to put in assembly binding redirects in its config file for any common DLLs that might be referenced by plugins.

That’s all I can see at this point. Here’s to hoping a future version of the framework will make this easier for us.

Tags: , , , , ,


Mar 17 2009

xPollinate First Look – Windows Live Writer Cross Post Plugin

Category: TechnologyAdam Toth @ 2:23 pm

I am just finishing up the touches on my first Windows Live Writer plugin, which I’m calling xPollinate. I’ve wanted to cross post to multiple blogs as well as Ping.FM from Windows Live Writer, and originally I found this plugin by Daniel Cazzulino that was not perfect, but got the job done.

I used this plugin as a basis and then added a bunch of bells and whistles that I wanted. Following are some feature enhancements that I’ve added:

  • Cross post to multiple blogs at the same time
  • Choose blogs to post to at cross post time
  • Set plugin defaults through the options dialog, and override at posting time.
  • Ability to turn comments and trackbacks (pings) on/off at the cross posted blog
  • Add header or footer html to each cross posted entry
  • Cross post to Ping.FM
    • Supports triggers and services
    • Post using blog, microblog, or status methods, simultaneously or individually

Here are some screen shots:

The Blogs Tab:

xPollinate Blogs Tab

This enables you to select multiple blogs to cross post to. You can optionally post a summary, and include header and footer text.

The Ping.FM tab:

xpollinate-options-ping-large-triggers

This enables you to post your blog entry via Ping.FM to your configured services. You can post a full or summarized version to your Ping.FM blogs, and can post a notification to your Ping.FM status and microblog services. The format of the notification is customizable.

Left to Do

I’ll be releasing this as a free plugin to the community, and will release the source as well. The final few things I have left to do are:

  • Clean up the installer
  • Get a public API key from Ping.FM
  • Submit to the Live Writer plugin gallery

Tags: , , ,