-
Notifications
You must be signed in to change notification settings - Fork 432
/
DotNetFunctionDescriptorProvider.cs
235 lines (209 loc) · 10.7 KB
/
DotNetFunctionDescriptorProvider.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// 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.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Script.Binding;
using Microsoft.Azure.WebJobs.Script.Diagnostics;
using Microsoft.Azure.WebJobs.Script.Extensibility;
using Microsoft.CodeAnalysis.Scripting;
using Microsoft.Extensions.Logging;
namespace Microsoft.Azure.WebJobs.Script.Description
{
internal sealed class DotNetFunctionDescriptorProvider : FunctionDescriptorProvider, IDisposable
{
private readonly IMetricsLogger _metricsLogger;
private readonly ILoggerFactory _loggerFactory;
private readonly ICompilationServiceFactory<ICompilationService<IDotNetCompilation>, IFunctionMetadataResolver> _compilationServiceFactory;
private static readonly Lazy<Regex> _taskOfUnitType = new Lazy<Regex>(() => new Regex(@"^System\.Threading\.Tasks\.Task`1\[\[Microsoft\.FSharp\.Core\.Unit, FSharp\.Core, Version=\d*\.\d*\.\d*\.\d*, Culture=.*, PublicKeyToken=b03f5f7f11d50a3a\]\]$", RegexOptions.Compiled));
public DotNetFunctionDescriptorProvider(ScriptHost host, ScriptJobHostOptions config, ICollection<IScriptBindingProvider> bindingProviders, IMetricsLogger metricsLogger, ILoggerFactory loggerFactory)
: this(host, config, bindingProviders, new DotNetCompilationServiceFactory(loggerFactory), metricsLogger, loggerFactory)
{
}
public DotNetFunctionDescriptorProvider(ScriptHost host, ScriptJobHostOptions config, ICollection<IScriptBindingProvider> bindingProviders,
ICompilationServiceFactory<ICompilationService<IDotNetCompilation>, IFunctionMetadataResolver> compilationServiceFactory, IMetricsLogger metricsLogger, ILoggerFactory loggerFactory)
: base(host, config, bindingProviders)
{
_metricsLogger = metricsLogger;
_loggerFactory = loggerFactory;
_compilationServiceFactory = compilationServiceFactory;
}
public void Dispose()
{
Dispose(true);
}
private void Dispose(bool disposing)
{
if (disposing)
{
}
}
public override async Task<(bool Success, FunctionDescriptor Descriptor)> TryCreate(FunctionMetadata functionMetadata)
{
if (functionMetadata == null)
{
throw new ArgumentNullException(nameof(functionMetadata));
}
// We can only handle script types supported by the current compilation service factory
if (!_compilationServiceFactory.SupportedLanguages.Contains(functionMetadata.Language))
{
return (false, null);
}
return await base.TryCreate(functionMetadata);
}
protected override IFunctionInvoker CreateFunctionInvoker(string scriptFilePath, BindingMetadata triggerMetadata, FunctionMetadata functionMetadata, Collection<FunctionBinding> inputBindings, Collection<FunctionBinding> outputBindings)
{
return new DotNetFunctionInvoker(Host,
functionMetadata,
inputBindings,
outputBindings,
new FunctionEntryPointResolver(functionMetadata.EntryPoint),
_compilationServiceFactory,
_loggerFactory,
_metricsLogger,
BindingProviders);
}
protected override async Task<Collection<ParameterDescriptor>> GetFunctionParametersAsync(IFunctionInvoker functionInvoker, FunctionMetadata functionMetadata,
BindingMetadata triggerMetadata, Collection<CustomAttributeBuilder> methodAttributes, Collection<FunctionBinding> inputBindings, Collection<FunctionBinding> outputBindings)
{
if (functionInvoker == null)
{
throw new ArgumentNullException(nameof(functionInvoker));
}
if (functionMetadata == null)
{
throw new ArgumentNullException(nameof(functionMetadata));
}
if (triggerMetadata == null)
{
throw new ArgumentNullException(nameof(triggerMetadata));
}
if (methodAttributes == null)
{
throw new ArgumentNullException(nameof(methodAttributes));
}
var dotNetInvoker = functionInvoker as DotNetFunctionInvoker;
if (dotNetInvoker == null)
{
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Expected invoker of type '{0}' but received '{1}'", typeof(DotNetFunctionInvoker).Name, functionInvoker.GetType().Name));
}
try
{
ApplyMethodLevelAttributes(functionMetadata, triggerMetadata, methodAttributes);
MethodInfo functionTarget = await dotNetInvoker.GetFunctionTargetAsync();
ParameterInfo[] parameters = functionTarget.GetParameters();
Collection<ParameterDescriptor> descriptors = new Collection<ParameterDescriptor>();
IEnumerable<FunctionBinding> bindings = inputBindings.Union(outputBindings);
ParameterDescriptor descriptor = null;
foreach (var parameter in parameters)
{
// Is it the trigger parameter?
if (string.Compare(parameter.Name, triggerMetadata.Name, StringComparison.Ordinal) == 0)
{
ParameterDescriptor triggerParameter = CreateTriggerParameter(triggerMetadata, parameter.ParameterType);
descriptors.Add(triggerParameter);
}
else
{
Type parameterType = parameter.ParameterType;
bool parameterIsByRef = parameterType.IsByRef;
if (parameterIsByRef)
{
parameterType = parameterType.GetElementType();
}
descriptor = new ParameterDescriptor(parameter.Name, parameter.ParameterType);
var binding = bindings.FirstOrDefault(b => string.Compare(b.Metadata.Name, parameter.Name, StringComparison.Ordinal) == 0);
if (binding != null)
{
Collection<CustomAttributeBuilder> customAttributes = binding.GetCustomAttributes(parameter.ParameterType);
if (customAttributes != null)
{
foreach (var customAttribute in customAttributes)
{
descriptor.CustomAttributes.Add(customAttribute);
}
}
}
if (parameter.IsOut)
{
descriptor.Attributes |= ParameterAttributes.Out;
}
descriptors.Add(descriptor);
}
}
// Add any additional required System parameters (if they haven't already been defined by the user)
if (!descriptors.Any(p => p.Type == typeof(ExecutionContext)))
{
// Add ExecutionContext to provide access to InvocationId, etc.
descriptors.Add(new ParameterDescriptor(ScriptConstants.SystemExecutionContextParameterName, typeof(ExecutionContext)));
}
if (TryCreateReturnValueParameterDescriptor(functionTarget.ReturnType, bindings, out descriptor))
{
// If a return value binding has been specified, set up an output
// binding to map it to. By convention, this is set up as the last
// parameter.
descriptors.Add(descriptor);
}
return descriptors;
}
catch (AggregateException exc)
{
if (!(exc.InnerException is CompilationErrorException))
{
throw;
}
}
catch (CompilationErrorException)
{
}
// We were unable to compile the function to get its signature,
// setup the descriptor with the default parameters
methodAttributes.Clear();
return await base.GetFunctionParametersAsync(functionInvoker, functionMetadata, triggerMetadata, methodAttributes, inputBindings, outputBindings);
}
internal static bool TryCreateReturnValueParameterDescriptor(Type functionReturnType, IEnumerable<FunctionBinding> bindings, out ParameterDescriptor descriptor)
{
descriptor = null;
if (string.Equals(functionReturnType.FullName, "Microsoft.FSharp.Core.Unit", StringComparison.Ordinal) ||
_taskOfUnitType.Value.IsMatch(functionReturnType.FullName))
{
return false;
}
if (functionReturnType == typeof(void) || functionReturnType == typeof(Task))
{
return false;
}
// Task<T>
if (typeof(Task).IsAssignableFrom(functionReturnType))
{
if (!(functionReturnType.IsGenericType && functionReturnType.GetGenericTypeDefinition() == typeof(Task<>)))
{
throw new InvalidOperationException($"{ScriptConstants.SystemReturnParameterBindingName} cannot be bound to return type {functionReturnType.Name}.");
}
functionReturnType = functionReturnType.GetGenericArguments()[0];
}
var byRefType = functionReturnType.MakeByRefType();
descriptor = new ParameterDescriptor(ScriptConstants.SystemReturnParameterName, byRefType);
descriptor.Attributes |= ParameterAttributes.Out;
var returnBinding = bindings.SingleOrDefault(p => p.Metadata.IsReturn);
if (returnBinding != null)
{
Collection<CustomAttributeBuilder> customAttributes = returnBinding.GetCustomAttributes(byRefType);
if (customAttributes != null)
{
foreach (var customAttribute in customAttributes)
{
descriptor.CustomAttributes.Add(customAttribute);
}
}
}
return true;
}
}
}