Wednesday, February 3, 2010

Add-In Pipeline Development Part-2

In my previous blog we looked into the architecture of the Add-In Pipeline , now its time to see practical implementation . To build a Add-In project we need following components of pipeline:-


  • Host
  • Host Views
  • Host Side Adapter
  • Contract
  • Add-in Side Adapter
  • Add-In Views
  • Add-In

We create a project for each of the above components. We also need a directory structure to implement our pipeline and for discovery of our Add-ins. Lets start with creation of directory structure, we create following folders to hold our pipeline assemblies.

  • [AddIns] :- folder to hold our add-in assemblies.Inside the folder all the add-in assemblies will be kept inside the subfolder having same name as add-in assembly.
  • [AddInSideAdapters] :- folder to hold the add-in side adapters assembly.
  • [AddInViews] :- folder to hold the add-in views assembly.
  • [Contracts] :- folder to hold the contracts assembly.
  • [HostSideAdapters] : - folder to hold host side adapter assembly.












Host and the host view assembly resides along with above folders without any subfolder.We start with creating a Contract Interface assembly :-

  • Add a new class library project to a new solution in visual studio.
  • Add reference of the assembly System.AddIn.Contract.
  • Add an interface code file from Add new project dialog box.Name the Interface.
  • Add using statement for System.AddIn.Contract and System.AddIn.Pipeline namespace to the Interface code file.
  • Implement IContract interface in the contract interface.
  • Add attribute [AddInContract] to the Interface.
  • Define the members of the interface. The members define the communication protocol to use between Add-In and Host.
[Click Image to see in full]









Here I define a rather simple contract , purpose is to show the basic setup for pipeline project.Although project can be scaled according to your requirments.

Next is the Views for Host and the Add-In . Host View :-

  • Add a new class library project to the same solution in visual studio.
  • Add reference of the assembly System.AddIn.Contract.
  • Add an interface code file from Add new project dialog box.
  • Define members for the Interface .Views have same members as the Contract Interface.

[Click Image to see in full]

In host view no attributes are defined . Add-In view assembly and Host view assembly are similar excepty for the attribute applied to them.

Add-In View :-

  • Add a new class library project to a new solution in visual studio.
  • Add reference of the assemblies System.AddIn.Contract and System.AddIn.
  • Add an interface code file from Add new project dialog box.Name the Interface.
  • Add using statement for System.AddIn.Pipeline namespace.
  • Add attribute [AddInBase] to the interface.
  • Define members for the Interface .Views have same members as the Contract Interface.
[Click Image to see in full]


Next is the HostSide Adapter .It will convert the Contract to the Host View :-

  • Add a new class library project to a new solution in visual studio.
  • Add reference of the assemblies System.AddIn.Contract , System.AddIn, Host View assembly and Contract assembly .
  • Add an class code file from Add new project dialog box.Name the adapter class.
  • Add using statement for namespace System.AddIn.Contract,System.AddIn, Host View namespace and Contract namespace.
  • Implement the Host View interface in adapter class.
  • Add attrinute [HostAdapter] to the adapter class.

    [Click Image to see in full]

Add-In side adapter converts the view to contract.
Add-In side Adapter:-

  • Add a new class library project to a new solution in visual studio.
  • Add reference of the assemblies System.AddIn.Contract , System.AddIn, Add-In View assembly and Contract assembly .
  • Add an class code file from Add new project dialog box.Name the adapter class.
  • Add using statement for namespace System.AddIn.Contract,System.AddIn, Add-In View namespace and Contract namespace.
  • Implement the Contract interface in adapter class and inherit the ContractBase class.
  • Add attrinute [AddInAdapter] to the adapter class.

[Click Image to see in full]

Now lets create Add-In :-

  • Add a new class library project to a new solution in visual studio.
  • Add reference of the assemblies System.AddIn.Contract , System.AddIn and Add-In View assembly.
  • Add a class code file from Add new project dialog box.Name the Add-In class.
  • Add using statement for namespace System.AddIn.Contract,System.AddIn, Add-In View namespace .
  • Implement the Add-In view interface in add-in class.
  • Add attrinute [AddIn("EmailAddIn")] to the addin class.

[Click Image to see in full]


Finally our host that probes for the add-in and activates the add-in to consume its services.
Host:-

  • Add a new class library project to a new solution in visual studio.
  • Add reference of the assemblies System.AddIn.Contract , System.AddIn and Host View assembly.
  • Add a class code file from Add new project dialog box.Name the Add-In class.
  • Add using statement for namespace System.AddIn.Hosting, System.AddIn, Host View namespace .

[Click Image to see in full]











Host provides the code for constructing our pipeline for the communication and add-in discovery. We are assuming that our pipeline directory is in the root directory of the solution. Host provide the code for discovery of add-in and the way to activate them

We use the AddInStore class to build our pipeline directory (AddInStore.Rebuild(Path)) . After our add-in pipeline is built we use AddInStore class to find all the add-ins in the pipeline add-in directory (AddInStore.FindAddIns(typeof(IEmailHostView), Path)) . FindAddIns method returns the collection of the AddInToken type which we use to activate any particular add-in .

AddInToken addInToken = addInTokens[0];
this.emailHostView = addInToken.Activate(AddInSecurityLevel.Internet);

Activating the addintoken returns the Host view interface. Now with the host view returned by token activation we can use the service of the add-in. To see all the thing in action we have to build all the assemblies and place them in there respective folders in Pipeline directory. As previously stated that Host and HostView assemblies resides along with all other pipeline folders and are not in there seperate folders. Now if we launch our host and call the add-in discovery and activation method , we can use the add-in service.

Host hst = new Host();

hst.GetEmailAddIn();

hst.ReceiveMessage("Message");

hst.SendMessage();

That's it . We can build Extensible application with Add-In pipeline development and provide our own sdk to the add-in developers to code add-ins for our Host application. In coming blogs I will like to blog about Managed Extensibility Framework as released with .Net 4.0. It's another way to build extensible application using composition containers and dependency injection.

No comments:

Post a Comment