ThomasArdal
ThomasArdal

Reputation: 5259

Error converting 1 input parameters for Function: Cannot convert input parameter 'token' to type 'System.Threading.CancellationToken'

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:

[Function("MyFunction")]
public async Task Run(
    [ServiceBusTrigger("topic", "subscription", Connection = "connectionString")]string mySbMsg,
    CancellationToken token)

The function is running and processing messages. But I see errors like this logged from time to time:

Microsoft.Azure.Functions.Worker.FunctionInputConverterException: Error converting 1 input parameters for Function 'MyFunction': Cannot convert input parameter 'token' to type 'System.Threading.CancellationToken' from type 'System.String'. Error:System.Text.Json.JsonException: 'p' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
 ---> System.Text.Json.JsonReaderException: 'p' is an invalid start of a value. LineNumber: 0 | BytePositionInLine: 0.
   at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan`1 bytes)
   at System.Text.Json.Utf8JsonReader.ConsumeValue(Byte marker)
   at System.Text.Json.Utf8JsonReader.ReadFirstToken(Byte first)
   at System.Text.Json.Utf8JsonReader.ReadSingleSegment()
   at System.Text.Json.Utf8JsonReader.Read()
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   --- End of inner exception stack trace ---
   at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& state, JsonReaderException ex)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.ContinueDeserialize(ReadBufferState& bufferState, JsonReaderState& jsonReaderState, ReadStack& readStack)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.DeserializeAsync(Stream utf8Json, CancellationToken cancellationToken)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.DeserializeAsObjectAsync(Stream utf8Json, CancellationToken cancellationToken)
   at Microsoft.Azure.Functions.Worker.Converters.JsonPocoConverter.GetConversionResultFromDeserialization(Byte[] bytes, Type type) in D:\a\_work\1\s\src\DotNetWorker.Core\Converters\JsonPocoConverter.cs:line 66
   at Microsoft.Azure.Functions.Worker.Context.Features.DefaultFunctionInputBindingFeature.BindFunctionInputAsync(FunctionContext context) in D:\a\_work\1\s\src\DotNetWorker.Core\Context\Features\DefaultFunctionInputBindingFeature.cs:line 94
   at MyFunction.DirectFunctionExecutor.ExecuteAsync(FunctionContext context) in D:\a\1\s\src\MyFunction\Microsoft.Azure.Functions.Worker.Sdk.Generators\Microsoft.Azure.Functions.Worker.Sdk.Generators.FunctionExecutorGenerator\GeneratedFunctionExecutor.g.cs:line 32
   at Microsoft.Azure.Functions.Worker.OutputBindings.OutputBindingsMiddleware.Invoke(FunctionContext context, FunctionExecutionDelegate next) in D:\a\_work\1\s\src\DotNetWorker.Core\OutputBindings\OutputBindingsMiddleware.cs:line 13

The error seems to happen in some generated source code:

/// <inheritdoc/>
public async ValueTask ExecuteAsync(FunctionContext context)
{
    var inputBindingFeature = context.Features.Get<IFunctionInputBindingFeature>();
    // ⬇️ The error happens when calling BindFunctionInputAsync
    var inputBindingResult = await inputBindingFeature.BindFunctionInputAsync(context);
    var inputArguments = inputBindingResult.Values;

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.

Upvotes: 3

Views: 1504

Answers (2)

ym185
ym185

Reputation: 56

I came across the same issue and found when I renamed the Parameter name to cancellationToken it resolved.

  public async Task Run(
   [ServiceBusTrigger("topic", "subscription", Connection = "connection", IsSessionsEnabled = true)]
          ServiceBusReceivedMessage message,
   ServiceBusMessageActions messageActions,
      int deliveryCount,
      CancellationToken cancellationToken)
  {
      await ProcessMessageAsync(message, messageActions, deliveryCount, cancellationToken);
  }

Upvotes: 2

Mitch Bukaner
Mitch Bukaner

Reputation: 181

While facing the same error I found this post and people saying updating fixed it.

https://github.com/Azure/azure-functions-dotnet-worker/issues/2040

It did not solve it 100% for me as it works from VS and in the devops pipeline, but not when running from a script in my local machine.

I hope it helps.

Upvotes: 0

Related Questions