用T4模板开发VS SDK扩展应用,在运行时AppDomain如果没配置正确路径就会抛出这个异常。处理方式:
Assembly myAssembly = Assembly.GetAssembly(this.GetType());
string assemblePath = myAssembly.Location.Substring(0, myAssembly.Location.LastIndexOf("\\"));
AppDomainSetup ads = new AppDomainSetup()
{
ApplicationName = "Generation App Domain",
ApplicationBase = assemblePath,
DisallowBindingRedirects = false,
DisallowCodeDownload = true,
ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
};
AppDomain domain = AppDomain.CreateDomain("Generation App Domain", null, ads);
设置ApplicationBase就不会出错,默认这个属性是无值的。下面是完整的处理类。
using Microsoft.VisualStudio.TextTemplating;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
namespace Vanyu.Common.CodeTools
{
class CodeTemplatingEngineHost : ITextTemplatingEngineHost, ITextTemplatingSessionHost
{
internal string TemplateFileValue;
public string TemplateFile
{
get { return TemplateFileValue; }
}
private string fileExtensionValue = ".txt";
public string FileExtension
{
get { return fileExtensionValue; }
}
private Encoding fileEncodingValue = Encoding.UTF8;
public Encoding FileEncoding
{
get { return fileEncodingValue; }
}
private CompilerErrorCollection errorsValue;
public CompilerErrorCollection Errors
{
get { return errorsValue; }
}
public IList<string> StandardAssemblyReferences
{
get
{
return new string[]
{
typeof(Uri).Assembly.Location
};
}
}
public IList<string> StandardImports
{
get
{
return new string[]
{
"System"
};
}
}
public bool LoadIncludeText(string requestFileName, out string content, out string location)
{
content = String.Empty;
location = String.Empty;
if (File.Exists(requestFileName))
{
content = File.ReadAllText(requestFileName);
return true;
}
else
{
return false;
}
}
public object GetHostOption(string optionName)
{
object returnObject;
switch (optionName)
{
case "CacheAssemblies":
returnObject = true;
break;
default:
returnObject = null;
break;
}
return returnObject;
}
public string ResolveAssemblyReference(string assemblyReference)
{
if (File.Exists(assemblyReference))
{
return assemblyReference;
}
string candidate = Path.Combine(Path.GetDirectoryName(this.TemplateFile), assemblyReference);
if (File.Exists(candidate))
{
return candidate;
}
return "";
}
public Type ResolveDirectiveProcessor(string processorName)
{
if (string.Compare(processorName, "XYZ", StringComparison.OrdinalIgnoreCase) == 0)
{
//return typeof();
}
throw new Exception("Directive Processor not found");
}
public string ResolvePath(string fileName)
{
if (fileName == null)
{
throw new ArgumentNullException("the file name cannot be null");
}
if (File.Exists(fileName))
{
return fileName;
}
string candidate = Path.Combine(Path.GetDirectoryName(this.TemplateFile), fileName);
if (File.Exists(candidate))
{
return candidate;
}
return fileName;
}
public string ResolveParameterValue(string directiveId, string processorName, string parameterName)
{
if (directiveId == null)
{
throw new ArgumentNullException("the directiveId cannot be null");
}
if (processorName == null)
{
throw new ArgumentNullException("the processorName cannot be null");
}
if (parameterName == null)
{
throw new ArgumentNullException("the parameterName cannot be null");
}
return String.Empty;
}
public void SetFileExtension(string extension)
{
fileExtensionValue = extension;
}
public void SetOutputEncoding(Encoding encoding, bool fromOutputDirective)
{
fileEncodingValue = encoding;
}
public void LogErrors(CompilerErrorCollection errors)
{
errorsValue = errors;
}
public AppDomain ProvideTemplatingAppDomain(string content)
{
Assembly myAssembly = Assembly.GetAssembly(this.GetType());
string assemblePath = myAssembly.Location.Substring(0, myAssembly.Location.LastIndexOf("\\"));
AppDomainSetup ads = new AppDomainSetup()
{
ApplicationName = "Generation App Domain",
ApplicationBase = assemblePath,
DisallowBindingRedirects = false,
DisallowCodeDownload = true,
ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
};
AppDomain domain = AppDomain.CreateDomain("Generation App Domain", null, ads);
return domain;
}
public ITextTemplatingSession CreateSession()
{
return Session;
}
public ITextTemplatingSession Session
{
get;
set;
}
}
}