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>();
});
}
}
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.
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
Member
134 Points
86 Posts
Problem in hosting Worker Service in Azure
Jul 09, 2020 05:15 PM|emrulkayes2103|LINK
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
Can anyone please help me to solve this problem?
All-Star
20953 Points
4984 Posts
Re: Problem in hosting Worker Service in Azure
Jul 09, 2020 05:22 PM|asteranup|LINK
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.
Anup Das Gupta
Visit My Blog
You can also connect with me in LinkedIn
Member
134 Points
86 Posts
Re: Problem in hosting Worker Service in Azure
Jul 10, 2020 01:06 AM|emrulkayes2103|LINK
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
https://stackoverflow.com/questions/58292980/how-to-setup-serilog-seq-for-net-core-3-0-worker-service