Error converting 1 input parameters for Function: Cannot convert input parameter 'token' to type 'System.Threading.CancellationToken' #2245
Replies: 5 comments
-
What version of nuget packages are you using? For your code, I got the below code generated. Is that not what you are seeing? Can you share the full code in the public async ValueTask ExecuteAsync(FunctionContext context)
{
var inputBindingFeature = context.Features.Get<IFunctionInputBindingFeature>();
var inputBindingResult = await inputBindingFeature.BindFunctionInputAsync(context);
var inputArguments = inputBindingResult.Values;
if (string.Equals(context.FunctionDefinition.EntryPoint, "GH2245ServiceBusTrigger.Function1.Run", StringComparison.Ordinal))
{
var instanceType = types["GH2245ServiceBusTrigger.Function1"];
var i = _functionActivator.CreateInstance(instanceType, context) as global::GH2245ServiceBusTrigger.Function1;
await i.Run((string)inputArguments[0], (global::System.Threading.CancellationToken)inputArguments[1]);
}
} Below are the relevant nuget package versions I used when I tried to repro <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.16.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.4" /> |
Beta Was this translation helpful? Give feedback.
-
I don't have all of the lines from the public async ValueTask ExecuteAsync(FunctionContext context)
{
var inputBindingFeature = context.Features.Get<IFunctionInputBindingFeature>();
var inputBindingResult = await inputBindingFeature.BindFunctionInputAsync(context);
var inputArguments = inputBindingResult.Values;
if (string.Equals(context.FunctionDefinition.EntryPoint, "MyFunction.Function1.Run", StringComparison.Ordinal))
{
var instanceType = types["MyFunction.Function1"];
var i = _functionActivator.CreateInstance(instanceType, context) as global::MyFunction.Function1;
i.Run((global::Microsoft.Azure.Functions.Worker.TimerInfo)inputArguments[0]);
}
else if (string.Equals(context.FunctionDefinition.EntryPoint, "MyFunction.Function2.Run", StringComparison.Ordinal))
{ I replaced the function namespace and names in the code. As you can see, there are two functions in the function app. One being the service bus triggered one and the other being a timered function. The issue is with the service bus triggered function. Here are the NuGet packages I'm using: <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.20.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.15.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.3.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.4" /> The project is running on .NET 8. |
Beta Was this translation helpful? Give feedback.
-
This code is source generated. You can find it under "Microsoft.Azure.Functions.Worker.Sdk.Generators" -> Microsoft.Azure.Functions.Worker.Sdk.Generators.FunctionExecutionGenerator -> GeneratedFunctionExecutor.g.cs |
Beta Was this translation helpful? Give feedback.
-
But I think that the code looks exactly like yours, with the only difference that there's an var inputBindingResult = await inputBindingFeature.BindFunctionInputAsync(context); |
Beta Was this translation helpful? Give feedback.
-
Here's the full source code generated locally: // <auto-generated/>
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Context.Features;
using Microsoft.Azure.Functions.Worker.Invocation;
namespace MyFunction
{
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class DirectFunctionExecutor : IFunctionExecutor
{
private readonly IFunctionActivator _functionActivator;
private readonly Dictionary<string, Type> types = new Dictionary<string, Type>()
{
{ "MyFunction.Heartbeat", Type.GetType("MyFunction.Heartbeat, MyFunction, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null") },
{ "MyFunction.HttpRequest", Type.GetType("MyFunction.HttpRequest, MyFunction, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null") }
};
public DirectFunctionExecutor(IFunctionActivator functionActivator)
{
_functionActivator = functionActivator ?? throw new ArgumentNullException(nameof(functionActivator));
}
/// <inheritdoc/>
public async ValueTask ExecuteAsync(FunctionContext context)
{
var inputBindingFeature = context.Features.Get<IFunctionInputBindingFeature>();
var inputBindingResult = await inputBindingFeature.BindFunctionInputAsync(context);
var inputArguments = inputBindingResult.Values;
if (string.Equals(context.FunctionDefinition.EntryPoint, "MyFunction.Heartbeat.Run", StringComparison.Ordinal))
{
var instanceType = types["MyFunction.Heartbeat"];
var i = _functionActivator.CreateInstance(instanceType, context) as global::MyFunction.Heartbeat;
i.Run((global::Microsoft.Azure.Functions.Worker.TimerInfo)inputArguments[0]);
}
else if (string.Equals(context.FunctionDefinition.EntryPoint, "MyFunction.HttpRequest.Run", StringComparison.Ordinal))
{
var instanceType = types["MyFunction.HttpRequest"];
var i = _functionActivator.CreateInstance(instanceType, context) as global::MyFunction.HttpRequest;
await i.Run((string)inputArguments[0], (global::System.Threading.CancellationToken)inputArguments[1]);
}
}
}
/// <summary>
/// Extension methods to enable registration of the custom <see cref="IFunctionExecutor"/> implementation generated for the current worker.
/// </summary>
public static class FunctionExecutorHostBuilderExtensions
{
///<summary>
/// Configures an optimized function executor to the invocation pipeline.
///</summary>
public static IHostBuilder ConfigureGeneratedFunctionExecutor(this IHostBuilder builder)
{
return builder.ConfigureServices(s =>
{
s.AddSingleton<IFunctionExecutor, DirectFunctionExecutor>();
});
}
}
/// <summary>
/// Auto startup class to register the custom <see cref="IFunctionExecutor"/> implementation generated for the current worker.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
public class FunctionExecutorAutoStartup : IAutoConfigureStartup
{
/// <summary>
/// Configures the <see cref="IHostBuilder"/> to use the custom <see cref="IFunctionExecutor"/> implementation generated for the current worker.
/// </summary>
/// <param name="hostBuilder">The <see cref="IHostBuilder"/> instance to use for service registration.</param>
public void Configure(IHostBuilder hostBuilder)
{
hostBuilder.ConfigureGeneratedFunctionExecutor();
}
}
} |
Beta Was this translation helpful? Give feedback.
-
I've implemented an Azure Function based on the Isolated model. The function processes message from a topic subscription on Azure Service Bus. The definition on the
Run
method looks similar to this:The function is running and processing messages. But I see errors like this logged from time to time:
The error seems to happen in some generated source code:
I don't understand why the generated code is trying to parse the
CancelationToken
as JSON. I already have the JSON sent over the service bus as the first parameter.Any help would be appreciated.
(copy from: https://stackoverflow.com/questions/77898023/error-converting-1-input-parameters-for-function-cannot-convert-input-parameter)
Beta Was this translation helpful? Give feedback.
All reactions