There was a problem getting an AppDomain to run the transformation from the host. The process cannot

本文介绍如何解决T4模板开发中因AppDomain配置不当导致的问题,通过设置正确的ApplicationBase路径来避免异常。提供了完整的处理类代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用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;
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值