Tag: .NET

Dynamic Plugin Loading Using MEF

The Managed Extensibility Framework (MEF) is a library that enables software to discover and load libraries at runtime without hard-coded references. Microsoft included MEF in .NET framework version 4.0 and since then it has been commonly used for dependency resolution and inversion of control patterns.

Orbital Bus makes communication possible between different parties by sharing contract and schemas. A receiver has a contract library that has all the information needed for a dispatcher to make proper synchronous and asynchronous calls all the way to an end consumer. The dispatcher downloads a receiver’s contract library and then uses it to construct calls with the right data schemas. It became very clear to us during development that a crucial requirement was that the dispatcher to be able handle any downloaded contract library DLL and process it without code changes. This is where MEF comes into play. It lets us inject libraries, in this case the receiver’s contract libraries, at the start-up stage.

Once we chose to use MEF as our integration tool, we were able to start the Code Generation Project. This project is a convenient CLI tool that efficiently generates the contract libraries and plugins which are loaded by the receiver. These libraries are made available for download to any dispatcher on the mesh network. One challenge we encountered downloading multiple contract libraries for the dispatcher was how to distinguish between these contract libraries. What if two contracts have similar operation names? How can the dispatcher tell what is the right operation to select from its composition container? We were able to solve this challenge by making sure that each contract library generated has a unique ServiceId that would be exported as metadata within the contract library. This setting enables the dispatcher to filter out various operations based on their ServiceId:

    namespace ConsumerContractLibrary
    {
        [ExportMetadata("ServiceId", "ConsumerLibrary")]
        public class AddCustomerOperation : IOperationDescription {}
    }

When the receiver starts up, it will pull the plugins from its Plugins folder and load the plugin.dll and adapters into MEF’s CompositionContainer, a component used to manage the composition of parts. Those dependencies will be injected into the receiver as it loads. In addition to handling messages destined for the consumer, the receiver also serves as file server that waits for the dispatcher to download the contract library when needed.

    public PluginLoader(IConfigurationService config)
    {
        this.config = config;
        var container = this.BuildContainer(); // load the plugin DLLs and create composition container
        this.RegisterAdapters(container);
        var details = this.RegisterPlugins(container);
        this.BootStrapSubscriberDetails(details); //Creates needed dependencies and bootstraps the given details.
    }

After a dispatcher downloads the available contract library specifications into a composition container, it will filter out and return all the exported values in the container corresponding the given ServiceId.

    public static IEnumerable<T> GetExportedValues<T>(this CompositionContainer container,
            Func<IDictionary<string, object>, bool> predicate)
    {
        var exportedValues = new List<T>();

        foreach (var part in container.Catalog.Parts)
        {
            foreach (var ExportDef in part.ExportDefinitions)
            {
                if (ExportDef.ContractName == typeof(T).FullName)
                {
                    if (predicate(ExportDef.Metadata))
                        exportedValues.Add((T)part.CreatePart().GetExportedValue(ExportDef));
                }
            }
        }

        return exportedValues;
    }

Where the predicate clause is actively the filter we need for ServiceId:

    metadata => metadata.ContainsKeyWithValue(METADATAKEY, serviceId)

After filtering the process, the dispatcher has all the contract library operations that are supported by the receiver.

MEF proved invaluable in solving the problem of runtime library integrations and to enable the plugin architecture. This implementation allows Orbital Bus the flexibility for developers to customize or update their contract libraries, service configurations, and translations without affecting other services on the bus. As our work continues, we plan on looking closer at the issue of versioning in the dispatcher to keep its cache in sync with the receiver’s contract libraries, making Orbital Bus an even more agile messaging solution.

  • Yi
  • Luo

Why .NET Doesn’t Have to be Expensive

.NET is a proven and mature framework that has great benefits, however it is often overlooked when companies are deciding on a language and framework. Many developers remember the Microsoft of old where you were immediately stuck with proprietary frameworks and Microsoft-only products that have high initial costs and outrageous scaling overhead. Fortunately for the industry, Microsoft is taking a sharp turn away from proprietary restrictions and is moving towards open source.

Let’s examine CheapMumble, a project which has been successfully deployed using .NET with no licensing costs. I’ll take a look at the frameworks, software, and hosting that has been used to make his project successful. I’ll also explore other options and the future of .NET in the open source world.

The CheapMumble Project

To understand what CheapMumble does, you first need to understand what mumble is. From the Mumble wiki: “Mumble is an open source, low-latency, high quality voice chat software primarily intended for use while gaming.” CheapMumble is simply a cheap hosting solution for mumble servers.

Take a look at the software stack used to create CheapMumble.

Front End

Razor (Open Source)

The beloved view-rendering engine of MVC.Net has been open sourced and has been freely available for some time.

Application Tier

.NET Framework 4.5

The same framework you read about or are familiar with, including Async Await, Linq, and all other features.

Mono (Open Source)

The team was able to use the framework by choosing the ever growing project Mono. At the time this article is written, Mono just recently released version 3.6.0. If you want to know about compatibility take a look here.

Nancyfx (Open Source)

Nancy is the web framework chosen to drive CheapMumble. You may have never heard of it, however it’s a full featured web framework ready to be used in any of your next web projects. The great thing about Nancy is the firm dedication of support for the Mono libraries. Take a look at their blog to learn more and see what they are up to.

Entity Framework (Open Source)

Don’t compromise on your data access. Use the best ORM out there (and yes I’ll fight you on that). Entity Framework has been open sourced for a long time and has great support under the Mono framework. Linq your hearts out on your next project.

Backend

MySQL (Open Source)

Entity Framework allows you to connect to any relational database you please, including MySQL, using the .NET Connector. Setup is easy and you will forget about your database while using code first features and strong object relational models.

Software during development can be a substantial cost if you’re not careful. Especially if you consider the cost of Visual Studio Ultimate MSDN subscriptions. Visual Studio is the best development IDE out there, however do you really need all its features? Let’s take a look at some cheaper alternatives.

Visual Studio Express

Free Visual Studio! What could go wrong? I’d love to tell you this is the solution to all your problems. It isn’t. They have, however, added a lot to the express editions over the years. Ability for multi-project solutions, unit testing, NuGet, code analysis. Trying to find the limitations online was not easy, and I didn’t find a reliable source. I would recommend giving it a shot. See what happens. It could very well be all your team needs.

Xamarin / MonoDevelop

MonoDevelop evolved into Xamarin whether on Windows or Mac. Don’t get scared by the price tag. The only price for Xamarin is when you want to compile source code to work with Android or iOS in a closed-source application. This means that all web applications can be developed free of use on Xamarin.

Sublime Text

Wait, really? Though sublime isn’t a full, feature-rich IDE, it is still a very strong candidate for a lot of developers. Recently on the Nancyfx blog they went through a tutorial on setting up Sublime to work with ASP.Net development.

With these technologies, the CheapMumble team was able to develop and deploy their software on whatever platform they saw fit. The best part was that no licensing cost was required.

The future of open source on the .NET framework is bright. Everything in this post works today, and tomorrow there will be even more. Recently, Microsoft unveiled ASP.Net vNext with a large amount of the software being open source. A great rundown of features was given from Scott Hanselman in his post Introducing ASP.NET vNext. The most exciting part is at the end:

ASP.NET vNext (and Rosyln) runs on Mono, on both Mac and Linux today. While Mono isn’t a project from Microsoft, we’ll collaborate with the Mono team, plus Mono will be added to our test matrix. It’s our aspiration that it “just work”.

The future for ASP.NET development is clear: Open Source and CHEAP!

  • Dan
  • McCrady