My First Foray Into Tech Blogging...
Other than drinking inordinate amounts of coffee and cursing at servers the two things I’m typically stuck doing is writing highly scalable multi threaded applications and getting bizarre 3rd party APIs to talk to each other. So, this blog is mostly going to be an adventure into the dark corners of .Net, C++ and other frameworks and platforms that you don’t want to mention in the presence of polite company.
My First Foray Into Tech Blogging...
Other than drinking inordinate amounts of coffee and cursing at servers the two things I’m typically stuck doing is writing highly scalable multi threaded applications and getting bizarre 3rd party APIs to talk to each other. So, this blog is mostly going to be an adventure into the dark corners of .Net, C++ and other frameworks and platforms that you don’t want to mention in the presence of polite company.
Implementing a Hot-Swap Plugin architecture for .Net (Part 1)
A while back I decided to dabble a bit in Erlang and to be honest I was completely and utterly jealous of the ability to be able to hot-load code on the fly. I wonder how we could do that in C#...
The Interface And The Loader.
Let’s say our pseudo-hypothetical service performs string transformations. There are really only 2 required parts of any .Net based plug-in framework, the interface and the loader. The interface is responsible for describing what needs to be done, a binary contract if you will. The loader, well, loads up plugins so our app can use them. So without further adieu…
public interface IPlugin
{
string DoSomethingCool(string input);
string Name { get; }
int Version { get; }
}
DoSomethingCool is our main method the app will be calling to perform the string transformations. Name and Version are going to be used for diagnostics along with our hot code loading system.
public IEnumerableGetPlugins () { string path; //our plugin directory is the current app path + \Plugins path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\Plugins"; if (path.StartsWith("file:\\")) path = path.Remove(0, 6); DirectoryInfo di = new DirectoryInfo(path); if (!di.Exists) di.Create(); var dlls = from fn in Directory.GetFiles(path) where fn.EndsWith(".dll") select fn; foreach (var dll in dlls) { object o = null; try { Assembly asm = Assembly.LoadFrom(dll); Type[] types = asm.GetTypes(); foreach (Type t in types) { Type typeInterface = t.GetInterface(typeof(T).FullName, true); //type query if (typeInterface != null) o = Activator.CreateInstance(t); } } catch{ /*log this*/ } if (o != null) yield return (T)o; } }
And that’s our simple loader. All it does is scan and yields a bunch of active instances of our plugins from the target assemblies. In part 2 we are going to look at how to dynamically manage these instances so we can add, remove, modify and upgrade them at runtime.
Last 4 tweets from identitylogix:
People talking about '@identitylogix':
Leave your comment