我认为这是一个权限问题,但我不知道如何解决它,我收到以下异常:
System.IO.FileLoadException: LoadFrom(), LoadFile(), Load(byte[]) and LoadModule() have been disabled by the host.
at System.Reflection.RuntimeAssembly.nLoadImage(Byte[] rawAssembly, Byte[] rawSymbolStore, Evidence evidence, StackCrawlMark& stackMark, Boolean fIntrospection, SecurityContextSource securityContextSource)
at System.Reflection.Assembly.Load(Byte[] rawAssembly, Byte[] rawSymbolStore, Evidence securityEvidence)
at Microsoft.CSharp.CSharpCodeGenerator.FromFileBatch(CompilerParameters options, String[] fileNames)
at Microsoft.CSharp.CSharpCodeGenerator.FromDomBatch(CompilerParameters options, CodeCompileUnit[] ea)
at Microsoft.CSharp.CSharpCodeGenerator.System.CodeDom.Compiler.ICodeCompiler.CompileAssemblyFromDomBatch(CompilerParameters options, CodeCompileUnit[] ea)
at System.CodeDom.Compiler.CodeDomProvider.CompileAssemblyFromDom(CompilerParameters options, CodeCompileUnit[] compilationUnits)
执行以下代码时:
var engine = this.CreateRazorEngine();
var typeName = "view_" + Guid.NewGuid().ToString("N");
var results = engine.GenerateCode(new StringReader(content), typeName, "", typeName + ".cs");
if (!results.Success) { this.Fail("Unable to compile view '" + this.filename + "'."); return; }
using (var codeProvider = new CSharpCodeProvider()) {
var tempPath = Path.GetTempPath();
var outputFile = Path.Combine(tempPath, Guid.NewGuid().ToString("N") + ".dll");
var compilerParameter = new CompilerParameters(this.references, outputFile, true) { GenerateInMemory = true, CompilerOptions = "/optimize", TempFiles = new TempFileCollection(tempPath) };
var compilerResults = codeProvider.CompileAssemblyFromDom(compilerParameter, results.GeneratedCode);
if (compilerResults.Errors.HasErrors) {
var compileExceptionMessage = string.Join(Environment.NewLine + Environment.NewLine, compilerResults.Errors.OfType<CompilerError>().Where(ce => !ce.IsWarning).Select(e => e.FileName + ":" + Environment.NewLine + e.ErrorText).ToArray());
this.Fail(compileExceptionMessage);
return;
}
this.view = Activator.CreateInstance(compilerResults.CompiledAssembly.GetType(typeName, true, false), true) as ViewBase;
}
为清楚起见,CreateRazorEngine代码为:
private RazorTemplateEngine CreateRazorEngine() {
var host = new RazorEngineHost(new CSharpRazorCodeLanguage()) { DefaultBaseClass = typeof(ViewBase).FullName };
foreach(var name in this.usings) { host.NamespaceImports.Add(name); }
return new RazorTemplateEngine(host);
}
有人可以让我知道代码和我需要在哪里提高安全级别,以便创建的程序集具有足够的权限来加载已经给出的引用。