Skip to content

Commit e6a5b26

Browse files
committed
Adding ability to load a function from an assembly in the shared context
1 parent 5160920 commit e6a5b26

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/WebJobs.Script/Description/DotNet/Compilation/DotNetCompilationResult.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public static DotNetCompilationResult FromStream(Stream assemblyStream, Stream p
1919
public static DotNetCompilationResult FromPath(string assemblyPath)
2020
=> new PathCompilationResult(assemblyPath);
2121

22+
public static DotNetCompilationResult FromAssemblyName(string assemblyName)
23+
=> new AssemblyReferenceCompilationResult(assemblyName);
24+
2225
public abstract Assembly Load(FunctionMetadata metadata, IFunctionMetadataResolver metadataResolver, ILogger logger);
2326

2427
private sealed class BinaryCompilationResult : DotNetCompilationResult
@@ -85,5 +88,23 @@ public PathCompilationResult(string assemblyPath)
8588
public override Assembly Load(FunctionMetadata metadata, IFunctionMetadataResolver metadataResolver, ILogger logger)
8689
=> FunctionAssemblyLoadContext.Shared.LoadFromAssemblyPath(AssemblyPath, true);
8790
}
91+
92+
private sealed class AssemblyReferenceCompilationResult : DotNetCompilationResult
93+
{
94+
public AssemblyReferenceCompilationResult(string assemblyName)
95+
{
96+
if (string.IsNullOrWhiteSpace(assemblyName))
97+
{
98+
throw new ArgumentException("Invalid assembly name string", nameof(assemblyName));
99+
}
100+
101+
AssemblyName = assemblyName;
102+
}
103+
104+
public string AssemblyName { get; }
105+
106+
public override Assembly Load(FunctionMetadata metadata, IFunctionMetadataResolver metadataResolver, ILogger logger)
107+
=> FunctionAssemblyLoadContext.Shared.LoadFromAssemblyName(new AssemblyName(AssemblyName));
108+
}
88109
}
89110
}

src/WebJobs.Script/Description/DotNet/Compilation/Raw/RawAssemblyCompilation.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace Microsoft.Azure.WebJobs.Script.Description
1414
{
1515
public class RawAssemblyCompilation : IDotNetCompilation
1616
{
17+
private const string AssemblyNamePrefix = "assembly:";
1718
private static readonly Regex _entryPointRegex = new Regex("^(?<typename>.*)\\.(?<methodname>\\S*)$", RegexOptions.Compiled);
1819
private readonly string _assemblyFilePath;
1920
private readonly string _entryPointName;
@@ -29,7 +30,17 @@ async Task<object> ICompilation.EmitAsync(CancellationToken cancellationToken)
2930

3031
public Task<DotNetCompilationResult> EmitAsync(CancellationToken cancellationToken)
3132
{
32-
return Task.FromResult(DotNetCompilationResult.FromPath(_assemblyFilePath));
33+
DotNetCompilationResult result = null;
34+
if (_assemblyFilePath != null && _assemblyFilePath.StartsWith(AssemblyNamePrefix))
35+
{
36+
result = DotNetCompilationResult.FromAssemblyName(_assemblyFilePath.Substring(AssemblyNamePrefix.Length));
37+
}
38+
else
39+
{
40+
result = DotNetCompilationResult.FromPath(_assemblyFilePath);
41+
}
42+
43+
return Task.FromResult(result);
3344
}
3445

3546
public ImmutableArray<Diagnostic> GetDiagnostics() => ImmutableArray<Diagnostic>.Empty;

0 commit comments

Comments
 (0)