5

I have a simple project with .NetFramework 3.5 Class library.

I compile and run c# dynamic code at the runtime (with CodeDom) in the .NetFramework project and call from the Asp.NetCore 2.0 project.

when I call the BuildAssembly Method in Common class from Asp.NetCore 2.0 project, I encounter with this Error: (CodeDom throws)

"System.PlatformNotSupportedException":"Operation is not supported on this platform." at Microsoft.CSharp.CSharpCodeGenerator.FromFileBatch(CompilerParameters options, String[] fileNames) at Microsoft.CSharp.CSharpCodeGenerator.System.CodeDom.Compiler.ICodeCompiler.CompileAssemblyFromSourceBatch(CompilerParameters options, String[] sources)

following code:

.NetFrameWork 3.5:

public class Common
{
    public Assembly BuildAssembly(string code)
    {
        CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
        CompilerParameters compilerparams = new CompilerParameters();
        compilerparams.GenerateExecutable = false;//Generate an executable instead of a class library
        compilerparams.GenerateInMemory = false; //Save the assembly as a physical file.

        compilerparams.ReferencedAssemblies.Add("mscorlib.dll");
        compilerparams.ReferencedAssemblies.Add("System.dll");
        compilerparams.ReferencedAssemblies.Add("System.Data.dll");
        compilerparams.ReferencedAssemblies.Add("System.Xml.dll");

        CompilerResults results = codeProvider.CompileAssemblyFromSource(compilerparams, code);
        if (results.Errors.HasErrors)
        {
            StringBuilder errors = new StringBuilder("Compiler Errors :\r\n");
            foreach (CompilerError error in results.Errors)
            {
                errors.AppendFormat("Line {0},{1}\t: {2}\n",
                        error.Line, error.Column, error.ErrorText);
            }
            throw new Exception(errors.ToString());
        }
        else
        {
            return results.CompiledAssembly;
        }
    }
}

Asp.NetCore 2.0:

public IActionResult Test()
{
    string code = @"
using System;
using System.Text;
using System.Data;
using System.Reflection;
using System.ComponentModel;

namespace testNameSpace
{
    public class DynamicClass
    {
         public string Test() {     return ""Hello world""; }
    }
}";
    var res = new Common().BuildAssembly(code);
    return Json("ok");
}

I searched at the web and accoding this Link I install the Microsoft.CodeAnalysis.CSharp nuget package and tested but It's not working.

I can not compile the simple code from this way.

how to fix this error?

AminRostami
  • 2,585
  • 3
  • 29
  • 45
  • 4
    Is there a reason why you're trying to consume a .NET Framework library in a .NET Core application? Why not just make a .NET Framework application? Or make a .NET Core/.NET Standard library? – ProgrammingLlama Nov 12 '19 at 06:39
  • nice, Because the `.Net Framework` application is already manufactured. and in the `Web` Application, We use the `Asp.Net Core`. and We do not want to rewrite the generated code. – AminRostami Nov 12 '19 at 06:45
  • [Related question 1](https://stackoverflow.com/questions/52407001/net-core-2-1-cant-reference-a-net-framework-4-7-2-class-library), [related question 2](https://stackoverflow.com/questions/52703060/referencing-net-framework-4-dll-in-net-core-2-0) – ProgrammingLlama Nov 12 '19 at 06:52
  • thanks @john, but the `.Net Framework` library referenced to `Asp.Net Core` Project without error. error accurse that when to Execute the `BuildAssembly` Method at line `CompilerResults results = codeProvider.CompileAssemblyFromSource(compilerparams, code);` – AminRostami Nov 12 '19 at 07:04
  • So all other methods in the .NET Framework library work when called from .NET Core except for this one? – ProgrammingLlama Nov 12 '19 at 07:05
  • Yes,all of method working well, this method run the `string Code` at runtime and has error in `runtime`. – AminRostami Nov 12 '19 at 07:07

0 Answers0