WebVella/WebVella-ERP

Incompatible with Microsoft.Extensions.Hosting?

Closed this issue · 2 comments

The new ASP.NET 3.1 Core Web Application templates use the following code in Program.cs

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }

When using this code WebVella can't be run as there is a NullReferenceException in WebVella.Erp.Jobs.ErpJobScheduleService line 11 trying to call ProcessScheduleAsync on ScheduleManager.Current

            ScheduleManager.Current.ProcessSchedulesAsync(stoppingToken);

image

To fix this replace the code in Program.cs to use Microsoft.AspNetCore.Hosting as follows:

	public class Program {
		public static void Main(string[] args) {
			BuildWebHost(args).Run();
		}

		public static IWebHost BuildWebHost(string[] args) =>
		   WebHost.CreateDefaultBuilder(args)
			   .UseStartup<Startup>()
			   .Build();
	}

It looks like some sort of timing issue perhaps Startup.ConfigureServices isn't given time to complete before Startup.Configure is called because its run Lazily

The problem is that background services start before Startup.Configure even called. So I changed ErpJobScheduleService and ErpJobProcessService to delay processing until ScheduleManager and JobManager singletons are initialized. ( nuget is not yet updated with that change)

The problem is that background services start before Startup.Configure even called. So I changed ErpJobScheduleService and ErpJobProcessService to delay processing until ScheduleManager and JobManager singletons are initialized.

Thanks that is a good fix and will work for me