-1

I am trying to put all the codes that is needed to create an ASP.NetCore Web Application in a text file and then read it in an ASP.Net Core 3.1 application and compile it using Roslyn and save it as a dll file. I tried a lot. I could do that for a Console application but not for a web application. This is what I did

public void Load(string id, string code, IEnumerable<string> allowedAssemblyNames, IEnumerable<Type> allowedTypes, string path)
{
    try
    {
        var _references = new List<MetadataReference>();
        foreach (var assemblyName in allowedAssemblyNames)
        {
            _references.Add(MetadataReference.CreateFromFile(RuntimeEnvironment.GetRuntimeDirectory() + assemblyName + ".dll"));
        }

        foreach (var type in allowedTypes)
        {
            _references.Add(MetadataReference.CreateFromFile(type.Assembly.Location));
        }
        _references.Add(MetadataReference.CreateFromFile(Assembly.Load("netstandard").Location));


        var options = new CSharpCompilationOptions(
            OutputKind.DynamicallyLinkedLibrary,
            reportSuppressedDiagnostics: true,
            optimizationLevel: OptimizationLevel.Release,
            generalDiagnosticOption: ReportDiagnostic.Error,
            allowUnsafe: false);

        var syntaxTree = CSharpSyntaxTree.ParseText(code, options: new CSharpParseOptions(LanguageVersion.Latest, kind: SourceCodeKind.Regular));
        var compilation = CSharpCompilation.Create(id, new[] { syntaxTree }, _references, options);

        assemblyLoadContext = new AssemblyLoadContext(id, true);

        using (var ms = new MemoryStream())
        {
            var result = compilation.Emit(ms);
            if (result.Success)
            {
                ms.Seek(0, SeekOrigin.Begin);
                bytes = ms.ToArray();
                File.WriteAllBytes(path, bytes);
                ms.Seek(0, SeekOrigin.Begin);
                assembly = assemblyLoadContext.LoadFromStream(ms);
            }
        }
    }
    catch (Exception ex)
    {
        throw;
    }

}

Is that possible at all?

Mohammad Taherian
  • 1,622
  • 1
  • 15
  • 34
  • Have you read and tried this? https://learn.microsoft.com/en-us/archive/msdn-magazine/2017/may/net-core-cross-platform-code-generation-with-roslyn-and-net-core – Christopher Painter Jan 24 '20 at 23:52
  • @ChristopherPainter Thank you. I tried that page before. It shows how to create different classes and run it but I need to create a new website from the scratch. It contains `Program` class, `main` method, `Startup` class and etc. Can I do that? – Mohammad Taherian Jan 26 '20 at 13:36

1 Answers1

1

After spending some time on it, based on a discussion on GitHub, it seems it is not possible to do that.

The answer given was:

I believe it's considered unsupported by .NET Core to compile directly against System.Private.CoreLib.dll. In general, it will probably be quite difficult to compile an application without using the dotnet tool and MSBuild, as you will have to effectively replicate all the logic that goes into picking the appropriate reference assemblies, then generating the right executing environment for the .NET Core shared loader.

Tony
  • 9,672
  • 3
  • 47
  • 75
Mohammad Taherian
  • 1,622
  • 1
  • 15
  • 34