I have an azure table and queue created and I have code that will take the messages in the queue and insert it into the table. The code works but how do I account for different data entries? For eg. ID:Name:Age:Vaccinated is the main input but a user inputs Name:Age:ID:Vaccinated.
[FunctionName("Function1")]
public void Run([QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem, ILogger log)
{
try
{
bool isVac = false;
log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
// Gets the ID from the myQueueItem
string ID = myQueueItem.Substring(0, myQueueItem.IndexOf(getString));
log.LogInformation($"ID: {ID}");
myQueueItem = getNewString(myQueueItem);
log.LogInformation($"New String: {myQueueItem}");
string name = myQueueItem.Substring(0, myQueueItem.IndexOf(getString));
log.LogInformation($"Name: {name}");
myQueueItem = getNewString(myQueueItem);
log.LogInformation($"New String: {myQueueItem}");
string age = myQueueItem;
log.LogInformation($"Age: {age}");
// Gets the vaccination status from the myQueueItem
string vac = myQueueItem.Substring(0, myQueueItem.IndexOf(getString));
log.LogInformation($"Vac: {vac}");
if (vac.ToUpper() == "TRUE")
{
isVac = true;
}
AddToTable(ID, name, int.Parse(age), isVac);
}
catch (System.Exception ex)
{
log.LogInformation(ex.Message);
}
}
private void AddToTable(string ID, string name, int age, bool isVac)
{
string connectionString ="";
TableClient tableClient = new TableClient(connectionString, "Vac");
tableClient.CreateIfNotExists();
var entity = new Person()
{
PartitionKey = "South Africa",
RowKey = ID,
Name = name,
Age = age,
Vacinated = isVac
};
tableClient.AddEntity(entity);
}
I tried a method to get each value from the string and reformat it but I had no luck on that (I don't have the code anymore).
CodePudding user response:
What you need to do is pass a json as the message content. Then, once you deserialize it, it doesn't matter the order as long as the name of the properties remain the same.
public class Person
{
public string PartitionKey => "South Africa";
[JsonProperty("rowKey")]
public string RowKey { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("age")]
public string Age { get; set; }
[JsonProperty("vacinated")]
public bool Vacinated { get; set; }
}
[FunctionName("Function1")]
public void Run([QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem, ILogger log)
{
try
{
var model = JsonConvert.DeserializeObject<Person>(myQueueItem);
AddToTable(model);
}
catch (System.Exception ex)
{
log.LogInformation(ex.Message);
}
}
private void AddToTable(Person model)
{
string connectionString ="";
TableClient tableClient = new TableClient(connectionString, "Vac");
tableClient.CreateIfNotExists();
tableClient.AddEntity(model);
}