![](/img/trans.png)
[英]Can the same thread run multiple functions simultaneously in Azure Functions?
[英]azure functions, Multiple triggers in same .net project
我是天藍色的Web函數的新手,我似乎找不到關於同一項目中多個觸發器的任何文檔。 我已經創建了解決方案,並且已經創建了一個不錯的TimerTrigger ,它可以正常工作。
此觸發器從ftp目錄下載文件,然后將文件上傳到我們的azure存儲帳戶。 代碼如下:
[DependencyInjectionConfig(typeof(DependencyInjectionConfig))]
public class FtpTrigger
{
[FunctionName("EOrderTimerTrigger")]
public static async Task Run(
[TimerTrigger("*/15 * * * * *")]TimerInfo myTimer,
TraceWriter log,
[Inject]IConfig config,
[Inject]SmartFileClient smartFileClient,
[Inject]SettingsHandler settingsHandler,
[Inject]StorageHandler storageHandler)
{
if (myTimer.IsPastDue)
log.Info("Timer is running late!");
log.Info($"Listing directories from { config.FtpUrl }");
var accounts = await smartFileClient.ListDirectories(config.FtpPath);
if (!accounts.Any())
{
log.Warning($"There are no files waiting to be processed for any account");
return;
}
foreach (var account in accounts)
{
try
{
log.Info($"Retrieving settings for { account }");
var url = $"{config.ApiBaseUrl}/{config.ApiSettingsPath}";
var settings = await settingsHandler.GetAsync(url, account);
log.Info($"Find all order files for { account }");
var fileNames = await smartFileClient.ListFiles($"{config.FtpPath}/{account}", settings.OrderFileSuffix.Replace(".", ""));
if (!fileNames.Any())
{
log.Warning($"No files to process for { account }");
continue;
}
log.Info($"Get a list of files awaiting to be processed for { account }");
var awaiting = await storageHandler.ListAsync(config.StorageProcessingContainer, account);
foreach(var fileName in fileNames)
{
log.Info($"Finding any files awaiting to be processed in the storage account for { account }");
var friendlyName = Regex.Replace(fileName, "[^a-zA-Z0-9_.]+", "", RegexOptions.Compiled); ;
var match = awaiting.Any(m => m.Equals(friendlyName));
if (match)
{
log.Warning($"File ({fileName}) already awaiting to be processed for { account }");
continue;
}
log.Info($"Download { fileName } from the ftp directory for { account }");
var bytes = await smartFileClient.DownloadFile($"{config.FtpPath}/{account}", fileName);
log.Info($"Upload { fileName } to the Azure Storage account for { account }");
await storageHandler.UploadAsync(friendlyName, bytes, config.StorageProcessingContainer, account);
log.Info($"Delete { fileName } from the ftp directory for { account }");
if (!await smartFileClient.DeleteFile($"{config.FtpPath}/{account}", fileName))
log.Error($"Failed to delete { fileName } from the ftp directory for { account }");
}
} catch (Exception ex)
{
log.Error($"{ ex.Message }");
}
}
log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
}
}
現在,我想添加第二個觸發器。 該觸發器將是BlobTrigger 。 我將其添加到我的項目中並運行它,即使創建了文件,它也從未觸發過。 所以我意識到我一定做錯了。
有人可以告訴我如何在一個項目中使用多個觸發器嗎? 如果無法完成, 有什么選擇?
實際上,您可以在同一個Azure功能項目中添加多個觸發器。
我將其添加到我的項目中並運行它,即使創建了文件,它也從未觸發過。
您可以閱讀本文 。
[FunctionName("Function2")]
public static void Run([BlobTrigger("samples-workitems/{name}", Connection = "blobconnect")]Stream myBlob, string name, TraceWriter log)
{
log.Info($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
}
注意 :Blob觸發路徑samples-workitems / {name}中的字符串{name}創建一個綁定表達式,您可以在函數代碼中使用該綁定表達式來訪問觸發Blob的文件名。 有關更多信息,請參見本文后面的Blob名稱模式。
您只需要將samples-workitems
修改為您的container name
。 並在local.hosting.json
文件中設置Blob連接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.