Add assembly reference from code template

Topics: Service Factory Modeling Edition Forum
Nov 19, 2007 at 12:57 PM
I'm adding extra code to the service implementation template. For this extra code I need a reference to an extra assembly. How can I let service factory automaticly do this without making changes to the source?

Throughout the templates a method named "AddProjectReference" is used, that seems quite similar to what I need. Is this method comming from the custom texttemplate host? Is variant for adding assembly references?


Nov 19, 2007 at 5:14 PM
Hi Gerben,

In order to add a new assembly to your only need to add at the top of the tt file something like:

<#@ Assembly Name="[your asm name], version/Culture/PublicKeyToken" #>
along with the import statement like:

<#@ Import Namespace="[your namespace]" #>
Regarding "AddProjectReference", this method (as its name describes) is used to add only project references to the project that will get the new code and as you said, this comes from the tt host "Microsoft.Practices.Modeling.CodeGeneration.Strategies.TextTemplating.TextTemplateHost".
Nov 20, 2007 at 9:27 AM
Thanks for the reply Charly, but this doesn't solve my problem. Let me give an example:

Suppose I need to do things with the System.Drawing dll in my service. So I add this to the template:

<#@ Assembly Name="System.Drawing" #>
<#@ Import Namespace="System.Drawing" #>

Generating code that uses System.Drawing now works fine. But compiling this generated code doesn't work since the project that I generated code to doesn't contain a reference to this assembly. So how could I get a reference automatically in the project to which I generate code?


Nov 20, 2007 at 9:00 PM
Try adding the full assembly name like:
<#@ Assembly Name="System.Drawing, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" #>
Then build all the sln and that should add the reference.
Nov 21, 2007 at 7:08 AM
No this is still not adding a reference to the project to which I generate the code. Any other ideas?


Nov 26, 2007 at 12:58 PM
The way to do this is just adding an "AssemblyReference" attribute to the artifact link that handle the code generation for your target project, in this case the "ServiceContractLink" class in the WCF extender library (Microsoft.Practices.ServiceFactory.Extenders.ServiceContract.Wcf). You may only update this class with the attrubute as in the following example and re build this project, copy del dll to the package folder and you're done.

[AssemblyReference("System.Drawing, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public sealed class ServiceContractLink : ArtifactLink
Nov 26, 2007 at 4:13 PM
Works fine, thanks!