.NET 8 独立函数在不运行函数代码的情况下触发

问题描述 投票:0回答:1

我最近使用 .NET 6 创建了一个 Azure Function 应用程序 (BlobTrigger),并且运行良好。然后我决定迁移到 .NET 8 并开始遇到以下问题。我尝试了各种解决方案,包括从头开始创建一个新的 .NET 8 函数应用程序,以尝试消除依赖性问题,但没有任何运气。基本上触发器记录它已成功执行但没有实际运行:

visual studio

我目前没有选择,不确定如何解决此问题,存储帐户是 StorageV2(通用 v2),但我看不出这是一个问题,因为应用程序在 .NET 6 上运行良好?

包含的套餐有:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Azure.Data.AppConfiguration" Version="1.4.1" />
    <PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.3.1" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.20.1" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Blobs" Version="6.2.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.4" />
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.1.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="7.2.0" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
  </ItemGroup>
</Project>

我一直在尝试启动这家公司,但这就是我目前正在运行的:

using Azure.Data.AppConfiguration;
using Azure.Extensions.AspNetCore.Configuration.Secrets;
using Azure.Identity;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Identity.Client;

namespace ViyaTransferService
{
    public record ConfidentialClientAppScopes(string[] Scopes);

    internal class Program
    {
        public static async Task Main()
        {
            var config = InitializeConfiguration(new ConfigurationBuilder());

            var host = ConfigureHostServices(config)
                .Build();

            Console.WriteLine($"running host");
            await host.RunAsync();
        }

        public static IHostBuilder ConfigureHostServices(IConfiguration config)
        {
            Console.WriteLine($"loading services");
            return new HostBuilder()
                .ConfigureFunctionsWorkerDefaults()
                .ConfigureFunctionsWebApplication()
                .ConfigureAppConfiguration(config => { config.AddUserSecrets<Program>(optional: true, reloadOnChange: false); })
                .ConfigureServices(services =>
                {
                    services.AddApplicationInsightsTelemetryWorkerService();
                    services.ConfigureFunctionsApplicationInsights();
                    services.AddScoped(x => new ConfigurationClient(Environment.GetEnvironmentVariable("AppConfigConnectionString")));
                    services.AddScoped<IFileTransferService, FileTransferService>();

                    services.AddSingleton(x => ConfidentialClientApplicationBuilder...
                        .Build()
                    );

                    services.AddSingleton(x => new ConfidentialClientAppScopes(...) + "/.default" }));

                    services.AddAzureClients(clientBuilder =>
                    {
                        clientBuilder.AddBlobServiceClient(Environment.GetEnvironmentVariable("ViyaStorageConnectionString"));
                    });

                    services.AddLogging();
                })
                .ConfigureLogging(logging =>
                {
                    ...
                });
        }

        static IConfiguration InitializeConfiguration(IConfigurationBuilder builder)
        {
            string? startupenv = Environment.GetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT");
            Console.WriteLine($"startupenv: {startupenv}");

            string cs = Environment.GetEnvironmentVariable("AppConfigConnectionString");
            builder.AddAzureAppConfiguration(cs);

            if (startupenv != "Development" && !string.IsNullOrEmpty(keyVaultUrl) && !string.IsNullOrEmpty(clientId))
            {
                Console.WriteLine($"loading keyvault");
                builder.AddAzureKeyVault(...);
            }

            Console.WriteLine($"loaded config");
            return builder.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true).AddEnvironmentVariables().Build();
        }
    }
}

如前所述,我尝试在 .NET 中创建一个新应用程序,我还尝试将自定义连接字符串更改为默认的

AzureWebJobsStorage
,我添加了尝试在启动中添加
AddBlobServiceClient
作为
AzureClient
,我'我尝试查看日志记录以确认触发器正在执行,我尝试覆盖该文件并添加多个文件,认为这可能是一个文件问题(之前使用的文件名称中带有空格)和其他一些问题比如更换套餐等

c# azure-functions .net-8.0 azure-blob-trigger azure-functions-isolated
1个回答
0
投票

当您的触发器内的操作无法正常工作并且它成功执行触发器时。可能是

processfile
功能无法正常工作。

确保检查任何异常,我建议使用

Try
Catch
方法。

这是我的简单文件传输代码

using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using System.IO;

namespace FunctionApp4
{
    public class Function
    {
        private readonly ILogger<Function> _logger;
        private readonly IFileTransferService _fileTransferService;

        public Function(ILogger<Function> logger, IFileTransferService fileTransferService)
        {
            _logger = logger;
            _fileTransferService = fileTransferService;
        }

        [Function("Function")]
        public void Run([BlobTrigger("test/{name}",Connection = "AzureWebJobsStorage")] Stream blobstream, string name)
        {
            using var blobStreamReader = new StreamReader(blobstream);
            var content = blobStreamReader.ReadToEnd();

            _logger.LogInformation($"C# Blob trigger function Processed blob\n Name: {name} \n Data: {content}");

            var sourcePath = "C:\\Users\\Vivek\\Documents\\upload.txt";
            var destinationPath = ".\\upload.txt";

            var transferResult = _fileTransferService.TransferFileAsync(sourcePath, destinationPath);

            _logger.LogInformation("Trigger executed successfully");
           

        }
    }

    public interface IFileTransferService
    {
        Task<bool> TransferFileAsync(string sourcePath, string destinationPath);
    }

    public class FileTransferService : IFileTransferService
    {
        public async Task<bool> TransferFileAsync(string sourcePath, string destinationPath)
        {
            
            try
            {
                File.Copy(sourcePath, destinationPath, true);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error transferring file: {ex.Message}");
                return false;
            }
        }
    }

}
using FunctionApp4;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
    .ConfigureServices(services =>
    {
        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();
        services.AddSingleton<IFileTransferService, FileTransferService>();
    })
    .Build();

host.Run();

OUTPUT

我的

upload.txt
文件已从源复制到我的
FunctionApp4\bin\Debug\net8.0
文件夹。

© www.soinside.com 2019 - 2024. All rights reserved.