This question is similar to Azure Blob Storage trigger Function not firing
However, their problem was that their Azure Function wasn't awaking immediately, giving the impression it wasn't processing triggers from Azure Blob Storage when in fact it was after 10 minutes which is exactly as the MS docs claim.
https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob-trigger?tabs=csharp
My problem is different. My blob has been sitting in the container now for 9 hours and it still hasn't been processed.
All it does is post a message onto ServiceBus.
[FunctionName("IncomingFileDetected")]
[return: ServiceBus("incoming-file-received", EntityType = Microsoft.Azure.WebJobs.ServiceBus.EntityType.Topic)]
public static IncomingFile Run(
[BlobTrigger("incoming-files/{filename}", Connection = "ConnectionStrings:MutableStorage")]
Stream contents,
string filename,
ILogger log)
{
log.LogInformation($"Detected new blob file: {filename}");
return new IncomingFile(filename);
}
No messages have appeared in the service bus.
Now, after 9 hours, I have restarted the function app and the blob was processed within about 10 minutes.
Update:
Thanks for Peter Morris's sharing, the problem comes from is service plan is d1. So first make sure you are based on the three kinds of plans: consumption plan, premium plan and app service plan. When we use azure function, even only test, we should use a consumption plan. The smallest in production is S1, which is normally used for testing.
Original Answer:
The below code works fine on my side. Even the consumption plan is no problem.
using System;
using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
namespace FunctionApp35
{
public static class Function1
{
[FunctionName("Function1")]
[return: ServiceBus("test", Connection = "ServiceBusConnection")]
public static string Run([BlobTrigger("samples-workitems/{name}", Connection = "str")]Stream myBlob, string name, ILogger log)
{
log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
string a = "111111111111111";
return a;
}
}
}
This is my local settings:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=lti/ThmF+mw9BebOacp9gVazIh76Q39ecikHSCkaTcGK5hmInspX+EkjzpNmvCPWsnvapWziHQHL+kKt2V+lZw==;EndpointSuffix=core.windows.net",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"str": "DefaultEndpointsProtocol=xxxxxx",
"ServiceBusConnection": "Endpoint=sb://bowmantestxxxxxx"
}
}
The str is from this place:
The ServiceBusConnection is from this place:
And please notice that the blob will will not be removed from container after the azure function is triggered. Also, dont forget to create at least one subscription in your service bus topic.
All all the above also works fine after the function be deployed to azure.(The difference from local is you need to add settings in configuration settings instead of local.settings.json)
Related
I have a queue in a service bus. After putting a message into a queue an azure logic app and an azure functions should betriggered and process the content.
My Azure logic app is triggered but my azure funcction is not triggered. My code for azure function:
[FunctionName("ReadMEssageFromQueue")]
public static void Run([ServiceBusTrigger("messagequeue", Connection = "AzureWebJobsStorage")]string myQueueItem, ILogger log)
{
log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
}
host json:
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"AzureWebJobsStorage": "******" // connection string of my service bus
}
}
should I set something in service bus queue to send message to both ressources?
Azure Service Bus Queue messages are picked up by only one processor. So I think in your case, the logic app is picking up and consuming the message first and the message is not available for the function to process. You can try temporarily disabling the logic app and letting function pick the message to confirm this.
Ref: azure-service-bus-queue-with-multiple-listeners
You can trigger the Azure function from your logic app (not sure if it'll help your use case), or you can use Azure Service Bus topics as topics support the model where multiple consumers can subscribe to a topic.
The former option might be a better approach for you from cost perspective, as you'd need to use Standard tier of service bus in order to use topics feature, which means additional cost for you over your current setup.
Also, you might want to use some other name for service bus connection string as AzureWebJobsStorage is used for storage account connection string
I am trying to trigger an Azure function when a new queue message is added. Both the storage account and the azure function are in the same region.
For my Azure Function, I clicked on Add, Azure Queue Storage Trigger, I gave my function a name, and the Queue name is the same name as my queue. I tried adding a new queue message, nothing is triggered.
I then tried modifying the code as the following:
using System;
[FunctionName("QueueTrigger")]
[StorageAccount("storagetestaccount1")]
public static void Run(
[QueueTrigger("queue1")] string myQueueItem,
ILogger log)
{
log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
}
But still no success. Any idea what might be causing this?
This is my first azure function so not sure what's correct and what's not.
I think the correct code is this:
public static class Function1
{
[FunctionName("Function1")]
public static void Run([QueueTrigger("queueName", Connection = "connectString")]string myQueueItem, ILogger log)
{
log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
}
}
Note
If you develop locally, you should config your azure storage connect string in local.settings.json
If you develop in azure portal, you need to config connect string in Application settings:
I have created a blob trigger azure function which uses connection string in the code at the moment.
local.settings.json
public static class BlobTrigger_Fun
{
[FunctionName("BlobTrigger_Fun")]
public static void Run([BlobTrigger("democontainerazure/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, ILogger log)
{
log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
}
}
I want to use managed identity to avoid use of connection string in the code.
No, you can't.
The MSI(managed identity) is not for such usage, it is just used for authenticating to azure services that support Azure AD authentication, the AzureWebJobsStorage is used for azure function runtime, in the function app, the property must be specified as an app setting in the site configuration.
I have two blob triggers which I want to trigger on. One works and one does not!
I use Azure Storage Explorer to make sure blobs are uploaded to each blob, scanFiles will never trigger, and scanExports seem to always trigger.
Question: What can cause some blobs to not trigger an Azure function?
[FunctionName("scanFiles")]
public static async Task FilesScan([BlobTrigger("files/{newBlobName}", Connection = "BlobConnectionstring")]CloudBlockBlob newBlob, string newBlobName, ILogger log, ExecutionContext context)
{
await VirusScan(newBlob, newBlobName, log, context);
}
[FunctionName("scanExports")]
public static async Task ExportsScan([BlobTrigger("exports/{newBlobName}", Connection = "BlobConnectionstring")]CloudBlockBlob newBlob, string newBlobName, ILogger log, ExecutionContext context)
{
await VirusScan(newBlob, newBlobName, log, context);
}
I found that the issue was because one of the blobs were considered High scale, which triggers this bug and also makes the host process kill itself when run locally (see this issue)
I have an Azure Service Bus with Service Bus Topic trigger. My function looks something like this
[FunctionName("SbListener")]
public static async Task Run(
[ServiceBusTrigger("test-topic", "test-sub-1", Connection = "ServiceBus")]string message,
[Inject("Microsoft.EventStore.Functions", true)] IWebNotificationManagerFactory webNotificationManagerFactory,
[Inject("Microsoft.EventStore.Functions", true)] ILogger logger)
{ ... }
The configuration for my Service Bus is in the local.settings.json file.
"ConnectionStrings": {
"ServiceBus": "Endpoint=sb://<my-sb>.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=<my-key>"
}
What I am looking for is that I want to refer the topic names from the configuration file as well, rather than hard-coding them in the ServiceBusTrigger. The problem is that in case I change the subscription name then I would have to re-deploy Function code (I want to avoid this at all cost).
Put topic and subscription in Values in local.settings.json(Application settings in portal) and reference them using app setting binding expressions--wrap the app setting name with %, check the doc.
[ServiceBusTrigger("%Topic%", "%Subscription%", Connection = "ServiceBus")]string message
Besides, I would suggest you put ServiceBus connection string in Values as well, ConnectionStrings is used by frameworks that typically get connection strings from the ConnectionStrings section of a configuration file, such as Entity Framework. See the doc.