When I am calling my azure function with HTTP POST and a json body the deserialization is not working for a decimal as I expected.
I am hosting the azure function locally and in the request body I am transferring a json object with a decimal in it.
{
"Receiver": {
"Name1": "Mr. Homer Simpson",
"AddressLine1": "742 Evergreen Terrace",
"ZipCode": "AEED",
"City": "Springfield",
"CountryCode": "US"
},
"ReferenceNumber": "US1939383",
"Weight": 4.2
}
public class LabelInformation
{
public ParcelAddress? Receiver { get; set; }
/// <summary>
/// Invoice number. TODO Should be renamed.
/// </summary>
public string? ReferenceNumber { get; set; }
/// <summary>
/// Total weight of the parcel.
/// </summary>
public decimal? Weight { get; set; }
}
public class ParcelAddress
{
public string? Name1 { get; set; }
public string? AddressLine1 { get; set; }
public string? ZipCode { get; set; }
public string? City { get; set; }
/// <summary>
/// Country 2 letter ISO code.
/// </summary>
public string? CountryCode { get; set; }
}
[FunctionName("GenerateLabelGLSFunctionHttpTrigger")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "label/gls")]
LabelInformation info)
{
...
}
Changing the type of info to string and then manually deserialize the string works as expected.
[FunctionName("GenerateLabelGLSFunctionHttpTrigger")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "label/gls")]
string info)
{
var labelInformation = JsonConvert.DeserializeObject<LabelInformation>(info);
_logger.LogInformation("create the label.");
GlsSoap.ShipmentRequestData payload = _labelService.CreateShipmentRequestData(labelInformation);
The error I receive is
[09.10.2019 10:28:38] Executed 'GenerateLabelGLSFunctionHttpTrigger' (Failed, Id=90330456-1ac2-43f3-9285-ab2284b6c31f)
[09.10.2019 10:28:38] System.Private.CoreLib: Exception while executing function: GenerateLabelGLSFunctionHttpTrigger. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'info'. System.Private.CoreLib: Input string was not in a correct format.
[09.10.2019 10:28:38] fail: Host.Results[0]
Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: GenerateLabelGLSFunctionHttpTrigger ---> System.InvalidOperationException: Exception binding parameter 'info' ---> System.FormatException: Input string was not in a correct format.
at System.Number.ParseSingle(ReadOnlySpan`1 value, NumberStyles options, NumberFormatInfo numfmt)
at System.String.System.IConvertible.ToSingle(IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
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 415
...
I expect the automatic deserialization not to use locale information (on my OS it is German - if I change to English everything is working as expected) for deserializing a decimal.
Or please explain me, why this should be good, as functions can be hosted on different locales and a caller to that function would need to know where the function is deployed to take into account the correct decimal seperator.