这是错误:
经过大量浪费时间,我终于将问题范围缩小到这行代码:
public class Startup : FunctionsStartup
{
public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
// If I comment out this line of code the project will run
builder.ConfigurationBuilder.AddEnvironmentVariables();
}
public override void Configure(IFunctionsHostBuilder builder)
{
// ...
}
}
我发现如果我引用 builder.ConfigurationBuilder ,就会出现错误。我看到了一篇与此类似的帖子,但它是针对孤立的,而这不是孤立的。
最初我在另一台笔记本电脑上将此项目从 .net 6 更新到 .net 8,我相信它工作得很好。但是在获得一台新笔记本电脑并再次从源代码控制中获取该项目后,我遇到了这个问题。
我已确保引用了正确版本的 Microsoft.Extensions.Configuration.Abstractions,但我仍然收到此错误。
我还启动了一个全新的项目,并添加了一个具有相同行代码的 Startup 类,它产生了相同的结果。我应该有什么不同的方式在 .net 8 中添加启动代码吗?
如果
<Nullable>enable</Nullable>
中有 .csproj
,请将其删除。
有时可以通过删除 bin 和 obj 文件夹来解决此错误,按照GitHub问题中所述重新重建项目。
更新您的 .csproj,如下所示:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
我使用 Startup 类创建了一个 .NET 8.0 进程内 Azure 函数,并实现了您的代码,如下所示。
创业班:
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
[assembly: FunctionsStartup(typeof(FunctionApp.Startup))]
namespace FunctionApp
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton<IMyService, MyService>();
}
public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
builder.ConfigurationBuilder.AddEnvironmentVariables();
}
}
public interface IMyService
{
string GetMessage();
}
public class MyService : IMyService
{
public string GetMessage() => "Hello from MyService!";
}
}
函数.cs:
public class Function1
{
private readonly IMyService _myService;
private readonly IConfiguration _configuration;
// Constructor injects both the service and configuration
public Function1(IMyService myService, IConfiguration configuration)
{
_myService = myService;
_configuration = configuration;
}
[FunctionName("Function1")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
log.LogInformation($"Message from MyService: {_myService.GetMessage()}");
string mySetting = _configuration["Setting1"];
log.LogInformation($"Configuration value 'Setting1': {mySetting}");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
string responseMessage = $"Hi {name}, This HTTP triggered function executed successfully." ;
return new OkObjectResult(responseMessage);
}
}
local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_INPROC_NET8_ENABLED": "1",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"Setting1": "Value"
}
}
控制台输出:
Functions:
Function1: [GET,POST] http://localhost:7146/api/Function1
For detailed output, run func with --verbose flag.
[2024-11-28T10:04:27.285Z] Host lock lease acquired by instance ID '000000000000xxxxCC'.
[2024-11-28T10:04:32.875Z] Executing 'Function1' (Reason='This function was programmatically called via the host APIs.', Id=694d84e8-f229-4f00-af98-812cfb115b30)
[2024-11-28T10:04:32.936Z] C# HTTP trigger function processed a request.
[2024-11-28T10:04:32.941Z] Message from MyService: Hello from MyService!
[2024-11-28T10:04:32.946Z] Configuration value 'Setting1': Value
[2024-11-28T10:04:33.062Z] Executed 'Function1' (Succeeded, Id=694d84e8-f229-4f00-af98-812cfb115b30, Duration=276ms)