开发者

How to use custom sections in app.config from a t4 template

开发者 https://www.devze.com 2023-02-06 20:04 出处:网络
I am trying to access a custom section in app.config file from a t4 template in VS2010, but the assembly that defines the custom section cannot be loaded.

I am trying to access a custom section in app.config file from a t4 template in VS2010, but the assembly that defines the custom section cannot be loaded.

I'm using the ConfigurationAccessor to get the section (ref 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" />
  <开发者_JAVA技巧/providers>
</MyProviders>

Calling this line in the .tt file:

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

Gives this error:

Running transformation: System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for MyProviders: Could not load file or assembly 'MyAssembly' or one of its dependencies. The system cannot find the file specified.

The .tt file references the assembly and so does the project, but this doesn't seem to help loading the config section. I have tried implementing the MySection class as a tt code block, but can't use that class in app.config either.

Any ideas?


I think <configSections> need full assembly names (version, culture, publickeytoken)

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


You need to put the MyAssembly in the GAC or just in Visual Studios PublicAssemblies folder for it to be able to find it. The location for the latter is <install_dir>\Common7\IDE\PublicAssemblies.

0

精彩评论

暂无评论...
验证码 换一张
取 消