views:

320

answers:

2

Does anyone have a good example or helper class that would allow me to read the connection string in a web application from a T4 template residing in ANOTHER assembly referenced by the web application. I am generating some code from the database that it references and i would appresiate some help on how to get the connection string for this use. ive read George Js example here however it only works when the template resides in the web app, please help!!!

+1  A: 

Hi Ori,

you could try to do something like that:

var config = ConfigurationManager.OpenExeConfiguration("../somePathTo/web.config")
//use the config to get values like: config.AppSettings

hope it helps.

-robert

Robert
hey rob, the t4 template is run under a custom tool and therefore relative paths dont work as it is in the context of the tool not the project.
almog.ori
If you have calling Assembly, you could do sth. like that: New Uri(Assembly.GetCallingAssembly().CodeBase).AbsolutePathThis would only work, if you custom Tool uses API calls to excetue T4. May be your custom tool could provide some type of path parameter or could write the path info into some kind of shared data store. (XML-File of the excecuting directory)?It would be nice to get more information.
Robert
this is the result of the above call to <#=System.Reflection.Assembly.GetCallingAssembly().CodeBase #>file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.TextTemplating.10.0/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.TextTemplating.10.0.dll
almog.ori
A: 

after a bit of searching around ive found my answer in t4 one may use

path = Host.ResolvePath(relativeFileName)
almog.ori