I am attempting to provide a URL in the web.config file that the build system will update before compiling the .tt files on my web project.
The relevant sections from my .tt file are below:
<#@ template debug="true" hostSpecific="true" language="C#" #>
<#@ output extension=".ts" #>
<#@ Assembly Name="System.Core.dll" #>
<#@ Assembly Name="System.Configuration.dll" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Configuration" #>
<#@ import namespace="System.Collections.Specialized" #>
import { Injectable } from "@angular/core";
<#= RegisterNameSpace("Server.Web.Dashboard.Models.APIRequest") #>
<#= RegisterNameSpace("Server.Web.Dashboard.Models.APIResponse") #>
<#= RegisterNameSpace("Server.Web.Dashboard.Models.APIGeneric") #>
<#= RegisterNameSpace("Server.Data.POCODataObjects") #>
@Injectable()
export class WebServiceCommunicatorService {
rootUrl : string = '<#= ConfigurationManager.AppSettings["ServerServiceURL"] #>';
.....
When I try to generate the .ts file from the .tt file I get the following error message:
Severity Code Description Project File Line Suppression State
Error An expression block evaluated as Null
System.ArgumentNullException: Value cannot be null.
Parameter name: objectToConvert
at Microsoft.VisualStudio.TextTemplating.ToStringHelper.ToStringWithCulture(Object objectToConvert)
at Microsoft.VisualStudio.TextTemplatingF553823B88CD6076D80EB08F6EA809752D0E5DC3E61C0FA53FB2F9AC22ACABF3B7BB9C8801A54E5DBC2A556C03FA42F2EA2AFCE58B708BEC94B0968193987868.GeneratedTextTransformation.TransformText() in webservicecommunicator.service.tt:line 35 Dashboard webservicecommunicator.service.tt 35
I've gone down the approach of pretending that the file is an XML file and not a config file. First i find the absolute path to the web.config, and then load it and read the node.
First the dependancies:
<#@ Assembly Name="System.Web.dll" #>
<#@ Assembly Name="System.Configuration.dll" #>
<#@ assembly name="System.Xml" #>
<#@ import namespace="System.Configuration" #>
<#@ import namespace="System.Web.Configuration" #>
<#@ import namespace="System.Collections.Specialized" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.IO" #>
So here is my attempt:
string GetWebServiceUrl() {
XmlDocument doc = new XmlDocument();
string absolutePath = this.Host.ResolvePath("../../../web.config");
doc.Load(absolutePath);
XmlNode node = doc.DocumentElement.SelectSingleNode("/configuration/appSettings/add[@key='ServerServiceURL']");
return node.Attributes["value"].Value.ToString();
}
You could try using the ConfigurationManager.OpenExeConfiguration(path)
method to open a hard-coded path (not sure if you can do relative paths). At that point you can use the returned Configuration
object's own AppSettings
property.
Add the System.Configuration assembly and import it into your template file as demonstrated below. This will allow you to reference the AppSettings using the ConfigurationManager.
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Configuration" #>
<#@ import namespace="System.Configuration" #>
<#
var myVariable = ConfigurationManager.AppSettings["MyConfigSetting"];
... more code here
#>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.