Ejemplo n.º 1
0
            public Task <ITriggerData> BindAsync(object value, ValueBindingContext context)
            {
                // If we are not directly passed a DurableActivityContext, we can assume we are being called directly
                // by the admin API. This is mainly used for the Azure Portal execution scenario.
                if (!(value is DurableActivityContext activityContext))
                {
                    if (!(value is string serializedInput))
                    {
                        throw new InvalidOperationException($"Cannot execute an Activity Trigger without a {nameof(DurableActivityContext)} or a {nameof(String)} that represents the serialized input.");
                    }

                    // Durable functions expects input as a JArray with one element.
                    serializedInput = $"[{serializedInput}]";

                    activityContext = new DurableActivityContext(this.durableTaskConfig, Guid.NewGuid().ToString(), serializedInput);
                }

                Type destinationType = this.parameterInfo.ParameterType;

                object convertedValue;

                if (destinationType == typeof(object))
                {
                    convertedValue = value;
                }
                else if (destinationType == typeof(IDurableActivityContext))
                {
                    convertedValue = activityContext;
                }
                else if (destinationType == typeof(JObject))
                {
                    convertedValue = ActivityContextToJObject(activityContext);
                }
                else
                {
                    convertedValue = activityContext.GetInput(destinationType);
                }

                var inputValueProvider = new ObjectValueProvider(
                    convertedValue,
                    this.parameterInfo.ParameterType);

                // Note that there could be conflicts in thiese dictionary keys, in which case
                // the order here determines which binding rule will win.
                var bindingData = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);

                bindingData[InstanceIdBindingPropertyName] = ((IDurableActivityContext)activityContext).InstanceId;
                bindingData[this.parameterInfo.Name]       = convertedValue;
                bindingData[DataBindingPropertyName]       = activityContext.GetInputAsJson();

                var triggerData = new TriggerData(inputValueProvider, bindingData);

                triggerData.ReturnValueProvider = new ActivityTriggerReturnValueBinder(
                    activityContext,
                    this.parameterInfo.ParameterType);

                return(Task.FromResult <ITriggerData>(triggerData));
            }
 private static string ActivityContextToString(DurableActivityContext arg)
 {
     return(arg.GetInput <string>());
 }