我目前正在尝试将 API 存储库从 .net6.0 更新到 .net8.0,并将 Cosmos 数据库从 2.13.1 更新到 3.35.4。我相信数据库升级很好,API 的应用服务也很好。这只是更改提要功能在尝试本地测试时出错。这是第一个更新到 8.0 的存储库,因此这是一次学习经历。 首先,这是我收到的错误。
这是我尝试过的:
我发现 Microsoft.Extensions.Configuration 包在我们的 nuget 包 Columbia.Cosmos.Common 中的域级别定义,以及在 Microsoft.Azure.Functions.Extensions 的函数级别定义。
这是域级别的 csproj 文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
<ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Consumer\**" />
<EmbeddedResource Remove="Consumer\**" />
<None Remove="Consumer\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="cdtservicecommon" Version="[*,9999.0]" />
<PackageReference Include="Columbia.Cosmos.Common" Version="3.1.0" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.20.0" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.46.0" />
<PackageReference Include="Microsoft.NETCore.App" Version="2.2.8" />
</ItemGroup>
</Project>
这是更改提要功能的 csproj 文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.7.0" />
<PackageReference Include="Columbia.ServiceBus.Common" Version="2.0.0" />
<PackageReference Include="csc.azure.datalake.client" Version="4.0.0-preview1" />
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="3.0.10" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.2.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="6.0.3" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
<PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="Polly.Contrib.WaitAndRetry" Version="1.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\cscentconsumer.infrastructure\cscentconsumer.infrastructure.csproj" />
</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.cs:
using CdtServiceCommon.Domain.Interfaces;
using CdtServiceCommon.Infrastructure.Logging;
using Columbia.Cosmos.Common.Extensions;
using Columbia.Cosmos.Common.FaultTolerance;
using csc.azure.datalake.client.Extensions;
using cscentconsumer.domain.Models.Consumer;
using CscEntConsumer.Infrastructure.Configuration;
using CscEntConsumer.Infrastructure.Repository;
using CscEntConsumerFunctions;
using CscEntConsumerFunctions.Configuration;
using CscEntConsumerFunctions.Utilities;
using CscEntConsumerFunctions.Utilities.Interfaces;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
[assembly: WebJobsStartup(typeof(Startup))]
namespace CscEntConsumerFunctions
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
IConfigurationRoot config = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
builder.Services.AddDatalakeClient(options => config.Bind(options));
builder.Services.AddSingleton(config);
builder.Services.AddCosmosDb(
config.GetValue<string>("cosmosdb-connection"),
options =>
{
options.DatabaseId = config["CosmosDb:DatabaseId"];
options.Containers = config["CosmosDb:Containers"].Split(',').ToList();
},
cosmosResiliencySettings: config.GetValue<CosmosResiliencySettings>("CosmosResiliencySettings"));
builder.Services.AddOptions();
builder.Services.Configure<ConsumerServiceDetails>(config.GetSection("ServiceDetails"));
builder.Services.Configure<DataLakeConfiguration>(config);
builder.Services.AddSingleton<IModelMasterRepository<Consumer>, ConsumerMasterRepository>();
builder.Services.AddUpserterAsSingleton<IModelMasterRepository<Consumer>, Consumer, string>();
builder.Services.AddSingleton<ITelemetryInitializer, TelemetryInitializer>();
builder.Services.AddSingleton<IMessageBuilderUtility, MessageBuilderUtility>();
}
}
}
如有任何建议,我们将不胜感激!如果您需要查看其他内容来帮助研究该问题,请告诉我。预先感谢!
将组件版本
Microsoft.NET.Sdk.Functions
升级至 4.5.0,将 Microsoft.Azure.WebJobs.Extensions.CosmosDB
升级至 4.8.1。
在
"FUNCTIONS_INPROC_NET8_ENABLED": "1"
中添加local.settings.json
。
local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_INPROC_NET8_ENABLED": "1",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"COSMOS_CONNECTION": "<CosmosDB_Connection_String>"
}
}
.csproj: 将所有软件包更新到最新版本。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.18.2" />
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="4.8.1" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.16.4" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="9.0.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.5.0" />
<PackageReference Include="Polly" Version="8.5.0" />
<PackageReference Include="Polly.Contrib.WaitAndRetry" Version="1.1.1" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
我已经创建了一个 .NET 8.0 Cosmos DB 触发器 Azure 函数,并且能够获得预期的响应。
Functions:
Function1: cosmosDBTrigger
For detailed output, run func with --verbose flag.
[2024-12-02T11:56:32.345Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
[2024-12-02T11:58:07.764Z] Executing 'Function1' (Reason='New changes on container container1 at 2024-12-02T11:58:07.7394366Z', Id=0997cc56-624d-4e74-9631-2332d3ee63dd)
[2024-12-02T11:58:07.778Z] Documents modified 1
[2024-12-02T11:58:07.781Z] First document Id 1
[2024-12-02T11:58:07.795Z] Executed 'Function1' (Succeeded, Id=0997cc56-624d-4e74-9631-2332d3ee63dd, Duration=50ms)