Integrating Integrators – BizTalk, Windows Azure, Windows Workflow, and Beyond
Join
Sign in
Search Options
Search Everything
Search BizTalk Blogs
Home
AppFabric
BizTalk Server
Windows Azure
Windows Workflow
Jobs (Hire A Guru)
More ...
Home
»
BizTalk Server
»
BizTalk Blogs
»
BizTalk Community Blogs via Syndication
»
Simple autogenerated facade WCF service with T4
Simple autogenerated facade WCF service with T4
BizTalk Blogs
This group is for blogs related to BizTalk Server. This includes Community Syndicated blogs and Stephen W. Thomas’s blog.
Get this RSS feed
Home
Blogs
Sitewide Application Navigation
Home
Blogs
Media
Forums
Wikis
Groups
Options
Share this
Monthly Archive List
Archives
May 2013
(51)
April 2013
(70)
March 2013
(65)
February 2013
(57)
January 2013
(79)
December 2012
(63)
November 2012
(68)
October 2012
(74)
September 2012
(66)
August 2012
(63)
July 2012
(77)
June 2012
(101)
May 2012
(64)
April 2012
(64)
March 2012
(68)
February 2012
(48)
January 2012
(34)
December 2011
(61)
November 2011
(44)
October 2011
(76)
September 2011
(66)
August 2011
(46)
July 2011
(66)
June 2011
(75)
May 2011
(58)
April 2011
(46)
March 2011
(65)
February 2011
(62)
January 2011
(76)
December 2010
(67)
November 2010
(140)
October 2010
(154)
September 2010
(143)
August 2010
(120)
July 2010
(86)
June 2010
(116)
May 2010
(91)
April 2010
(120)
March 2010
(98)
February 2010
(103)
January 2010
(107)
December 2009
(64)
November 2009
(118)
October 2009
(127)
September 2009
(89)
August 2009
(74)
July 2009
(115)
June 2009
(129)
May 2009
(134)
April 2009
(136)
March 2009
(161)
February 2009
(100)
January 2009
(107)
December 2008
(107)
November 2008
(106)
October 2008
(173)
September 2008
(146)
August 2008
(139)
July 2008
(101)
June 2008
(115)
May 2008
(120)
April 2008
(134)
March 2008
(104)
February 2008
(136)
January 2008
(106)
December 2007
(73)
November 2007
(135)
October 2007
(143)
September 2007
(138)
August 2007
(144)
July 2007
(139)
June 2007
(139)
May 2007
(166)
April 2007
(199)
March 2007
(200)
February 2007
(188)
January 2007
(182)
December 2006
(151)
November 2006
(149)
October 2006
(184)
September 2006
(147)
August 2006
(124)
July 2006
(125)
June 2006
(125)
May 2006
(89)
April 2006
(63)
March 2006
(83)
February 2006
(40)
January 2006
(42)
December 2005
(16)
November 2005
(32)
October 2005
(17)
September 2005
(29)
August 2005
(15)
July 2005
(11)
June 2005
(45)
May 2005
(39)
April 2005
(28)
March 2005
(14)
February 2005
(16)
January 2005
(18)
December 2004
(14)
November 2004
(13)
October 2004
(11)
September 2004
(26)
August 2004
(8)
July 2004
(9)
June 2004
(2)
May 2004
(2)
April 2004
(2)
March 2004
(2)
February 2004
(1)
Tags
.Net
.NET Framework
AppFabric
Architecture
ASP.NET
Azure
BizTalk
BizTalk 2006
BizTalk 2009
BizTalk 2010
BizTalk Server
Cloud
Community News
General
Microsoft
REST
SharePoint
SOA
Tellago
Uncategorized
Visual Studio
WCF
WCF/WF
Web Services
WF
BizTalk Community Blogs via Syndication
Numerous BizTalk Bloggers all in one spot. All content is property of the original blog owner.
RSS for posts
Simple autogenerated facade WCF service with T4
Rate This
Syndicated BizTalk Author
Thu, Jan 3 2013 6:50 AM
Comments
0
If you don't want to use a lot of money on service virtualization tools you can create a simple autogenerated WCF facade service that does a little bit of the things the big service virtualization tools does.
This gives you the option to hide service between one common service. The services behind this service could be a service from your backend system, a service from your CRM system and it might even be some external services.
You could do it manually, but then you would have to update it each time one of the backend services changes and with the human factor as an error source. Visual Studio gives you the option to use text templates which can be used to generate code.
Start by creating a WCF Service Application in Visual Studio and add a text template to this project, which will give you a *.tt file. This is the file that you will edit to create the autogenerated code. You might want to read a bit about it at MSDN before reading on.
Here is the link
.
Working with tt files is like going back in time. Back to the time with ASP and where you don't have intellisense. I stongly recommend to use Visual Studio 2012 that at least gives you a good debug option.
The first part of the tt file is the part where you specify which .Net namespace you want to use. The tt files knows nothing about the references that you have in your project, which I found out the hard way. My initial thoughts were to use reflection to get information about the services that I was going to work with, but I got into problems when I wanted to work with service references. So I ended up using the EnvDTE namespace, where you work with the FileCodeModel in Visual Studio. So this gives me this first part of the tt file:
<#@ template debug="true" hostspecific="true" #>
<#@ output extension="cs" #>
<#@ assembly name="EnvDTE" #>
<#@ assembly name="System.Xml" #>
<#@ import namespace="EnvDTE" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.Collections.Generic" #>
I wanted to control which services got autogenerated in an XML file, so I at a *.mapping file to the solution where I created a structure with the name of the service I wanted.
Based on this I could load the FileCodeModel and find the CodeFunctions that needed to be exposed.
<#
EnvDTE.DTE dte = GetEnvDTE();
string myFile = this.Host.ResolvePath("AutoGeneratedFacade.Mapping");
XmlDocument xdoc = new XmlDocument();
xdoc.Load(myFile);
foreach (XmlNode node in xdoc.SelectNodes(@"//class/name"))
{
string sourceFileName = node.InnerText;
sourceFileName = @"Service References\" + sourceFileName + @"\Reference.cs";
ProjectItem enumProjectItem = dte.Solution.FindProjectItem(sourceFileName);
FileCodeModel codeModel = enumProjectItem.FileCodeModel;
CodeNamespace codeNamespace = FindNamespace(codeModel.CodeElements);
CodeInterface codeInterface = FindInterface(codeModel.CodeElements);
List<CodeFunction> codeFunctions = FindMethods(codeInterface.Children);
#>
In the above code there are a couple of utility functions:
<#+
private CodeNamespace FindNamespace(CodeElements elements)
{
foreach (CodeElement element in elements)
{
CodeNamespace ns = element as CodeNamespace;
if (ns != null)
return ns;
}
return null;
}
private CodeInterface FindInterface(CodeElements elements)
{
foreach (CodeElement element in elements)
{
CodeInterface codeInterface = element as CodeInterface;
if (codeInterface != null)
return codeInterface;
codeInterface = FindInterface(element.Children);
if (codeInterface != null)
return codeInterface;
}
return null;
}
private List<CodeFunction> FindMethods(CodeElements elements)
{
List<CodeFunction> methods = new List<CodeFunction>();
foreach (CodeElement element in elements)
{
CodeFunction method = element as CodeFunction;
if (method != null)
methods.Add(method);
}
return methods;
}
private EnvDTE.DTE GetEnvDTE()
{
IServiceProvider hostServiceProvider = (IServiceProvider)Host;
if (hostServiceProvider == null)
throw new Exception("Host property returned unexpected value (null)");
EnvDTE.DTE dte = (EnvDTE.DTE)hostServiceProvider.GetService(typeof(EnvDTE.DTE));
if (dte == null)
throw new Exception("Unable to retrieve EnvDTE.DTE");
return dte;
}
#>
With the CodeFunctions in hand I am now able to create the new interface and class implementation for the autogenerated WCF facade service:
<#+
private void WriteInterface(CodeFunction method)
{
foreach(CodeElement element in method.Attributes)
{
string at = "";
CodeAttribute att = element as CodeAttribute;
if (att != null)
{
at = at + "[";
at = at + att.Name;
at = at + "(";
at = at + att.Value;
at = at + ")]";
WriteLine(at);
}
}
Write(method.Type.AsString);
Write(" ");
Write(method.Name);
Write("(");
string sparameter = "";
foreach(CodeElement element in method.Parameters)
{
int count = 0;
CodeParameter parameter = element as CodeParameter;
if (parameter != null)
{
sparameter =sparameter + parameter.Type.AsString + " ";
sparameter = sparameter + parameter.Name;
if(count < method.Parameters.Count)
sparameter = sparameter + ", ";
count++;
}
}
sparameter = sparameter.Substring(0,sparameter.Length -2);
Write(sparameter);
Write(");");
}
#>
And with the interface I am able to create the class the implements the interface:
<#+
private void WriteServiceClass(CodeFunction method, string servicename)
{
Write("public ");
Write(method.Type.AsString);
Write(" ");
Write(method.Name);
Write("(");
string sparameter = "";
string sparameternames = "";
foreach(CodeElement element in method.Parameters)
{
int count = 0;
CodeParameter parameter = element as CodeParameter;
if (parameter != null)
{
sparameter =sparameter + parameter.Type.AsString + " ";
sparameter = sparameter + parameter.Name;
sparameternames = sparameternames + parameter.Name;
if(count < method.Parameters.Count)
{
sparameter = sparameter + ", ";
sparameternames = sparameternames + ", ";
}
count++;
}
}
sparameter = sparameter.Substring(0,sparameter.Length -2);
Write(sparameter);
Write(")");
WriteLine(String.Empty);
WriteLine("{");
PushIndent(" ");
Write("FacadeService.");
Write(servicename);
Write(".");
Write(servicename);
Write("Client service = new FacadeService.");
Write(servicename);
Write(".");
Write(servicename);
Write("Client();");
WriteLine(String.Empty);
Write("return service.");
Write(method.Name);
sparameternames = sparameternames.Substring(0,sparameternames.Length -2);
Write("(");
Write(sparameternames);
Write(");");
WriteLine(String.Empty);
ClearIndent();
PushIndent(" ");
WriteLine("}")
}
#>
The final piece is to change the SVC file to point to the autogenerated class with changes to the Service and Codebehind attributes.
I used this
article
as inspiration and borrowed some lines of code from it.
With this I just have to update the service proxy and save the tt file to update my facade service and I don't have to write copy paste code.
Random posts:
Nikon autofocus system
SQL Server, convert to and text from image field
Superliga opsamling runde 11
Welcome to app construction
Welcome to Office Power blog
Help me, help you. You know how ;-) (click, click, click...)
Read the complete post at
feedproxy.google.com/.../simple-autogenerated-facade-wcf-service.html
Windows Communication Foundation