现在的位置: 首页 > 综合 > 正文

使用T4 模板来自动生成WCF 代理类 代码

2013年12月07日 ⁄ 综合 ⁄ 共 3893字 ⁄ 字号 评论关闭

先看一下代码,下面这个代码是生成WCF代理类的一个模板,根据契约接口生成一个调用WCF的代理类。

vs 2012 新建一个 BaseDataProxyService.tt  文件,代码如下

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="$(ProjectDir)bin\debug\Quantum.PowerTMS5.ServiceHost.dll" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="Quantum.PowerTMS5.ServiceHost" #>
<#@ import namespace="Quantum.PowerTMS5.Service" #>
<#@ output extension=".cs" #>
<#@ include file="..\..\Common.tt"#>
// ******************************************************
// This file was generated by the T4 engine and the 
// contents of this source code will be changed after save t4 file
// create by gavin
// ******************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quantum.FrameWorkCore.WCFEx;
using System.ServiceModel;
using Quantum.FrameWorkCore.ExceptionEx;
using Quantum.ProxyService.AsynLog;
using Quantum.PowerTMS5.DTO;
using Quantum.PowerTMS5.ServiceHost;
using Quantum.PowerTMS5.Service;
using Quantum.FrameWorkCore.DynamicSearch;

namespace Quantum.ProxyService.PowerTMS5.Configuration
{
	public class BaseDataProxyService 
	{
		
		public static BaseDataProxyService GetInstance()
        {
            return new BaseDataProxyService();
        }
		
		<#foreach (MethodInfo method in typeof(IService1).GetMethods()) {#>
		public <#=GetMethodReturnType(method)#> <#=method.Name#>(<#=GetMethodParameterList(method)#>)
		{
			ServiceInfo<IService1> service = FindService.BasicHttpBinding<IService1>();
			try
			{

				<#if(method.ReturnType != typeof(void)){#>return<#}#> service.t.<#=method.Name#>(<#=GetMethodParameterValueList(method)#>);
			}
			catch (FaultException<UserFriendlyError> ufe)
            {
                if (ufe.Detail.HintLevel == ExceptionLevel.Business)
                {
                    AsynLogServiceProxy.GetInstance().WriteExceptionLogInfo(ufe, "");
                    throw new Exception("未知错误");
                }
                else
                {
                    throw ufe;
                }
            }
			catch (Exception ex)
            {
                AsynLogServiceProxy.GetInstance().WriteExceptionLogInfo(ex, "");
                throw new Exception("未知错误");
            }
            finally
            {
                service.Close();
                AsynLogServiceProxy.GetInstance().WriteOperationLogInfo("");
            }
		}
<#}#>

	}
}

简单说下方法,如果开发过asp的人或者mvc的人人,应该对这种模式比较清楚。

先下载一个工具 t4 editor,这个工具有助于你编写模板,比如高亮关键字,自动收缩

安装完后,对下面几处做一个解释就明白了

output extension=".cs"

这个是指模板生成的文件后缀名,你可以随便写

<#@ assembly name="$(ProjectDir)bin\debug\Quantum.PowerTMS5.ServiceHost.dll" #>
<#@ import namespace="Quantum.PowerTMS5.ServiceHost #>

assembly 是加载程序集,比如你这个t4 模板要使用你自己写的一个dll,就得先加载进来,然后用import 引用进来,import 等于using的意思

可以使用 $(variableName) 语法引用 Visual Studio 或MSBuild变量(如 $(SolutionDir)),以及使用 %VariableName% 来引用环境变量。

常用的有:

$(ConfigurationName)

当前项目配置的名称(如“Debug”)。

$(PlatformName)

当前项目平台的名称(如“Win32”)。

$(ProjectName)

项目的基本名称。

$(TargetDir)

生成的主输出文件的目录(定义为驱动器 + 路径);包括尾部的反斜杠“\”。

$(TargetName)

生成的主输出文件的基本名称。

$(FrameworkDir)

安装 .NET Framework 的目录。

$(FrameworkVersion)

Visual Studio 使用的 .NET Framework 版本。

$(WebDeployPath)

从 Web 部署根到项目输出所属于的位置的相对路径。返回与RelativePath相同的值。

$(WebDeployRoot)

指向<localhost>位置的绝对路径。例如,c:\inetpub\wwwroot。

// ******************************************************
// This file was generated by the T4 engine and the 
// contents of this source code will be changed after save t4 file
// create by gavin
// ******************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quantum.FrameWorkCore.WCFEx;
using System.ServiceModel;
using Quantum.FrameWorkCore.ExceptionEx;
using Quantum.ProxyService.AsynLog;
using Quantum.PowerTMS5.DTO;
using Quantum.PowerTMS5.ServiceHost;
using Quantum.PowerTMS5.Service;
using Quantum.FrameWorkCore.DynamicSearch;
 
namespace Quantum.ProxyService.PowerTMS5.Configuration
{

这些东西就是原始输出了,你写什么,模板就照搬出来

<#foreach (MethodInfo method in typeof(IService1).GetMethods()) {#>

这个是循环控制块,循环输出代理方法 

有三种类型的控制块,根据其左括号对它们进行区分:

1.      <# 标准控制块 #>                            可以包含语句。

2.      <#= 表达式控制块 #>            将一个可以计算为字符串的表达式括起来,用于提供要写入“输出”文件的字符串的代码。

3.      <#+ 类功能控制块 #>            可以使用类功能控制块向文本模板添加方法、属性、字段甚至是嵌套类。必须作为文件中的最后一个块显示,或者用<#@ include #>引入外部文件。

好了,你自己可以试试了,简单的入门,呵呵

抱歉!评论已关闭.