-
Notifications
You must be signed in to change notification settings - Fork 447
/
ExtensionLoader.cs
181 lines (156 loc) · 6.74 KB
/
ExtensionLoader.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.WebJobs.Host.Config;
using Microsoft.Azure.WebJobs.Script.Description;
using Microsoft.Azure.WebJobs.Script.Models;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using FunctionMetadata = Microsoft.Azure.WebJobs.Script.Description.FunctionMetadata;
namespace Microsoft.Azure.WebJobs.Script.Binding
{
internal class ExtensionLoader
{
private readonly ScriptHostConfiguration _config;
private readonly ILogger _logger;
public ExtensionLoader(ScriptHostConfiguration config, ILogger logger)
{
_config = config;
_logger = logger;
}
public IEnumerable<string> DiscoverBindingTypes(IEnumerable<FunctionMetadata> functions)
{
HashSet<string> bindingTypes = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var function in functions)
{
foreach (var binding in function.InputBindings.Concat(function.OutputBindings))
{
string bindingType = binding.Type;
bindingTypes.Add(bindingType);
}
}
return bindingTypes;
}
// Load extensions that are directly references by the user types.
public void LoadDirectlyReferencedExtensions(IEnumerable<Type> userTypes)
{
var possibleExtensionAssemblies = UserTypeScanner.GetPossibleExtensionAssemblies(userTypes);
foreach (var kv in possibleExtensionAssemblies)
{
var assembly = kv.Key;
var locationHint = kv.Value;
LoadExtensions(assembly, locationHint);
}
}
public void LoadCustomExtensions()
{
string binPath = Path.Combine(_config.RootScriptPath, "bin");
string metadataFilePath = Path.Combine(binPath, ScriptConstants.ExtensionsMetadataFileName);
if (File.Exists(metadataFilePath))
{
var extensionMetadata = JObject.Parse(File.ReadAllText(metadataFilePath));
var extensionItems = extensionMetadata["extensions"]?.ToObject<List<ExtensionReference>>();
if (extensionItems == null)
{
_logger.LogWarning("Invalid extensions metadata file. Unable to load custom extensions");
return;
}
foreach (var item in extensionItems)
{
string extensionName = item.Name ?? item.TypeName;
_logger.LogInformation($"Loading custom extension '{extensionName}'");
Type extensionType = Type.GetType(item.TypeName,
assemblyName =>
{
string path = item.HintPath;
if (string.IsNullOrEmpty(path))
{
path = assemblyName.Name + ".dll";
}
var hintUri = new Uri(path, UriKind.RelativeOrAbsolute);
if (!hintUri.IsAbsoluteUri)
{
path = Path.Combine(binPath, path);
}
if (File.Exists(path))
{
return FunctionAssemblyLoadContext.Shared.LoadFromAssemblyPath(path);
}
return null;
},
(assembly, typeName, ignoreCase) =>
{
return assembly?.GetType(typeName, false, ignoreCase);
}, false, true);
if (extensionType == null ||
!LoadIfExtensionType(extensionType, extensionType.Assembly.Location))
{
_logger.LogWarning($"Unable to load custom extension type for extension '{extensionName}' (Type: `{item.TypeName}`)." +
$"The type does not exist or is not a valid extension. Please validate the type and assembly names.");
}
}
}
}
private void LoadExtensions(Assembly assembly, string locationHint)
{
foreach (var type in assembly.ExportedTypes)
{
LoadIfExtensionType(type, locationHint);
}
}
private bool LoadIfExtensionType(Type extensionType, string locationHint)
{
if (!typeof(IExtensionConfigProvider).IsAssignableFrom(extensionType))
{
return false;
}
if (IsExtensionLoaded(extensionType))
{
return false;
}
IExtensionConfigProvider instance = (IExtensionConfigProvider)Activator.CreateInstance(extensionType);
LoadExtension(instance, locationHint);
return true;
}
// Load extensions that are directly references by the user types.
private void LoadDirectlyReferencesExtensions(IEnumerable<Type> userTypes)
{
var possibleExtensionAssemblies = UserTypeScanner.GetPossibleExtensionAssemblies(userTypes);
foreach (var kv in possibleExtensionAssemblies)
{
var assembly = kv.Key;
var locationHint = kv.Value;
LoadExtensions(assembly, locationHint);
}
}
private bool IsExtensionLoaded(Type type)
{
var registry = _config.HostConfig.GetService<IExtensionRegistry>();
var extensions = registry.GetExtensions<IExtensionConfigProvider>();
foreach (var extension in extensions)
{
var loadedExtentionType = extension.GetType();
if (loadedExtentionType == type)
{
return true;
}
}
return false;
}
// Load a single extension
private void LoadExtension(IExtensionConfigProvider instance, string locationHint = null)
{
JobHostConfiguration config = _config.HostConfig;
var type = instance.GetType();
string name = type.Name;
string msg = $"Loaded custom extension: {name} from '{locationHint}'";
_logger.LogInformation(msg);
config.AddExtension(instance);
}
}
}