Reputation: 155
I'm working in Azure Queue storage and Azure functions queue trigger. I'm using the c# console project to send a class object as a message to the Azure queue and then setting up a queue trigger function to consume a class object in a queue.
But when I run it, it keeps showing that :
Executed 'Function1' (Failed, Id=9134f8f9-0399-4049-a351-a5613b17aa12, Duration=35ms)[2023-06-10T07:38:48.866Z]
System.Private.CoreLib: Execution while executing function: MyQueueFunction. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'message'. System.Private.CoreLib: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an i illegal character among the padding characters.
This is the console code i used to send message to Azure Queue
public class Parameters
{
public string? Usersid { get; set; }
}
class Program
{
static async Task Main(string[] args)
{
string connectionString = "<connectstring>";
string queueName = "<Queuename>";
QueueClientOptions options = new QueueClientOptions();
options.Diagnostics.IsLoggingEnabled = false;
QueueClient queueClient = new QueueClient(connectionString, queueName, options);
var message = new Parameters
{
Usersid = "Kth7",
};
string jsonMessage = JsonConvert.SerializeObject(message);
byte[] messageBytes = Encoding.UTF8.GetBytes(jsonMessage);
string EncodedMessage = Convert.ToBase64String(messageBytes);
try
{
await queueClient.SendMessageAsync(EncodedMessage);
Console.WriteLine("Message sent");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to send the message. Error: {ex.Message}");
}
}
}
The code I used to consume messages via Azure Function Queue Trigger
public static class Function1
{
[FunctionName("Function1")]
public static async Task Run([QueueTrigger("<queuename>", Connection = "<constring>")]string message, ILogger log)
{
byte[] messageBytes = System.Convert.FromBase64String(message);
string decodedMessage = Encoding.UTF8.GetString(messageBytes);
Parameters classObject = JsonConvert.DeserializeObject<Parameters>(decodedMessage);
log.LogInformation($"Received message: userid={classObject.Usersid}");
await Task.CompletedTask;
}
}
I added encode and decode after I got this error but still, I'm getting this error. Do I need to change anything in my code? how to solve this error?
Thanks.
Upvotes: 0
Views: 568
Reputation: 11113
I have reproduced in my environment and got expected results as below:
My console application code:
using Azure.Storage.Queues;
using Newtonsoft.Json;
using System.Text;
public class Parameters
{
public string? Name { get; set; }
}
class Program
{
static async Task Main(string[] args)
{
string cs = "DefaultEndpointsProtocol=https;AccountName=rith;AccountKey=BT2tkeA==;EndpointSuffix=core.windows.net";
string qn = "rithwik";
QueueClientOptions o = new QueueClientOptions();
o.Diagnostics.IsLoggingEnabled = false;
QueueClient queueClient = new QueueClient(cs, qn, o);
var msg = new Parameters
{
Name = "Rithwik Bojja",
};
string j = JsonConvert.SerializeObject(msg);
byte[] m = Encoding.UTF8.GetBytes(j);
string EncodedMessage = Convert.ToBase64String(m);
try
{
await queueClient.SendMessageAsync(EncodedMessage);
Console.WriteLine("Message sent Rithwik, Please Check");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to send the message and the Error is : {ex.Message}");
}
}
}
Now Azure Function App code:
using System;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace FunctionApp31
{
public class Function1
{
private readonly ILogger _logger;
public Function1(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<Function1>();
}
public class MyM
{
public string Name { get; set; }
}
[Function("Function1")]
public void Run([QueueTrigger("rithwik", Connection = "con")] string myQueueItem)
{
_logger.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
MyM message = JsonConvert.DeserializeObject<MyM>(myQueueItem);
_logger.LogInformation($"Received message: Name={message.Name}");
}
}
}
Now Firstly run the Azure Function app and the run or send the message using Console app:
Console app output:
Function App Output:
Try to follow above process and get the desired results as I have got.
Upvotes: 0