解决Azure Functions中「未找到作业函数」报错问题
Azure Functions 本地运行提示“No job functions found”问题排查与解决
环境信息
- Azure Functions Core Tools版本:4.0.5455(64位)
- Function Runtime版本:4.27.5.21554
- 目标框架:net7.0
- Functions 模型:隔离进程模型(Worker SDK)
错误日志
[2023-12-25T14:17:48.218Z] Found /Users/username/Downloads/Programming/ProjectName/ProjectName.csproj. Using for user secrets file configuration. [2023-12-25T14:17:49.193Z] No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).
项目代码
项目文件(ProjectName.csproj)
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net7.0</TargetFramework> <AzureFunctionsVersion>v4</AzureFunctionsVersion> <OutputType>Exe</OutputType> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.20.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.4" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\MealzNow.Core\MealzNow.Core.csproj" /> <ProjectReference Include="..\MealzNow.Services\MealzNow.Services.csproj" /> <ProjectReference Include="..\MealzNow.Db\MealzNow.Db.csproj"> <GlobalPropertiesToRemove></GlobalPropertiesToRemove> </ProjectReference> </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>
Program.cs
using MealzNow.Services; using MealzNow.Db; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using var loggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); }); var logger = loggerFactory.CreateLogger<Program>(); try { var config = new ConfigurationBuilder() .AddJsonFile("local.settings.json", optional: true) .AddEnvironmentVariables() .Build(); var cosmosDbAccount = config.GetValue<string>("CosmosDb:Account"); var cosmosDbKey = config.GetValue<string>("CosmosDb:Key"); var cosmosDbDatabaseName = config.GetValue<string>("CosmosDb:DatabaseName"); var host = new HostBuilder() .ConfigureServices((context, services) => { services.AddDbContext<MealzNowDataBaseContext>(options => options.UseCosmos(cosmosDbAccount, cosmosDbKey, cosmosDbDatabaseName)); services.AddRepositories(config); services.AddServices(config); services.AddAutoMapper(typeof(Program)); }).ConfigureFunctionsWorkerDefaults() .Build(); await host.RunAsync(); } catch (Exception ex) { logger.LogCritical("JW: " + ex.Message); }
函数类(MealzNowMobAppApi.cs)
using System.Net; using System.Text.Json; using MealzNow.Core.RequestModels; using MealzNow.Services.Interfaces; using Microsoft.Azure.Functions.Worker; using Microsoft.Azure.Functions.Worker.Http; using Microsoft.Extensions.Logging; using Newtonsoft.Json; namespace MealzNow.MobApi { public class MealzNowMobAppApi { private readonly ILogger _logger; private readonly IAppService _appService; public MealzNowMobAppApi(ILoggerFactory loggerFactory, IAppService appService) { _logger = loggerFactory.CreateLogger<MealzNowMobAppApi>(); _appService = appService; } [Function(nameof(GetClientFranchises))] public async Task<HttpResponseData> GetClientFranchises([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req) { _logger.LogInformation("Calling GetClientFranchises funtion"); var content = await new StreamReader(req.Body).ReadToEndAsync(); if (content == null) return req.CreateResponse(HttpStatusCode.BadRequest); var request = JsonConvert.DeserializeObject<CommonRequest>(content); if (request == null) return req.CreateResponse(HttpStatusCode.BadRequest); if (request.Id == Guid.Empty) return req.CreateResponse(HttpStatusCode.BadRequest); var data = await _appService.GetClientFranchises(request.Id.Value); var response = req.CreateResponse(HttpStatusCode.OK); await response.WriteAsJsonAsync(data); return response; } } }
可行解决方案
1. 确认访问修饰符完整性
检查函数类和方法是否均为public,确保没有代码被误设为internal或其他非公开修饰符。
2. 清理重建项目
- 删除项目目录下的
bin和obj文件夹 - 执行命令:
dotnet clean && dotnet build - 重启Azure Functions Core Tools,重新运行
func start
3. 验证host.json配置
确保host.json无错误配置,标准隔离进程模型的host.json示例:
{ "version": "2.0", "logging": { "applicationInsights": { "samplingSettings": { "isEnabled": true, "excludedTypes": "Request" } } } }
4. 检查项目引用与程序集加载
如果函数类位于其他项目,确认项目引用正确,且被引用项目的输出类型为类库(Class Library),构建后能被主函数项目正确加载。
5. 更新.NET SDK与工具包
确保本地安装的.NET 7 SDK为最新稳定版,同时更新Microsoft.Azure.Functions.Worker和Microsoft.Azure.Functions.Worker.Sdk到兼容的最新版本,避免版本冲突。
内容的提问来源于stack exchange,提问作者Haseeb Rajpoot
相关产品推荐
相关产品推荐

