azure-webjobs-sdk-extensions: HttpTrigger binding to POCO fails on Enums

I’ve recently updated my Azure Functions app to v2.0.12050.0 which upgrade the webjobs-sdk-extensions from 3.0.0-beta5 to 3.0.0-beta8.

I have several functions using HttpTrigger in POST binding to a POCO object. The POCO has an enum property and before the upgrade I was able to post content into it and the sdk was correctly binding an int value to the enum. See below for more details.

POCO:

public class Message
{
     public Region RegionId { get; set; }
     public string Content { get; set; }
}

public enum Region
{
     UK = 1
}

Request payload:

{
   RegionId: 1,
   Content: "test"
}

Function declaration: public static async Task ProcessMessage([HttpTrigger(AuthorizationLevel.Anonymous, "POST")]Message message);

Expected behavior

The message property should be populated from the payload and the enum property Region correctly set.

Actual behavior

Invocation breaks with the error:

System.Private.CoreLib: Exception while executing function: AddRequestTweakingWebsiteRule. Microsoft.Azure.WebJobs.Host: Exception binding parameter ‘req’. System.Private.CoreLib: Invalid cast from ‘System.String’ to ‘LoveTheSales.Shared.Enums.Region’.

Stacktrace:

 System.InvalidOperationException: Exception binding parameter 'req' ---> System.InvalidCastException: Invalid cast from 'System.String' to 'Shared.Enums.Region'.
   at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
   at System.String.System.IConvertible.ToType(Type type, IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType)
   at Microsoft.Azure.WebJobs.Extensions.Http.HttpTriggerAttributeBindingProvider.HttpTriggerBinding.ConvertValueIfNecessary(Object value, Type targetType) in C:\azure-webjobs-sdk-extensions\src\WebJobs.Extensions.Http\HttpTriggerAttributeBindingProvider.cs:line 413
   at Microsoft.Azure.WebJobs.Extensions.Http.HttpTriggerAttributeBindingProvider.HttpTriggerBinding.ApplyBindingData(Object target, IDictionary`2 bindingData) in C:\azure-webjobs-sdk-extensions\src\WebJobs.Extensions.Http\HttpTriggerAttributeBindingProvider.cs:line 228
   at Microsoft.Azure.WebJobs.Extensions.Http.HttpTriggerAttributeBindingProvider.HttpTriggerBinding.BindAsync(Object value, ValueBindingContext context) in C:\azure-webjobs-sdk-extensions\src\WebJobs.Extensions.Http\HttpTriggerAttributeBindingProvider.cs:line 174
   at Microsoft.Azure.WebJobs.Host.Triggers.TriggeredFunctionBinding`1.BindCoreAsync(ValueBindingContext context, Object value, IDictionary`2 parameters) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Triggers\TriggeredFunctionBinding.cs:line 57
   --- End of inner exception stack trace ---
   at Microsoft.Azure.WebJobs.Host.Executors.DelayedException.Throw() in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\DelayedException.cs:line 27
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstance instance, ParameterHelper parameterHelper, ILogger logger, CancellationTokenSource functionCancellationTokenSource) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 478
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstance instance, ParameterHelper parameterHelper, IFunctionOutputDefinition outputDefinition, ILogger logger, CancellationTokenSource functionCancellationTokenSource) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 444
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstance instance, FunctionStartedMessage message, FunctionInstanceLogEntry instanceLogEntry, ParameterHelper parameterHelper, ILogger logger, CancellationToken cancellationToken) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 249
   --- End of inner exception stack trace ---
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstance instance, FunctionStartedMessage message, FunctionInstanceLogEntry instanceLogEntry, ParameterHelper parameterHelper, ILogger logger, CancellationToken cancellationToken) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 293

Known workarounds

I can change the HttpTrigger to bind to HttpRequestMessage and deserialize the content manually into my POCO but I was working fine before the upgrade.

Related information

This started to happen when we upgraded from 3.0.0-beta5 to 3.0.0-beta8.

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 4
  • Comments: 28 (5 by maintainers)

Most upvoted comments

Having the same issue. Is this possible to solve in the startup configurations? Or do I have to deserialize it within the azure function?

System.Private.CoreLib: Exception while executing function: PostSomething. 
Microsoft.Azure.WebJobs.Host: One or more errors occurred. (Exception binding parameter 'something') (Exception binding parameter 'req'). 
Exception binding parameter 'something'. System.Private.CoreLib: 
Invalid cast from 'System.String' to 'My.SomethingEnum'.

I will take a shot at this in this Sprint.

This is still an issue in the release version 3.0.0 and is a real big showstopper for our project. @mathewc or @brettsam, could you please comment on this issue?

As @ankitkumarr mentioned, this is a configuration setup issue, but the goo news is that there’s a clear workaround. While the problem is not resolved, please add the following to your function project (in addition to a reference to Microsoft.Azure.Functions.Extensions:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters.Json.Internal;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

[assembly: FunctionsStartup(typeof(MyFunctionApp.FunctionAppStartup))]

namespace MyFunctionApp
{
    public class FunctionAppStartup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
           builder.Services.AddTransient<IConfigureOptions<MvcOptions>, MvcJsonMvcOptionsSetup>();
        }
    }
}

Seems guys messed things a lil bit in 3.0.0-beta8 😃 https://github.com/Azure/azure-webjobs-sdk-extensions/issues/485

Any plans on resolving this? Still an issue for .net6.0 and Microsoft.Net.Sdk.Functions 4.0.1

Seems like lots of people are having this issue and are forced to manually bind the models with enums that comes from querystring.

It appears to also be a problem for net6.0 on Functions V4 (referencing Microsoft.NET.Sdk.Functions 4.1.1) @fabiocav @jeffhollan

@fabiocav @jeffhollan could you at least give some kind of response?