You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Azure部署基于Hangfire的Worker Service的最佳方案及相关疑问

Deploying Hangfire-based Worker Service to Azure: App Service vs Web Jobs

1. Recommendation: App Service or Web Jobs?

  • Azure App Service is the preferred choice if you need access to the Hangfire dashboard, want flexible scaling options, or plan to run other web components alongside your worker. It’s easier to manage, offers better visibility into service health, and integrates smoothly with Azure’s monitoring tools.
  • Azure Web Jobs (Continuous type) works for lighter-weight scenarios where you don’t need the Hangfire dashboard, or if you want to attach the worker to an existing App Service without spinning up a separate resource. Note that Web Jobs are being phased out in favor of Azure Functions, but they still function for legacy use cases.

2. Deployment Steps for Azure App Service

  • Prepare your project: Ensure your Worker Service is configured with Hangfire using an Azure-compatible storage (Azure SQL Database, Azure Redis Cache, etc.). Update appsettings.json with the corresponding connection strings.
  • Enable Always On: Before deploying, navigate to your App Service’s Configuration > General settings and toggle "Always On" to On. This prevents the service from shutting down due to idle time (required for paid tiers; not available on Free tier).
  • Deploy the service:
    • Using Visual Studio: Right-click your project → Publish → Select Azure → Azure App Service (Windows/Linux) → Choose an existing App Service or create a new one. Configure connection strings in the publish settings, then click Publish.
    • Using Azure CLI: Run az webapp deployment source config-local-git --name <app-service-name> --resource-group <resource-group> to set up Git deployment, then push your published code to the generated Git URL.
  • Verify deployment: Check the App Service’s Log Stream to confirm Hangfire initializes correctly. If you enabled the Hangfire dashboard, navigate to <app-service-url>/hangfire (ensure you secure it with authentication).

3. Deployment Steps for Azure Web Jobs

  • Create an App Service: Web Jobs are tied to an existing App Service, so first create one if you don’t have it already.
  • Package your Worker Service: Publish your project as a self-contained executable. Run dotnet publish -c Release -r <target-runtime> --self-contained true (replace <target-runtime> with win-x64, linux-x64, etc.).
  • Zip the published files: Compress all files in the bin/Release/net<version>/<target-runtime>/publish directory into a ZIP archive.
  • Upload to Web Jobs:
    • Go to your App Service → Web Jobs → Add.
    • Name your Web Job, select the ZIP file, set Type to "Continuous", and enter the command to run your executable (e.g., MyWorkerService.exe for Windows, ./MyWorkerService for Linux).
  • Configure settings: Add your Hangfire storage connection strings in the App Service’s Configuration > Connection Strings section.
  • Enable Always On: Ensure "Always On" is enabled for the parent App Service to keep the continuous Web Job running.

4. Will App Service stop during long idle periods?

By default, yes. App Service has an idle timeout of 20 minutes for most tiers. To prevent this, enable the Always On setting in your App Service’s configuration. Note that Always On is only available in Basic, Standard, Premium, and Isolated tiers—Free and Shared tiers don’t support this, so your service will shut down after idle time if using those.

5. Relevant Documentation

  • Microsoft’s official guides cover deploying .NET Worker Services to Azure App Service, including configuration for continuous running and connection string management.
  • Hangfire’s official documentation provides details on setting up Azure-compatible storage providers and securing the Hangfire dashboard.

内容的提问来源于stack exchange,提问作者Khandakar Rashed Hassan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 22:35:18