I'm creating a requirement specifications for 1 of my components, and I've found that people might be struggling with the new System.AddIns from .Net Framework 3.5. Here is a step by step walkthrough that I created to help to get you start with System.AddIns. There are several problems that I faced earlier, so please read it carefully, play around with the sample file and have fun.
1
Creating the Addin Contract
1.1
Add reference to System.AddIn.Contract
1.2
Add reference to the pipeline hint
Mine can be located at
C:\WINDOWS\assembly\GAC_MSIL\PipelineHints\1.0.0.0__3546f02f48709321\PipelineHints.dll
1.3
Add reference to for other assembly (TopCoder.Services.WCF.GenericNotes.dll)
1.4
Set the output project of your contract to the
output\Contracts
using System;
using
System.AddIn.Contract;
using
System.AddIn.Pipeline;
using System.Web;
using
System.Collections.Generic;
using
TopCoder.Services.WCF.GenericNotes.Entities;
//These
attributes indicate the assembly name we want to use for each of our components
[assembly: PipelineHints.SegmentAssemblyName(PipelineHints.PipelineSegment.HostView, "Hyperion.AspNet.Narratives.AddIns.HostView")]
[assembly: PipelineHints.SegmentAssemblyName(PipelineHints.PipelineSegment.AddInView, "Hyperion.AspNet.Narratives.AddIns.AddInView")]
[assembly: PipelineHints.SegmentAssemblyName(PipelineHints.PipelineSegment.HostSideAdapter, "Hyperion.AspNet.Narratives.AddIns.HostSideAdapters")]
[assembly: PipelineHints.SegmentAssemblyName(PipelineHints.PipelineSegment.AddInSideAdapter, "Hyperion.AspNet.Narratives.AddIns.AddInSideAdapters")]
namespace
Hyperion.AspNet.Narratives.AddIns.Contracts
{
/// <summary>
/// This interface defines the contract between the host and
the add-in, and
/// therefore cannot be versioned.
/// </summary>
[AddInContract]
public interface INarrativesContract
: IContract
{
IList<GenericNote> GetNarratives(Guid uniqueId);
void
SaveNarrative(GenericNote note, Guid uniqueId);
}
}
Note: make sure you can
build the solution and project successfully before running the pipeline builder
2
Running the pipeline builder
Go to Tool ->
PipelineBuilder
Once you have successfully
run the pipeline builder, you should see 4 new projects created in your
solution explorer
3
Set up the build location.
This
example, I put the output folder as a sibling folder to the project folder.
4
AddInView
Since I’m using the 3rd
party dll (GenericNote.dll), I have to set the local copy = true. Both host
project and AddInView projects are the only projects where you have to set the
local copy = true. You don’t need to change any thing on the local copy of auto
generated projects for addins.
If you get any errors, make
sure that all of the dependent components that GenericNote used are also copied
in the AddInView. I notice that generic note needs configuration manage,
exception manager, logging wrapper, object factory, self documenting exception
and wcf service. Therefore all of these dlls needs to go to the AddInView
folder as well.
5
References for GenericNote for other projects
Add references for other
addins projects that needed. However set the Local Copy = false
Make sure that the reference
to the GenericNote of the Contract project is set to Local Copy = false
6
Implementing the AddIn
Create a new project, and
add reference to your AddInView project. Make sure the local copy is set to
false. Also add a reference to GenericNote and also set the local copy = false.
Finally, add the reference to System.AddIn in order to make it work
The output of this project
must be placed under a AddIns folder under output folder, and then you create
another folder that descripe your addIns implementation. The folder name of
“ClaimantNarrative” don’t have to match as long as the parent folders are
correct
Don’t forget to mark the
AddIn with AddIn attribute.
And here is my code
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
using System.AddIn;
using
TopCoder.Services.WCF.GenericNotes.Entities;
namespace
Hyperion.AspNet.Narratives.Claimant{
[AddIn("ClaimantNarrative Addin", Description =
"ClaimantNarrative Addin Description",
Publisher = "Harry
Soetardjo", Version = "1.0.0.0")]
public class ClaimantNarrative
: Hyperion.AspNet.Narratives.INarratives
{
public IList<GenericNote>
GetNarratives(Guid uniqueId)
{
throw
new NotImplementedException();
}
public void SaveNarrative(GenericNote
note, Guid uniqueId)
{
throw
new NotImplementedException();
}
}
}
7
Host
Reference Settings:
Only need to reference to
HostView project. And set local copy = false
Set the reference of local
copy = true for the 3rd party dll (in this example, The
TopCoder.WCF.GenericNotes is being used as 3rd party dll)
The output of the Host
project must be pointed to the output folder specified for the AddIns,
therefore it needs to go to the “output folder”
The final set is to add the
referece to System.AddIn
Test the host by using this
following code (done in the console application). You can also use the web
application to be the host or even WCF service to be the host of the AddIns
using System;
using
System.AddIn.Hosting;
using
System.Collections.ObjectModel;
namespace Host
{
class Program
{
static void Main(string[] args)
{
// Set
the add-ins discovery root directory to be the current directory
string
addinRoot = Environment.CurrentDirectory;
//
Rebuild the add-ins cache and pipeline components cache.
string[]
results = AddInStore.Rebuild(addinRoot);
foreach
(string s in
results)
{
Console.WriteLine(s);
}
// Get
registerd add-ins of type SimpleAddInHostView
Collection<AddInToken> addins = AddInStore.FindAddIns(typeof(Hyperion.AspNet.Narratives.INarratives), addinRoot);
foreach
(AddInToken addinToken in addins)
{
//
Activate the add-in
Hyperion.AspNet.Narratives.INarratives addinInstance =
addinToken.Activate<Hyperion.AspNet.Narratives.INarratives>(AddInSecurityLevel.Internet);
//
Use the add-in
Console.WriteLine(String.Format("Add-in
{0} Version {1}", addinToken.Name, addinToken.Version));
addinInstance.SaveNarrative(null, Guid.NewGuid());
}
Console.WriteLine("Press any key to continue...");
Console.ReadLine();
}
}
}
Why my AddIns does not work, check these following items
1. If
you use any other dll, the class /entity must be marked as Serializable
2. For
Host project make sure you set the output of your project to the root of output
folder.
3. Make
sure that the Addins attributes are added correctly on the AddIns,
AddInSideAdapter, HostSiteAdapter
4. Make
sure all of the dependent component dll are available in the AddInViews as well
as root of output folder. For example all of the dependent components that
generic note used are also copied there.
References
- Pipeline
Builder - Download the latest one
http://www.codeplex.com/clraddins
- Different
Approaches to Add-In Discovery [Jesse Kaplan]
http://blogs.msdn.com/clraddins/archive/2007/01/26/different-approaches-to-add-in-discovery.aspx
- Reflection
Problem - Dependent Component
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2327543&SiteID=1
Download the project file
This download file is used for educational purpose only and provided as-is. I hope it helps you to get started with System.AddIns and enjoy it as much as I do.
System.AddIns.zip (515.21 kb)