本地Azure Function启动错误连接Azurite非默认端口问题求助
问题解决:Azure Functions本地运行错误连接localhost:5016而非Azurite默认端口
问题现象
Windows10环境下,使用Visual Studio 22运行依赖Blob Storage的.NET6 HTTP Trigger Azure Function,Azurite作为VS服务依赖自动启动(Blob服务127.0.0.1:10000、Queue服务10001、Table服务10002)。配置"AzureWebJobsStorage": "UseDevelopmentStorage=true"后,函数启动时却尝试连接localhost:5016,触发“无法读取Blob Storage Secret Repository”错误。设置"AzureWebJobsSecretStorageType": "files"后能启动,但触发函数时仍出现相同连接错误。已尝试重装VS、Azurite、Azure Core Tools,命令行运行、重启系统、清理Azurite数据等操作,均未解决。
相关日志与配置
Azurite启动日志
Stream.DataLakeTransaction: azurite.cmd --location "\AppData\Local\.vstools\azurite" --debug "AppData\Local\.vstools\azurite\debug.log" --skipApiVersionCheck {ProjectName}: Azurite Blob service is starting at http://127.0.0.1:10000 {ProjectName}: Azurite Blob service is successfully listening at http://127.0.0.1:10000 {ProjectName}: Azurite Queue service is starting at http://127.0.0.1:10001 {ProjectName}: Azurite Queue service is successfully listening at http://127.0.0.1:10001 {ProjectName}: Azurite Table service is starting at http://127.0.0.1:10002 {ProjectName}: Azurite Table service is successfully listening at http://127.0.0.1:10002
local.settings.json配置
{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "dotnet", "FUNCTIONS_EXTENSION_VERSION": "~4" } }
函数启动错误日志
FUNCTIONS_INPROC_NET8_ENABLED app setting is not enabled in local.settings.json Selected inproc6 host. Starting child process for inproc6 model host. C:\Program Files\Microsoft\Azure Functions Core Tools\in-proc6\func.exe present Started child process with ID: 19196 %%%%%% %%%%%% @ %%%%%% @ @@ %%%%%% @@ @@@ %%%%%%%%%%% @@@ @@ %%%%%%%%%% @@ @@ %%%% @@ @@ %%% @@ @@ %% @@ %% % Azure Functions Core Tools Core Tools Version: 4.0.6518 Commit hash: N/A +74ed9095fdd6c5326276aae6b8a7d41ccbdeb6aa (64-bit) Function Runtime Version: 4.35.4.23179 Found {ProjectName.csproj}. Using for user secrets file configuration. [2024-12-03T18:00:30.447Z] Building host: version spec: ~4, startup suppressed: 'False', configuration suppressed: 'False', startup operation id: '16eac4bd-8933-4a55-8ff5-c044ff92701f' [2024-12-03T18:00:30.461Z] Reading host configuration file '..\bin\output\host.json' [2024-12-03T18:00:30.465Z] Host configuration file read: [2024-12-03T18:00:30.466Z] { [2024-12-03T18:00:30.469Z] "version": "2.0", [2024-12-03T18:00:30.471Z] "logging": { [2024-12-03T18:00:30.472Z] "applicationInsights": { [2024-12-03T18:00:30.474Z] "samplingSettings": { [2024-12-03T18:00:30.476Z] "isEnabled": true, [2024-12-03T18:00:30.476Z] "excludedTypes": "Request" [2024-12-03T18:00:30.477Z] } [2024-12-03T18:00:30.477Z] } [2024-12-03T18:00:30.478Z] } [2024-12-03T18:00:30.478Z] } [2024-12-03T18:00:30.500Z] Extension Bundle not loaded. Loading extensions from ..\bin\output\bin. BundleConfigured: False, PrecompiledFunctionApp: False, LegacyBundle: False, DotnetIsolatedApp: False, isLogicApp: False [2024-12-03T18:00:30.503Z] Script Startup resetting load context with base path: '..bin\output\bin'. [2024-12-03T18:00:30.511Z] Loading startup extension 'Startup' [2024-12-03T18:00:30.593Z] Loaded extension 'Startup' (1.0.0.0) [2024-12-03T18:00:30.612Z] Loading startup extension 'DurableTask' [2024-12-03T18:00:30.615Z] Loaded extension 'DurableTask' (2.0.0.0) [2024-12-03T18:00:30.617Z] Loading startup extension 'EventGrid' [2024-12-03T18:00:30.621Z] Loaded extension 'EventGrid' (2.1.0.0) [2024-12-03T18:00:30.648Z] Reading host configuration file '..\bin\output\host.json' [2024-12-03T18:00:30.650Z] Host configuration file read: [2024-12-03T18:00:30.651Z] { [2024-12-03T18:00:30.654Z] "version": "2.0", [2024-12-03T18:00:30.657Z] "logging": { [2024-12-03T18:00:30.658Z] "applicationInsights": { [2024-12-03T18:00:30.660Z] "samplingSettings": { [2024-12-03T18:00:30.661Z] "isEnabled": true, [2024-12-03T18:00:30.663Z] "excludedTypes": "Request" [2024-12-03T18:00:30.665Z] } [2024-12-03T18:00:30.666Z] } [2024-12-03T18:00:30.667Z] } [2024-12-03T18:00:30.669Z] } [2024-12-03T18:00:31.403Z] Using the default storage provider: AzureStorage. [2024-12-03T18:00:32.665Z] Initializing Warmup Extension. [2024-12-03T18:00:32.678Z] Resolved secret storage provider BlobStorageSecretsRepository [2024-12-03T18:01:20.598Z] There was an error performing a read operation on the Blob Storage Secret Repository. [2024-12-03T18:01:20.601Z] Azure.Core: No connection could be made because the target machine actively refused it. (localhost:5016). System.Net.Http: No connection could be made because the target machine actively refused it. (localhost:5016). System.Net.Sockets: No connection could be made because the target machine actively refused it. [2024-12-03T18:01:20.671Z] A host error has occurred during startup operation '16eac4bd-8933-4a55-8ff5-c044ff92701f'. [2024-12-03T18:01:20.674Z] Azure.Core: No connection could be made because the target machine actively refused it. (localhost:5016). System.Net.Http: No connection could be made because the target machine actively refused it. (localhost:5016). System.Net.Sockets: No connection could be made because the target machine actively refused it.
函数代码
using Domain.Models.Shared.Transactions; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Extensions.DurableTask; using Microsoft.Azure.WebJobs.Extensions.Http; using Microsoft.Extensions.Logging; using System; using System.Net.Http; using System.Threading.Tasks; namespace Stream.Transaction { public class TransactionOrchestrationClient { [FunctionName(nameof(TransactionOrchestrationClient))] public async Task TransactionTrigger( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req, [DurableClient] IDurableOrchestrationClient starter, ILogger logger) { try { var data = await req.Content.ReadAsAsync<Transaction>(); var instanceId = await starter.StartNewAsync(nameof(TransactionOrchestrator), data); logger.LogInformation($"Started orchestration with ID = '{instanceId}'."); starter.CreateHttpManagementPayload(instanceId); } catch (Exception ex) { logger.LogError(ex, $"Error occurred while executing {nameof(TransactionOrchestrationClient)}"); } } } }
解决方案
1. 显式指定Azurite完整连接字符串
替换local.settings.json中的AzureWebJobsStorage为Azurite专属连接字符串,强制函数指向正确端口:
"AzureWebJobsStorage": "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;"
2. 清理环境变量冲突
- 检查系统/用户环境变量,删除或修改
AZURE_STORAGE_CONNECTION_STRING(若存在),避免覆盖配置文件设置 - 确认VS调试配置的环境变量中无额外存储连接字符串
3. 重置存储与编译文件
- 手动删除Azurite存储目录(日志路径:
\AppData\Local\.vstools\azurite)下所有文件,彻底重置存储状态 - 删除项目
bin、obj目录,重新编译项目
4. 配置Durable Task明确指向存储
在host.json中添加Durable Task存储配置,确保其使用指定的Azurite连接:
{ "version": "2.0", "logging": { "applicationInsights": { "samplingSettings": { "isEnabled": true, "excludedTypes": "Request" } } }, "extensions": { "durableTask": { "storageProvider": { "connectionStringName": "AzureWebJobsStorage" } } } }
内容的提问来源于stack exchange,提问作者Engin Mustafa
相关产品推荐
相关产品推荐

