Geeks With Blogs
A Developer's Expedition www.kenl.net

I have been playing around with Managed Extensibility Framework (MEF) and have been very happy with the way it works overall.

If you remember when you use refection you do something like this

try
            {
                Assembly assy = Assembly.LoadFile(<DLLLocation>);
                Type assyType = assy.GetType("ClassLibrary1.Class1");
                object assyInstance = Activator.CreateInstance(assyType);
                PropertyInfo msginfo = assyType.GetProperty("message");
                var msgValue = "Hello from this Reflected assembly";
                assyType.GetProperty("message").SetValue(assyInstance, msgValue, null);
                assyType.InvokeMember("Run", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, null, assyInstance, null);
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }

The problem with reflection is once you import the assembly you have to restart your appliation to drop that assembly. Kind of like the old VB 6 DLL hell days.

When you use MEF, you can import a DLL on the fly by using a filewatcher, consume the DLL, drop the DLL, pick up another DLL, whatever you need to do LIVE and without having to stop your app. Considering how I work on systems that must be in production 24/7 I am finding MEF the best way to write my rule engines. For example business rules are usually different from state to state in the insurance business. So when my company rolls out a new state, or updates rules for a state I simply drop the new rules into a DLL and drop the DLL into my MEF Library.  Here is how;

When the file watch finds a file the event called is;

private static void DLL_Found(object source, System.IO.FileSystemEventArgs e)
        {
            Console.WriteLine("DLL: " + e.FullPath + " " + e.ChangeType);
            MEF_Consume_DLLs(@"C:\MEFTests2");
        }

And to consume the DLL's found in the folder simply;

 private static void MEF_Consume_DLLs(string FolderLocation)
        {
            // Get collection of DLL's consume with MEF
            var dc = new DirectoryCatalog(FolderLocation);
            foreach (var dcassy in dc.LoadedFiles)
            {
                try
                {
                    var catalog2 = new AggregateCatalog();
                    catalog2.Catalogs.Add(new AssemblyCatalog(dcassy));
                    var con = new CompositionContainer(catalog2);
                    con.GetExport<IMessage>().Value.message = "Hello from this MEF Assembly - " + dcassy.ToString();
                    bool test2 = con.GetExport<IMessage>().Value.MessageString();
                    Console.WriteLine("Message returned from ClassLibrary: {0} \n", test2);
                }
                catch (Exception ex)
                {
                    Console.Write(ex.Message);
                }
            }
        }

Here is a copy of the entire test application in Visual Studio 2010 .net 4.0 - http://www.kenl.net/mef-examples.zip

 

 

 

Posted on Monday, September 27, 2010 8:04 AM | Back to top


Comments on this post: Managed Extensibility Framework - Fun

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Ken Lovely, MCSE, MCDBA, MCTS | Powered by: GeeksWithBlogs.net