diff --git a/src/WebJobs.Extensions.DurableTask/Bindings/EntityTriggerAttributeBindingProvider.cs b/src/WebJobs.Extensions.DurableTask/Bindings/EntityTriggerAttributeBindingProvider.cs index c632f2ce6..0124aa081 100644 --- a/src/WebJobs.Extensions.DurableTask/Bindings/EntityTriggerAttributeBindingProvider.cs +++ b/src/WebJobs.Extensions.DurableTask/Bindings/EntityTriggerAttributeBindingProvider.cs @@ -108,7 +108,7 @@ private static IReadOnlyDictionary GetBindingDataContract(Paramete return contract; } - public Task BindAsync(object value, ValueBindingContext context) + public Task BindAsync(object? value, ValueBindingContext context) { if (value is DurableEntityContext entityContext) { @@ -150,10 +150,16 @@ public Task BindAsync(object value, ValueBindingContext context) var triggerData = new TriggerData(contextValueProvider, EmptyBindingData); return Task.FromResult(triggerData); } - else + + // Portal/Admin API invocations use null when input is omitted and a serialized string otherwise. + else if (value is null || value is string) { - throw new ArgumentException($"Don't know how to bind to {value?.GetType().Name ?? "null"}.", nameof(value)); + throw new InvalidOperationException( + "Durable entity functions do not support direct invocation. " + + "Signal an entity from a client or orchestrator function by using a Durable client."); } + + throw new ArgumentException($"Don't know how to bind to {value.GetType().Name}.", nameof(value)); } public ParameterDescriptor ToParameterDescriptor() diff --git a/src/WebJobs.Extensions.DurableTask/Bindings/OrchestrationTriggerAttributeBindingProvider.cs b/src/WebJobs.Extensions.DurableTask/Bindings/OrchestrationTriggerAttributeBindingProvider.cs index e28f743b2..c811c2f1e 100644 --- a/src/WebJobs.Extensions.DurableTask/Bindings/OrchestrationTriggerAttributeBindingProvider.cs +++ b/src/WebJobs.Extensions.DurableTask/Bindings/OrchestrationTriggerAttributeBindingProvider.cs @@ -198,10 +198,16 @@ public Task BindAsync(object? value, ValueBindingContext context) var triggerData = new TriggerData(contextValueProvider, EmptyBindingData); return Task.FromResult(triggerData); } - else + + // Portal/Admin API invocations use null when input is omitted and a serialized string otherwise. + else if (value is null || value is string) { - throw new ArgumentException($"Don't know how to bind to {value?.GetType().Name ?? "null"}.", nameof(value)); + throw new InvalidOperationException( + "Durable orchestrator functions do not support direct invocation. " + + "Start an orchestration from a client function by using a Durable client."); } + + throw new ArgumentException($"Don't know how to bind to {value.GetType().Name}.", nameof(value)); } public ParameterDescriptor ToParameterDescriptor() diff --git a/test/FunctionsV2/Tests/Unit/TriggerBindingProviderTests.cs b/test/FunctionsV2/Tests/Unit/TriggerBindingProviderTests.cs new file mode 100644 index 000000000..45bb7ca51 --- /dev/null +++ b/test/FunctionsV2/Tests/Unit/TriggerBindingProviderTests.cs @@ -0,0 +1,166 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +#nullable enable + +using System; +using System.Collections.Generic; +using System.Reflection; +using System.Threading; +using System.Threading.Tasks; +using DurableTask.AzureStorage; +using Microsoft.Azure.WebJobs.Host.Triggers; +using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Options; +using Xunit; + +namespace Microsoft.Azure.WebJobs.Extensions.DurableTask.Tests +{ + public class TriggerBindingProviderTests + { + public static IEnumerable DirectInvocationTriggerValues() + { + yield return new object?[] { null }; + yield return new object?[] { string.Empty }; + } + + public static IEnumerable UnsupportedTriggerValues() + { + yield return new object?[] { 42, "Int32" }; + } + + [Theory] + [MemberData(nameof(DirectInvocationTriggerValues))] + [Trait("Category", PlatformSpecificHelpers.TestCategory)] + public async Task OrchestrationBinding_DirectInvocationValue_ThrowsDirectInvocationError(object? value) + { + ITriggerBinding binding = await CreateOrchestrationBindingAsync(); + + InvalidOperationException exception = await Assert.ThrowsAsync( + () => binding.BindAsync(value!, context: null!)); + + Assert.Equal( + "Durable orchestrator functions do not support direct invocation. " + + "Start an orchestration from a client function by using a Durable client.", + exception.Message); + } + + [Theory] + [MemberData(nameof(DirectInvocationTriggerValues))] + [Trait("Category", PlatformSpecificHelpers.TestCategory)] + public async Task EntityBinding_DirectInvocationValue_ThrowsDirectInvocationError(object? value) + { + ITriggerBinding binding = await CreateEntityBindingAsync(); + + InvalidOperationException exception = await Assert.ThrowsAsync( + () => binding.BindAsync(value!, context: null!)); + + Assert.Equal( + "Durable entity functions do not support direct invocation. " + + "Signal an entity from a client or orchestrator function by using a Durable client.", + exception.Message); + } + + [Theory] + [MemberData(nameof(UnsupportedTriggerValues))] + [Trait("Category", PlatformSpecificHelpers.TestCategory)] + public async Task OrchestrationBinding_UnsupportedValue_PreservesTypeDiagnostic( + object? value, + string expectedType) + { + ITriggerBinding binding = await CreateOrchestrationBindingAsync(); + + ArgumentException exception = await Assert.ThrowsAsync( + () => binding.BindAsync(value!, context: null!)); + + Assert.Contains($"Don't know how to bind to {expectedType}.", exception.Message); + Assert.Equal("value", exception.ParamName); + } + + [Theory] + [MemberData(nameof(UnsupportedTriggerValues))] + [Trait("Category", PlatformSpecificHelpers.TestCategory)] + public async Task EntityBinding_UnsupportedValue_PreservesTypeDiagnostic( + object? value, + string expectedType) + { + ITriggerBinding binding = await CreateEntityBindingAsync(); + + ArgumentException exception = await Assert.ThrowsAsync( + () => binding.BindAsync(value!, context: null!)); + + Assert.Contains($"Don't know how to bind to {expectedType}.", exception.Message); + Assert.Equal("value", exception.ParamName); + } + + private static async Task CreateOrchestrationBindingAsync() + { + DurableTaskExtension extension = CreateExtension(); + var provider = new OrchestrationTriggerAttributeBindingProvider( + extension, + connectionName: "AzureWebJobsStorage", + TestHelpers.GetMockPlatformInformationService()); + var context = new TriggerBindingProviderContext( + GetTriggerParameter(nameof(TestOrchestrator)), + CancellationToken.None); + + return await provider.TryCreateAsync(context) + ?? throw new InvalidOperationException("The orchestration trigger binding was not created."); + } + + private static async Task CreateEntityBindingAsync() + { + DurableTaskExtension extension = CreateExtension(); + var provider = new EntityTriggerAttributeBindingProvider(extension, connectionName: "AzureWebJobsStorage"); + var context = new TriggerBindingProviderContext( + GetTriggerParameter(nameof(TestEntity)), + CancellationToken.None); + + return await provider.TryCreateAsync(context) + ?? throw new InvalidOperationException("The entity trigger binding was not created."); + } + + private static ParameterInfo GetTriggerParameter(string methodName) + { + MethodInfo method = typeof(TriggerBindingProviderTests) + .GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Static) + ?? throw new InvalidOperationException($"The test method '{methodName}' was not found."); + return method.GetParameters()[0]; + } + + private static DurableTaskExtension CreateExtension() + { + var options = new DurableTaskOptions + { + HubName = "TestHub", + WebhookUriProviderOverride = () => new Uri("https://localhost"), + }; + + return new DurableTaskExtension( + new OptionsWrapper(options), + NullLoggerFactory.Instance, + TestHelpers.GetTestNameResolver(), + [ + new AzureStorageDurabilityProviderFactory( + new OptionsWrapper(options), + new TestStorageServiceClientProviderFactory(), + TestHelpers.GetTestNameResolver(), + NullLoggerFactory.Instance, + TestHelpers.GetMockPlatformInformationService()), + ], + new TestHostShutdownNotificationService(), + new DurableHttpMessageHandlerFactory(), + platformInformationService: TestHelpers.GetMockPlatformInformationService()); + } + + private static void TestOrchestrator( + [OrchestrationTrigger] IDurableOrchestrationContext context) + { + } + + private static void TestEntity( + [EntityTrigger] IDurableEntityContext context) + { + } + } +} diff --git a/test/e2e/Tests/Helpers/HttpHelpers.cs b/test/e2e/Tests/Helpers/HttpHelpers.cs index 9de59f6ca..c838bb7b7 100644 --- a/test/e2e/Tests/Helpers/HttpHelpers.cs +++ b/test/e2e/Tests/Helpers/HttpHelpers.cs @@ -5,6 +5,7 @@ using System.Net; using System.Net.Http; using System.Net.Http.Headers; +using System.Text; using System.Threading.Tasks; namespace Microsoft.Azure.Durable.Tests.DotnetIsolatedE2E; @@ -28,6 +29,18 @@ public static async Task InvokeHttpTriggerWithBody(string f return await GetResponseMessage(request); } + public static async Task InvokeAdminFunction(string functionName, string body) + { + using HttpRequestMessage request = new() + { + RequestUri = new Uri($"{Constants.FunctionsHostUrl}/admin/functions/{functionName}"), + Method = HttpMethod.Post, + Content = new StringContent(body, Encoding.UTF8, "application/json"), + }; + + return await GetResponseMessage(request); + } + private static HttpRequestMessage GetTestRequest(string functionName, string queryString = "") { return new HttpRequestMessage diff --git a/test/e2e/Tests/Tests/DirectInvocationTests.cs b/test/e2e/Tests/Tests/DirectInvocationTests.cs new file mode 100644 index 000000000..833b9a7c2 --- /dev/null +++ b/test/e2e/Tests/Tests/DirectInvocationTests.cs @@ -0,0 +1,55 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +using System.Net; +using Xunit; +using Xunit.Abstractions; + +namespace Microsoft.Azure.Durable.Tests.DotnetIsolatedE2E; + +[Collection(Constants.FunctionAppCollectionName)] +public class DirectInvocationTests +{ + private readonly FunctionAppFixture fixture; + + public DirectInvocationTests(FunctionAppFixture fixture, ITestOutputHelper testOutputHelper) + { + this.fixture = fixture; + this.fixture.TestLogs.UseTestLogger(testOutputHelper); + } + + [Theory] + [InlineData( + "HelloCities", + "{\"input\":\"\"}", + "Durable orchestrator functions do not support direct invocation. Start an orchestration from a client function by using a Durable client.")] + [InlineData( + "HelloCities", + "{}", + "Durable orchestrator functions do not support direct invocation. Start an orchestration from a client function by using a Durable client.")] + [InlineData( + "Counter", + "{\"input\":\"\"}", + "Durable entity functions do not support direct invocation. Signal an entity from a client or orchestrator function by using a Durable client.")] + [InlineData( + "Counter", + "{}", + "Durable entity functions do not support direct invocation. Signal an entity from a client or orchestrator function by using a Durable client.")] + [Trait("PowerShell", "Skip")] + [Trait("Python", "Skip")] + [Trait("Node", "Skip")] + [Trait("Java", "Skip")] + public async Task AdminApiDirectInvocation_LogsActionableGuidance( + string functionName, + string requestBody, + string expectedMessage) + { + using HttpResponseMessage response = await HttpHelpers.InvokeAdminFunction(functionName, requestBody); + + Assert.Equal(HttpStatusCode.Accepted, response.StatusCode); + await this.fixture.TestLogs.AssertLogExistsAsync( + log => log.Contains($"Functions.{functionName}", StringComparison.Ordinal) && + log.Contains(expectedMessage, StringComparison.Ordinal), + $"Expected the direct-invocation guidance for '{functionName}' was not found in the host logs."); + } +}