public DefaultFunctionContext(IServiceScopeFactory serviceScopeFactory, IInvocationFeatures features) { _serviceScopeFactory = serviceScopeFactory ?? throw new ArgumentNullException(nameof(serviceScopeFactory)); Features = features ?? throw new ArgumentNullException(nameof(features)); _invocation = features.Get <FunctionInvocation>() ?? throw new InvalidOperationException($"The '{nameof(FunctionInvocation)}' feature is required."); FunctionDefinition = features.Get <FunctionDefinition>() ?? throw new InvalidOperationException($"The {nameof(Worker.FunctionDefinition)} feature is required."); }
public FunctionContext CreateContext(IInvocationFeatures features) { var invocation = features.Get <FunctionInvocation>() ?? throw new InvalidOperationException($"The {nameof(FunctionInvocation)} feature is required."); var functionDefinition = _functionMap[invocation.FunctionId]; features.Set <FunctionDefinition>(functionDefinition); return(_functionContextFactory.Create(features)); }
/// <summary> /// Gets a feature of type <typeparamref name="T"/> from the <see cref="IInvocationFeatures"/>. /// </summary> /// <typeparam name="T">The type of the feature to get.</typeparam> /// <param name="features">The <see cref="IInvocationFeatures"/>.</param> /// <returns>A feature object of type <typeparamref name="T"/>.</returns> /// <exception cref="InvalidOperationException">There is no feature of type <typeparamref name="T"/>.</exception> public static T GetRequired <T>(this IInvocationFeatures features) { var feature = features.Get <T>(); if (feature is null) { throw new InvalidOperationException($"No feature is registered with the type {typeof(T)}."); } return(feature); }
public TestFunctionContext(FunctionDefinition functionDefinition, FunctionInvocation invocation, IInvocationFeatures features = null) { FunctionDefinition = functionDefinition; _invocation = invocation; if (features != null) { Features = features; } Features.Set <IFunctionBindingsFeature>(new TestFunctionBindingsFeature { OutputBindingsInfo = new DefaultOutputBindingsInfoProvider().GetBindingsInfo(FunctionDefinition) }); BindingContext = new DefaultBindingContext(this); }
internal static async Task <InvocationResponse> InvocationRequestHandlerAsync(InvocationRequest request, IFunctionsApplication application, IInvocationFeaturesFactory invocationFeaturesFactory, ObjectSerializer serializer, IOutputBindingsInfoProvider outputBindingsInfoProvider) { FunctionContext? context = null; FunctionInvocation?invocation = null; InvocationResponse response = new() { InvocationId = request.InvocationId }; try { invocation = new GrpcFunctionInvocation(request); IInvocationFeatures invocationFeatures = invocationFeaturesFactory.Create(); invocationFeatures.Set <FunctionInvocation>(invocation); context = application.CreateContext(invocationFeatures); invocationFeatures.Set <IFunctionBindingsFeature>(new GrpcFunctionBindingsFeature(context, request, outputBindingsInfoProvider)); await application.InvokeFunctionAsync(context); var functionBindings = context.GetBindings(); foreach (var binding in functionBindings.OutputBindingData) { var parameterBinding = new ParameterBinding { Name = binding.Key }; if (binding.Value is not null) { parameterBinding.Data = await binding.Value.ToRpcAsync(serializer); } response.OutputData.Add(parameterBinding); } if (functionBindings.InvocationResult != null) { TypedData?returnVal = await functionBindings.InvocationResult.ToRpcAsync(serializer); response.ReturnValue = returnVal; } response.Result = new StatusResult { Status = StatusResult.Types.Status.Success }; } catch (Exception ex) { response.Result = new StatusResult { Exception = ex.ToRpcException(), Status = StatusResult.Types.Status.Failure }; } finally { (context as IDisposable)?.Dispose(); } return(response); }
/// <summary> /// Tries to get a feature of type <typeparamref name="T"/> from the <see cref="IInvocationFeatures"/>. /// </summary> /// <typeparam name="T">The type of the feature to get.</typeparam> /// <param name="features">The <see cref="IInvocationFeatures"/>.</param> /// <param name="feature">The feature, if found. Otherwise, null.</param> /// <returns>True if the feature was found. Otherwise, false.</returns> public static bool TryGet <T>(this IInvocationFeatures features, out T?feature) { feature = features.Get <T>(); return(feature is not null); }
public TestFunctionContext(IInvocationFeatures features) : this(new TestFunctionDefinition(), new TestFunctionInvocation(), features) { }
public FunctionContext Create(IInvocationFeatures invocationFeatures) { return(new DefaultFunctionContext(_serviceScopeFactory, invocationFeatures)); }