azure 函数,同一个 .net 项目中的多个触发器
azure functions, Multiple triggers in same .net project
我是 azure 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 函数项目中添加多个触发器。
I added it to my project and ran it and it never fired, even when files were created.
你可以关注这个article。
[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 名称模式。
您只需要将samples-workitems
修改为您的container name
。并在 local.hosting.json
文件中设置 blob 连接。
我是 azure 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 函数项目中添加多个触发器。
I added it to my project and ran it and it never fired, even when files were created.
你可以关注这个article。
[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 名称模式。
您只需要将samples-workitems
修改为您的container name
。并在 local.hosting.json
文件中设置 blob 连接。