Nov 29 2007

System.AddIn

Category: C# | TechEdfossmo @ 17:13

At TechEd some weeks ago, I went to a presentation about System.AddIn. It's a new cool addin framework for .NET. It's shipped with the .NET 3.5 framework. Making addIns and using them is much easier with this framework. To demonstrate how easy creating addins is, I will create a simple addin.

Here we go:

For this project I'm using Visual Studio 2008 Express. In the Express version, advanced build configuration is turned off by default. Lets turn it on:

In VS2008 Express choose Tools -> Options and then Show all settings.

SS-11.28.2007-12.54.05AM

Then choose Projects and solutions -> General -> Show advanced build configuration.

SS-11.28.2007-12.59.03AM

Now advanced build configuration is turned on and you can choose debug and release from the dropdown menu. Set it to debug.

SS-11.28.2007-01.03.16AM

I am going to create a console application. I'll name it TestAddIn.

In the Main method, I add the following line:

string addInPath = Environment.CurrentDirectory;

This line tells the application where to look for the addins.

Then I add references to this namespaces:

System.AddIn and System.AddIn.Contract

These two references have to be added to all the projects in this solution.

Next, I updates my addIns in store so the application is aware of the addins:

AddInStore.Update(addInPath);

To discover the addIns, add this line of code:

Collection<AddInToken> tokens = AddInStore.FindAddIns(typeof(MyTextHostView), addInPath);

The class MyTextHostView does not exist yet, so I have to create it. I add a new class library, and call it Host.View and add the following code:

public abstract class MyTextHostView
{
    public abstract string ViewText(string inputText);
}
 

SS-11.28.2007-05.21.51PM

Back in the main method, add:

foreach (AddInToken addinsToken in tokens)
{
     MyTextHostViewtextInstance =
        addinsToken.Activate<MyTextHostView>(AddInSecurityLevel.Internet);
     Console.WriteLine(textInstance.ViewText("My input text"));
}

The addin views define the base class for the add-ins. It is the way the addin receives the calls from the host. I create a class library called AddIn.Views and add this code:

[AddInBase]
public class MyTextAddinView
{
    public abstract string ViewText(string inputText);
}

SS-11.27.2007-12.55.59AM

Set the output path to ..\build\AddInViews.

SS-11.28.2007-05.30.33PM

Then it's time to create the first addin.

A new class library is required; AddIn.First. Add this code:

[AddIn("First addIn", Version="1.0.0.0")]
public class First:MyTextAddinView
{
    public override string ViewText(string inputText)
    {
        return "Addin 1: " + inputText;
    }
}


Set the output path to ..\build\AddIns\First\

Add a reference to the AddIn.View project, and make sure you set Copy Local = False.

SS-11.28.2007-05.52.55PM 

The interface between the host and the addIns, is expressed by a contract.

Create a class library called Contracts and add this code:

[AddInContract]
public interface IMyTextContract : IContract
{
    string ViewText(string name);
}

SS-11.27.2007-12.51.35PM

Set the output path to ..\build\Contracts\

An other class library is needed. Create a library called Host.Adapters. You know the drill; add the following code:

[HostAdapter]
public class MyTextContractToHostViewAdapter : MyTextHostView
{
    private IMyTextContract _contract;
    private ContractHandle _handle;

    public MyTextContractToHostViewAdapter(IMyTextContract contract)
    {
        this._contract = contract;
        _handle = new ContractHandle(contract);
    }

    public override string ViewText(string inputText)
    {
        return this._contract.ViewText(inputText);
    }

}

SS-11.28.2007-12.16.24AM

Add a reference to the Host.View project and Contracts project, and make sure you set Copy Local = False.

I also need a adapter on the addInside. Create a class library called AddIn.Adapter and add the code:

[AddInAdapter]
public class SimpleAddInViewToContractAdapter : ContractBase, IMyTextContract
{
    private MyTextAddinView _view;

    public SimpleAddInViewToContractAdapter(MyTextAddinView view)
    {
        this._view = view;
    }

    public string ViewText(string inputText)
    {
        return this._view.ViewText(inputText);
    }
}

SS-11.28.2007-06.06.03PM

Add a reference to the AddIn.View project and Contracts project, and make sure you set Copy Local = False.

Then it's time to test the project. Press F5, and run it.

SS-11.28.2007-06.11.30PM

I will write more in detail about this cool framework later.


Download project:

TestAddIn1.rar (19.56 KB)

Tags:

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Comments are closed