question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HttpTrigger binding to POCO fails on Enums

See original GitHub issue

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.

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
jansorencommented, Oct 19, 2018

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'.
4reactions
ankitkumarrcommented, Jun 13, 2019

I will take a shot at this in this Sprint.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Serializing Enum as string using attribute in Azure ...
I have tried getting an HTTP-trigger in an Azure Functions 3.0/3.1 app to return the string-representation of enums without any luck.
Read more >
Azure Tables input bindings for Azure Functions
The following example shows a C# function that reads a single table row. For every message sent to the queue, the function will...
Read more >
Different Ways to Parse Http Request Data in Http-triggered ...
Accessing HTTP Request Metadata When Auto-binding to POCOs. If you want to do automatic binding and also want to get request metadata, you...
Read more >
How to serialize enum as string in Function App running on ...
Coding example for the question How to serialize enum as string in Function App running on .net5 with OpenAPI/Swagger?
Read more >
Category: Application Architecture
The framework installs extensions into ASP.Net Core that adjust model metadata and binding (including from request bodies – that ASP.Net Core ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found