我是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 。我将它添加到我的项目并运行它,即使创建了文件也从未触发过。 所以我意识到我一定做错了。
有人可以告诉我如何在项目中使用多个触发器吗?如果不能做到;有什么替代方案?
答案 0 :(得分:0)
实际上,您可以在同一个天蓝色功能项目中添加多个触发器。
我将它添加到我的项目中并运行它并且它从未被触发,即使创建了文件也是如此。
您可以关注此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触发器路径中的字符串{name} samples-workitems / {name}创建一个绑定表达式,您可以在函数代码中使用该表达式来访问触发blob的文件名。有关详细信息,请参阅本文后面的Blob名称模式。
您只需将samples-workitems
修改为container name
即可。并在local.hosting.json
文件中设置blob连接。