如何用C#代码覆盖Azure Function App中host.json的functiontimeout?是否可行?
functionTimeout in host.json via C# code in an Azure Function App? Great question! The short answer is yes, but directly modifying the host.json file via C# code at runtime isn't the right approach—instead, we have better, more maintainable ways to achieve custom functionTimeout behavior in Azure Functions. Let's break this down:
Why modifying host.json directly in code isn't feasible
- The
host.jsonconfiguration is loaded only once when the Functions host starts up. Any changes you make to the file at runtime won't take effect until the host restarts. - In consumption plans, the function app's file system is ephemeral—changes will be lost when the app scales down, restarts, or moves to a new instance. Even in dedicated plans, modifying configuration files directly via code is bad practice for consistency and maintainability.
Recommended approach: Use App Settings to override functionTimeout
You can override the functionTimeout value from host.json using an application setting (environment variable). This takes precedence over the file's value, and you can even manage this setting programmatically (with the right Azure permissions).
Step 1: Set the override via App Settings
Add an app setting with the key AzureFunctionsJobHost__functionTimeout (note the double underscores, which map to nested configuration paths in .NET). Set its value to your desired timeout (e.g., 00:10:00 for 10 minutes). This will immediately take precedence over the value in host.json the next time the host starts.
Step 2: Read and use the value in C# code
If you want to reference this timeout in your function logic (e.g., to enforce custom timeout behavior), inject IConfiguration to read the value:
using Microsoft.Extensions.Configuration; using Microsoft.Azure.WebJobs; using Microsoft.Extensions.Logging; using System.Threading; using System.Threading.Tasks; public class CustomTimeoutFunction { private readonly IConfiguration _config; public CustomTimeoutFunction(IConfiguration config) { _config = config; } [FunctionName("CustomTimeoutFunction")] public async Task Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log) { // Read the overridden timeout from app settings if (TimeSpan.TryParse(_config["AzureFunctionsJobHost:functionTimeout"], out var functionTimeout)) { log.LogInformation($"Current effective function timeout: {functionTimeout}"); // Use the timeout to enforce custom cancellation logic using var cts = new CancellationTokenSource(functionTimeout); try { await ExecuteLongRunningTask(cts.Token); } catch (OperationCanceledException) { log.LogWarning("Function execution timed out per custom configuration"); } } else { log.LogWarning("Could not parse function timeout value—falling back to host.json default"); } } private async Task ExecuteLongRunningTask(CancellationToken cancellationToken) { // Simulate a long-running operation await Task.Delay(TimeSpan.FromMinutes(12), cancellationToken); } }
Granular per-function timeouts
If you need different timeouts for individual functions (rather than a global host-level timeout), skip the host configuration altogether and implement custom timeout logic directly in your function code using CancellationTokenSource. This gives you full control over when the timeout triggers, independent of the host's settings.
Critical timeout limits to remember
Azure Functions enforces maximum timeout values based on your hosting plan—you can't exceed these, no matter what configuration you set:
- Consumption Plan: Maximum 10 minutes (hard limit, cannot be extended)
- Dedicated (App Service) Plan: Maximum 60 minutes
- Premium Plan: Maximum 60 minutes (default is 30 minutes)
内容的提问来源于stack exchange,提问作者NarsingRao

