3

In my ASP.NET Core app Iused the following code to initialize and seed database, especially for testing. Example below.

ASP.NET Core example:

protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            string entityName = $"Data Source=master_data.{typeof(TTestedEntity).Name}.db";

            builder.ConfigureServices(services =>
            {
                ServiceProvider serviceProvider = new ServiceCollection()
                    .AddEntityFrameworkSqlite()
                    .BuildServiceProvider();

                services.AddDbContext<MasterDataContext>(options =>
                {
                    options.UseSqlite(entityName);
                    options.UseInternalServiceProvider(serviceProvider);
                });

                ServiceProvider sp = services.BuildServiceProvider();

                using (IServiceScope scope = sp.CreateScope())
                {
                    IServiceProvider scopedServices = scope.ServiceProvider;
                    MasterDataContext db = scopedServices.GetRequiredService<MasterDataContext>();
                    ILogger<DiLibMasterDataWebApplicationFactory<TStartup, TTestedEntity>> logger = scopedServices
                        .GetRequiredService<ILogger<DiLibMasterDataWebApplicationFactory<TStartup, TTestedEntity>>>();

                    db.Database.EnsureDeleted();
                    db.Database.EnsureCreated();

                    MasterDataSeed.Seed(db);
                }
            });

I want to use something similar in Azure Durable Functions on my local machine for testing, playing purposes. But it cases this error. The documentation says the following.

Is the quoted text refers to that I want to do? I mean does it says that, hey, coder, don't do that because there are other stuff you are not aware of and you might find yourself in a crazy situation!

The startup class is meant for only setup and registration. Avoid using services registered at startup during the startup process. For instance, don't try to log a message in a logger that is being registered during startup. This point of the registration process is too early for your services to be available for use. After the Configure method is run, the Functions runtime continues to register additional dependencies, which can affect how your services operate.

If no then what is the solution for this?

The error message linked above mention two types which aren't there. I haven't found these types anywhere.

private void ConfigureControlPanel(IFunctionsHostBuilder builder)
        {

            builder.Services.AddDbContext<ControlPanelContext>(options =>
                {
                    options.UseSqlite("Data Source=test.db");
                });
            builder.Services.AddTransient<ModuleValidator>();
            builder.Services.AddTransient<IControlPanelBusinessLogic, ControlPanelBusinessLogic>();

            ServiceProvider sp = builder.Services.BuildServiceProvider();

            using (IServiceScope serviceScope = sp.CreateScope())
            {
                IServiceProvider scopedServices = serviceScope.ServiceProvider;

                ControlPanelContext controlPanelContext = scopedServices.GetRequiredService<ControlPanelContext>();

                controlPanelContext.Database.EnsureDeleted();
                controlPanelContext.Database.EnsureCreated();

                Module m1 = new Module { Name = "asd", Description = "asdd", ModuleRoute = "asd"};
                Module m2 = new Module { Name = "asd2", Description = "asdd2", ModuleRoute = "asd2"};

                controlPanelContext.Modules.Add(m1);
                controlPanelContext.Modules.Add(m2);

            }
        }
AndrasCsanyi
  • 3,943
  • 8
  • 45
  • 77
  • Create a hosted service and do your scoped functionality when it is started. – Nkosi Oct 12 '19 at 16:13
  • Sadly hosted services are not supported for Azure Functions. – Martijn Lentink Jul 01 '21 at 08:14
  • I know this is an old thread, but what did you end up doing as a solution? I am torn between either writing an explicit deployment script/project vs doing the setup steps in Startup like you are recommending above. – ellison Apr 26 '23 at 12:53
  • I ended up not using Durable Functions for other reasons. Probably the community have solutions for this. I haven't followed Durable Functions in the last 3 years. – AndrasCsanyi Apr 26 '23 at 13:06

0 Answers0