Saturday, May 19, 2012
  • Resource Center
  • Support
  • Contact Us
  • Products
    • SpyLogix Enterprise
      1. SpyLogix Platform
      2. SpyLogix Modules
        • Active Directory
        • Windows Server
        • User Security
        • FIM 2010
        • LDAP Directories
        • CA SiteMinder
        • VMware vSphere
        • IBM System z and i
        • Module SDK
      3. SpyLogix Architecture
    • SpyLogix for Microsoft
      1. Active Directory
      2. Windows Server
      3. User Security
      4. FIM 2010
    • IDx Identity Assurance Suite
      1. IDx Voice Self Service Password Reset
  • Solutions
    • SpyLogix Key Benefits
    • Cloud Solutions
    • Microsoft Solutions
    • Government Solutions
    • Identity Assurance Solutions
    • Information Security Solutions
  • Partners
    • Overview
    • System Integrators
    • Cloud Service Partners
    • Technology Partners
    • Become an IdentityLogix Partner
  • News & Events
    • Events
    • Webinars
    • Press Releases
    • In The News
  • Company
    • About Us
    • Careers
    • Support
    • Contact Us
  • Blog
onthebeachblog3
  • Subscribe to updates
  • Print
  • PDF
  • Bookmark
Blaise Boscaccy

My First Foray Into Tech Blogging / A Hot-Swap Plugin Framework for .Net

By Blaise Boscaccy on
Blaise Boscaccy
IdentityLogix VP of Product Development
User is currently offline
Aug 17 in Code 0 Comments
Tweet

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 IEnumerable GetPlugins()
{

    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.

Tags: .NET, plugin, C Sharp, C#, code
Hits: 2042
Rate this blog entry
2 votes
  • SmartGrid Breakaway Capability
  • In My Humble Opinion
  • Author
  • Trackbacks

About the author

Blaise Boscaccy

IdentityLogix VP of Product Development

Trackbacks

Trackback URL for this blog entry

Comments

No comments made yet. Be the first to submit a comment

Leave your comment

Loading
Guest
Guest Saturday, 19 May 2012
Member Login

Categories

GRC
3 post(s)
IT GRC
2 post(s)
Code
1 post(s)
PCI
1 post(s)

Bloggers

Gary Sheehan
Gary Sheehan
1 post(s)
"Gary is the Director of GRC Services for Advanced ..."
http://gsheehan@asmgi.com
Blaise Boscaccy
Blaise Boscaccy
1 post(s)
"IdentityLogix VP of Product Development"
Steven Phipps
Steven Phipps
5 post(s)
"Steven is IdentityLogix Vice President Profession ..."

Join Us

Tag Cloud

Audit NISTIR 7756 Monitor M2C Assess Energy PCI 1.2 ISO38500 Smart Grid risk assessment real-time PCI 2.0 NERC-CIP Compliance eGRC Utilities plugin PCI DSS CCM C# Report code AMI Continuous Monitoring ISO 27002 PCI Remediate COBIT Smart Meter CAESARS C Sharp Continuous Control Monitoring NISTIR 7628 Asset Management Smart Grid IT GRC .NET NIST 800-53 GRC Regulatory MDMS

Follow Us

identitylogix's avatar
IdentityLogix identitylogix
Loading...

Last 4 tweets from identitylogix:

People talking about '@identitylogix':

  • Solutions
  • Products
  • Partners
  • News & Events
  • Company
  • Legal Notice
  • Privacy Policy
  • Contact Us
© Copyright 2010, IdentityLogix, All Rights Reserved.

Login

  • Forgot your password?
  • Forgot your username?