Проблема Visual Studio 2010 с .Net T4 - Host.LoadIncludeText не интерпретирует код - PullRequest
2 голосов
/ 15 июля 2011

У нас есть файлы app.config для развертывания в 8 различных средах, например, Dev, Local, Test, UAT, Prod и т. Д. Чтобы упростить изменения, каждый верхний уровень .tt выглядит следующим образом:

например.

app.dev.tt

<#
    EnvironmentName = @"Dev";
#>
<#@ include file="CommonTemplate.ttinclude" #>

CommonTemplate.ttinclude включает в себя:

<#@ template language="C#" hostspecific="True" #>
<#@ output extension="config" #>
<#@ include file="..\MasterConfig\MasterConfig.ttinclude" #>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   ...
</configuration>

MasterConfig.ttinclude имеет что-то вроде этого:

   <#
        string hostPath = this.Host.ResolvePath("..\MasterConfig");
        string EnvironmentIncludePath = string.Format(@"{0}\EnvironmentSettings\{1}.ttinclude", hostPath, EnvironmentName);

        EnvironmentIncludePath = System.IO.Path.GetFullPath(EnvironmentIncludePath);
        string context = null;
        string resolvedName = null;
        Host.LoadIncludeText(EnvironmentIncludePath, out context, out resolvedName);

        public string ServicesMachineName = "";

Обычно он ищет файл с именем MasterConfig \ EnvironmentSettings \ Dev.ttinclude и динамически включает его.

и содержимое Dev.ttinclude выглядит так:

<#
    ServicesMachineName = "SOMESERVERNAME";
        ... [snip other setting specific stuff stuff]
#>

Хотя кажется, что все работает (и работало на VS 2008), проблема в том, что код в Dev.ttinclude не выполняется, поэтому ServicesMachineName никогда не устанавливается.

Текущее решение:

Это работает, но не так элегантно или динамично, как другое решение, но мы не можем понять, почему LoadIncludeText больше не работает с момента перехода с VS 2008.

switch(EnvironmentName.ToUpper().Trim())
    {
        case "DEV":
            #><#@ include file="..\MasterConfig\EnvironmentSettings\Dev.ttinclude" #><#
            break;
        case "LOCAL":
            #><#@ include file="..\MasterConfig\EnvironmentSettings\Local.ttinclude" #><#
            break;
        default:
            throw new Exception("Error the specified Environment Name was not found");
    }

Вопрос:

Что-то не так с Host.LoadIncludeText или функциональность изменилась между VS 2008 и VS 2010?

...