Пример #1
0
        public FunctionDefinition Create(FunctionLoadRequest request)
        {
            FunctionMetadata metadata = request.ToFunctionMetadata();

            if (metadata.PathToAssembly == null)
            {
                throw new InvalidOperationException("The path to the function assembly is null.");
            }

            if (metadata.EntryPoint == null)
            {
                throw new InvalidOperationException("The entry point is null.");
            }

            var entryPointMatch = _entryPointRegex.Match(metadata.EntryPoint);

            if (!entryPointMatch.Success)
            {
                throw new InvalidOperationException("Invalid entry point configuration. The function entry point must be defined in the format <fulltypename>.<methodname>");
            }

            string typeName   = entryPointMatch.Groups["typename"].Value;
            string methodName = entryPointMatch.Groups["methodname"].Value;

            Assembly assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(metadata.PathToAssembly);

            Type?functionType = assembly.GetType(typeName);

            MethodInfo?methodInfo = functionType?.GetMethod(methodName);

            if (methodInfo == null)
            {
                throw new InvalidOperationException($"Method '{methodName}' specified in {nameof(FunctionMetadata.EntryPoint)} was not found. This function cannot be created.");
            }

            IFunctionInvoker invoker = _functionInvokerFactory.Create(methodInfo);

            IEnumerable <FunctionParameter> parameters = methodInfo.GetParameters()
                                                         .Where(p => p.Name != null)
                                                         .Select(p => new FunctionParameter(p.Name !, p.ParameterType));

            return(new DefaultFunctionDefinition(metadata, invoker, parameters));
        }
Пример #2
0
        public async Task ExecuteAsync(FunctionContext context)
        {
            var invoker = _invokerCache.GetOrAdd(context.FunctionId,
                                                 _ => _invokerFactory.Create(context.FunctionDefinition));

            object?instance            = invoker.CreateInstance(context.InstanceServices);
            var    modelBindingFeature = context.Features.Get <IModelBindingFeature>();

            object?[] inputArguments;
            if (modelBindingFeature is null)
            {
                Log.ModelBindingFeatureUnavailable(_logger, context);
                inputArguments = new object?[context.FunctionDefinition.Parameters.Length];
            }
            else
            {
                inputArguments = modelBindingFeature.BindFunctionInput(context);
            }

            context.GetBindings().InvocationResult = await invoker.InvokeAsync(instance, inputArguments);
        }