-
Notifications
You must be signed in to change notification settings - Fork 158
Example 08
###Extensibility: Writing a simple extension In this example we will create some simple extensions (plugins). Backload uses .NET MEF (Managed Extensibility Framework) introduced in .NET4 to provide the extensibility feature. Extensions must be within the ~/bin/Extensions folder resp. a subfolder and can be dynamically added or removed while the application is running. After the extensibility feature is fully implemented (Backload version 2.0 and above) it will provide several extension points through the whole processing pipeline, from the incoming request to the outgoing response. Multiple extensions can handle a single extension point and may all influence the further processing within the pipeline. Each interfaces in the Backload.Contracts.dll assembly corresponds to a specific extension point. When a specific extension point is reached within the processing pipeline, Backload looks in the Etensions folder if there is one or more extensions that are derived from this specific interface. If there are any, the extension is instantiated, properties are filled by the pipline and a processing method is called within the extension. After the extension is finished (maybe other extensions are called) the pipeline reads the values from the extension and uses it for further processing. All you need to do is to derive a class from an appropriate interface (e. g. Backload.Contracts.IIncomingRequest), mark this class with the System.ComponentModel.Composition.ExportAttribute (e. g. [Export(typeof(IIncomingRequest))]), do some processing, build and copy the assembly into the Extensions folder (together with the Backload.Contracts.dll).
####3 steps to develop a basic extension
- Create a class library and derive a class from an appropriate interface from Backload.Contracts.dll
- Mark your class with the System.ComponentModel.Composition.ExportAttribute (e.g. [Export(typeof(IIncomingRequest))])
- Build the project and copy the dll into the ~/bin/Extensions folder of your application (The assembly must have the naming schema Backload.Extension.*.dll).
####Example 1: A simple extension In this example we use the subproject "Extension01". This extension will handle requests immediately after the request comes in. The purpose of this plugin is to refuse further processing if an artist is not in the database (Only on uploads, httpMethod=POST).
namespace Backload.Extension.Contoso { [Export(typeof(IIncomingRequest))] public class IncomingRequest : IIncomingRequest { public string ObjectContext { get; set; } public string UploadContext { get; set; } public string QueryFileName { get; set; } public string QueryExtraFolder { get; set; } public StopProcessingType StopProcessing { get; set; } public string ProcessingMessage { get; set; } public List Logger { get; set; } public bool ProcessStep(System.Web.HttpRequestBase request, string httpMethod) { if (httpMethod == "POST") { // Validate if the artist is in the database using (var artists = new ArtistsLibrary()) { var artist = artists.Artists.FirstOrDefault(a => a.ArtistId == this.ObjectContext); if (artist == null) // Artist not in list { // Stop further processing of the pipeline but all extensions can do their work (maybe logging, etc.). this.StopProcessing = StopProcessingType.StopProcessingPipelineOnly; this.ProcessingMessage = "Artist not in list"; return true; // Return value is true, because the extension has changed properties. } } } return false; // No properties have been changed, so false is returned. } } }
####Example 2: Using Logger to access extensions called so far. In this example we use the Logger property to get some information about extensions called so far (if any). Add the existing project Extension02 into the solution (as its unloaded so far), set a breakpoint, run the application and upload a file.
namespace Backload.Extension.Contoso { [Export(typeof(IIncomingRequest))] public class MultipleDemo : IIncomingRequest { public string ObjectContext { get; set; } public string UploadContext { get; set; } public string QueryFileName { get; set; } public string QueryExtraFolder { get; set; } public StopProcessingType StopProcessing { get; set; } public string ProcessingMessage { get; set; } public List Logger { get; set; }public bool ProcessStep(System.Web.HttpRequestBase request, string httpMethod) { if (httpMethod == "POST") { // UploadContext is a subfolder of ObjectContext. // A tree of subfolders can be achieved by seperating the subfolder names with a semicolon. // When there are multiple extensions manipulating the UploadContext or any other property, // all can contibuting to the value returned to the processing pipline. if (string.IsNullOrEmpty(this.UploadContext) == false) this.UploadContext += ";"; this.UploadContext += "subfolder_" + Logger.Count.ToString(); // Logger lists all extensions call so far return true; // Return value is true, because the extension has changed properties. } return false; // No properties have been changed, so false is returned. } }
}
####Example 3: Multiple extensions dynamically added/removed In this example you'll learn how dynamically adding or removing extensions is supported. Be sure you have a breakpoint within the MultipleDemo.cs (Extension02). Do not stop the application, go to the ~/bin/Extensions folder and copy the Backload.Extension.MultipleDemo.dll. Make sure the copies have the correct naming schema (Backload.Extension.*.dll). When you upload a file, the breakpoint will now be called multiple times with every instance of an extension. Each extension now adds a new subfolder to the UploadContext property (which is the purpose of this extension we created).
Don't forget, to include Backload.Contract.dll in the ~/bin/Extensions folder.
####Conclusion In this example we showed you how easy it is to create your own extensions to the Backload component. We also used the Logger property to get information about extensions called so far. Finally we added (deleted/renamed) multiple extensions dynamically to the Extensions folder. All where called in the request and contributed to Backloads processing pipeline.
Code examples: Example08
Backload. (Standard version): Copyright 2013, Steffen Habermehl, License (Standard version): MIT license
Professional and Enterprise (source code) version are available under a commercial license.
Follow us on Twitter: @Backload_MVC