.net core worker service how to DI boilerplate libraries?

Viewed 131

I have created a worker service, in this service I need to use other implemented services which happen to be using classes from abp domain like the IRepository. now I want to configure the (WorkerModule.cs, Program.cs) so I can use Dependency Injection of abp classes. I followed the demo on github and the documentation step by step, still keeps giving me exception saying: "Value can not be null" at the line: "abpBootstrapper.Initialize();" in my Program.cs.

this is my Program.cs code:

public class Program
{
    public static void Main(string[] args)
    {
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
            .Enrich.FromLogContext()
            .WriteTo.File(@"C:\temp\workerservice\LogFile.txt")
            .CreateLogger();

        try
        {
            Log.Information("Starting up the service");
            CreateHostBuilder(args).Build().Run();
            return;
        }
        catch (Exception ex)
        {
            Log.Fatal(ex, "There was a problem starting the service");
            return;
        }
        finally
        {
            Log.CloseAndFlush();
        }
    }

    public static IHostBuilder CreateHostBuilder(string[] args)
    {
        return Host.CreateDefaultBuilder(args)
            .ConfigureServices((hostContext, services) =>
            {
                var abpBootstrapper = AbpBootstrapper.Create<WorkerModule>();
                services.AddSingleton(abpBootstrapper);
                abpBootstrapper.Initialize();

                services.AddHostedService<Worker>();
            })
            .UseWindowsService()
            .UseCastleWindsor(IocManager.Instance.IocContainer)
            .UseSerilog();
    }
}

and this is my WorkerModule.cs code:

[DependsOn(
    typeof(AbpZeroCoreModule),
    typeof(AbpZeroLdapModule),
    typeof(AbpAutoMapperModule),
    typeof(AbpAspNetZeroCoreModule),
    typeof(AbpMailKitModule),
    typeof(AbpEntityFrameworkCoreModule),
    typeof(AbpRedisCacheModule)
    )]
public class WorkerModule : AbpModule
{
    private readonly IConfigurationRoot _appConfiguration;

    public WorkerModule()
    {
        _appConfiguration = AppConfigurations.Get(Assembly.GetExecutingAssembly().GetDirectoryPathOrNull());
    }

    public override void PreInitialize()
    {
        Configuration.DefaultNameOrConnectionString = _appConfiguration.GetConnectionString(MyAppConsts.ConnectionStringName);

        var builder = new DbContextOptionsBuilder<MyDbContext>();

        MyDbContextConfigurer.Configure(builder, Configuration.DefaultNameOrConnectionString);

        IocManager.IocContainer.Register(
           Component
               .For<DbContextOptions<MyDbContext>>()
               .Instance(builder.Options)
               .LifestyleTransient()
       );
    }

    public override void Initialize()
    {
        IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
    }
}
0 Answers
Related