Answered by:
Problem in hosting Worker Service in Azure

Question
-
User611346789 posted
Dear all,
I have created a scheduler using ASP.NET Core 3.1 Worker Service and using EntityFramework to connect the database. Everything is working fine in my Local Development environment from Visual studio. But when I am hosting that worker service as Azure Web Job in App service then nothing happens. I don't get any log message in the Azure App Service Log. Then I created another worker service project which is default project template provided by the visual studio and host that to the Azure as Web Job and that works.
Then I have commented on all codes from the first project and only keep the default Worker Service and then again publish to azure and that time it starts working. So now my question is is this problem happening for DB Connection? I have also used Serilog for logging.
Below is the code of Program.cs
using System; using System.IO; using DataAccess.Data; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog; using SSPM.Synchronizer.Core; using SSPM.Synchronizer.Persistence; namespace SSPM.Synchronizer { public class Program { public static IConfiguration Configuration { get; } = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true) .Build(); public static void Main(string[] args) { Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(Configuration) .CreateLogger(); try { CreateHostBuilder(args).Build().Run(); } catch (Exception e) { Log.Error(e,"On Startup"); } } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseSerilog() .UseWindowsService() .ConfigureServices((hostContext, services) => { services.AddDbContext<CoreDbContext>(opt => { opt.UseSqlServer(hostContext.Configuration.GetConnectionString("DBCS")); }); services.AddHostedService<EventsSynchronizer>(); //services.AddHostedService<Worker>(); services.AddSingleton<ISomeService, SomeService>(); services.AddSingleton<ISome1Service, Some1Service>(); }); } }
Can anyone please help me to solve this problem?
Thursday, July 9, 2020 5:15 PM
Answers
-
User348806598 posted
You can try enabling application insight and do some logging to check what the issues may be. You can also use kudu console to see any errors you might be facing. Please check also if you have appropriate connection string and other settings that you have in webapp's application settings. Your application may not be able to read configuration keys that you have specified.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 9, 2020 5:22 PM -
User611346789 posted
Thanks a lot for your message. The main reason of the above problem was the application can not read appsetting.json file and Serilog causes the problem
Below links might help others to set up the config file
https://www.devfuel.net/Post/dotnet-core-30-PublishSingleFile-cant-find-appsettingsjson
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, July 10, 2020 1:06 AM
All replies
-
User348806598 posted
You can try enabling application insight and do some logging to check what the issues may be. You can also use kudu console to see any errors you might be facing. Please check also if you have appropriate connection string and other settings that you have in webapp's application settings. Your application may not be able to read configuration keys that you have specified.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 9, 2020 5:22 PM -
User611346789 posted
Thanks a lot for your message. The main reason of the above problem was the application can not read appsetting.json file and Serilog causes the problem
Below links might help others to set up the config file
https://www.devfuel.net/Post/dotnet-core-30-PublishSingleFile-cant-find-appsettingsjson
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, July 10, 2020 1:06 AM