I have a place in my application that I'm building on SoapBox Core, and I want 3rd parties to be able to extend this point with their own custom logic.

asked 01 Jun '10, 21:34

Scott%20Whitlock's gravatar image

Scott Whitlock ♦♦
696262833
accept rate: 50%


Importing and Executing a List of IExecutableCommands

In order to import a list of commands, you need a reference to the Extension Service, and you will probably want a reference to the Logging Service as well, in case something goes wrong with the commands and you want to log the error. Of course you also need to get a reference to the commands themselves. Use an Import Attribute in a class that exports itself and is somehow already imported by SoapBox Core. Here is an example:

[Import(SoapBox.Core.Services.Logging.LoggingService, typeof(ILoggingService))] 
private ILoggingService logger { get; set; }

[Import(SoapBox.Core.Services.Host.ExtensionService, typeof(IExtensionService))] 
private IExtensionService extensionService { get; set; }

[ImportMany(ExtensionPoints.MyProject.MyClass.CommandsToExecute, 
            typeof(IExecutableCommand), AllowRecomposition=true)] 
private IEnumerable<IExecutableCommand> commandsToExecute { get; set; }

private void ExecuteCommands()
{
    foreach(IExecutableCommand cmd in extensionService.Sort(commandsToExecute))
    {
        try
        {
            cmd.Run();
        }
        catch (Exception e)
        {
            logger.Error("Exception while running command " + cmd.ID, e);
        }
    }
}
link

answered 01 Jun '10, 21:35

Scott%20Whitlock's gravatar image

Scott Whitlock ♦♦
696262833
accept rate: 50%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×23
×2
×1

Asked: 01 Jun '10, 21:34

Seen: 1,871 times

Last updated: 01 Jun '10, 21:35

powered by OSQA