-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRemoteControllerFeatureProvider.cs
56 lines (51 loc) · 2.38 KB
/
RemoteControllerFeatureProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System.Diagnostics;
using System.Reflection;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using dotnet_roslyn_dynamic_api.Controllers;
namespace dotnet_roslyn_dynamic_api
{
public class RemoteControllerFeatureProvider : IApplicationFeatureProvider<ControllerFeature>
{
public void PopulateFeature(IEnumerable<ApplicationPart> parts, ControllerFeature feature)
{
// Dynamic location of rosslyn config classes
// Must pull from remote location for original class definitions
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("config/appsettings.json")
.AddEnvironmentVariables()
.Build();
string connectionString = config.GetValue<string>("AzureBlob:Url");
var remoteCode = new HttpClient().GetStringAsync(connectionString).GetAwaiter().GetResult();
if (remoteCode != null)
{
var compilation = CSharpCompilation.Create("DynamicAssembly", new[] { CSharpSyntaxTree.ParseText(remoteCode) },
new[] {
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
MetadataReference.CreateFromFile(typeof(RemoteControllerFeatureProvider).Assembly.Location)
},
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
using (var ms = new MemoryStream())
{
var emitResult = compilation.Emit(ms);
if (!emitResult.Success)
{
// handle, log errors etc
Debug.WriteLine("Compilation failed!");
return;
}
ms.Seek(0, SeekOrigin.Begin);
var assembly = Assembly.Load(ms.ToArray());
var candidates = assembly.GetExportedTypes();
foreach (var candidate in candidates)
{
feature.Controllers.Add(typeof(BaseController<>).MakeGenericType(candidate).GetTypeInfo());
GlobalVariables.GeneratedClasses.Add(candidate.ToString());
}
}
}
}
}
}