【发布时间】: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