0

I believe this is a permissions issue but I don't know how to solve it, I receive the following exception:

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)

When executing the following code:

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

For clarity the CreateRazorEngine code is:

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

Can someone let me know what code and where I need to put it to increase the level of security so that the created assembly has permission enough to load the references it has been given please.

Anupheaus
  • 3,383
  • 2
  • 24
  • 30
  • Very unusual. Focus less on the code, that's not the problem, focus on why this web server is configured in such an usual way. Definitely talk to the admin of the machine, asked what he changed in the config file. – Hans Passant Nov 09 '13 at 15:18
  • Well, yes and no, I didn't want to go into too much detail (I thought I'd maybe rambled on enough already) but essentially this code is being run by PostSharp (during compiling), so there may be some restrictions to permissions due to that. It runs fine during runtime normally. – Anupheaus Nov 09 '13 at 18:38

0 Answers0