【问题标题】:How to use custom sections in app.config from a t4 template如何从 t4 模板使用 app.config 中的自定义部分
【发布时间】:2011-01-25 10:31:26
【问题描述】:

我正在尝试从 VS2010 中的 t4 模板访问 app.config 文件中的自定义部分,但无法加载定义自定义部分的程序集。

我正在使用 ConfigurationAccessor 获取该部分(参考 http://skysanders.net/subtext/archive/2010/01/23/accessing-app.configweb.config-from-t4-template.aspx)。

app.config:

<configSections>
    <section name="MyProviders" type="System.Web.Security.MySection, MyAssembly" />
</configSections>

<MyProviders default="SQLMyProvider">
  <providers>
    <add name="SQLMyProvider" ... connectionStringName="MyConnectionString" />
  </providers>
</MyProviders>

在 .tt 文件中调用这一行:

MySection section = (MySection)config.Configuration.GetSection("MyProviders");

给出这个错误:

运行转换: System.Configuration.ConfigurationErrorsException: 创建时出错 配置节处理程序 MyProviders:无法加载文件或 程序集“MyAssembly”或其之一 依赖关系。系统找不到 指定的文件。

.tt 文件引用程序集,项目也是如此,但这似乎无助于加载配置部分。 我尝试将 MySection 类实现为 tt 代码块,但也无法在 app.config 中使用该类。

有什么想法吗?

【问题讨论】:

  • 您尝试在哪里检索自定义部分?我在使用不同的 appdomains 时遇到了同样的麻烦。我的问题是我的部分 dll 必须放在基本目录中,以便 appdomain 可以正确探测。

标签: c# .net visual-studio-2010 .net-4.0 t4


【解决方案1】:

我认为&lt;configSections&gt; 需要完整的程序集名称(版本、文化、publickeytoken)

type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"

【讨论】:

    【解决方案2】:

    您需要将MyAssembly 放在GAC 或Visual Studios PublicAssemblies 文件夹中才能找到它。后者的位置是&lt;install_dir&gt;\Common7\IDE\PublicAssemblies

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-28
      • 2020-10-12
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-11
      相关资源
      最近更新 更多