servicestack

"GenerateCrudServices.DbFactory is not configured" error in scaffolded ServiceStack template


I've just scaffolded a new blazor template with the latest version (v8.0.1) of the ServiceStack x tool using this command:

x new blazor && x mix ef-sqlserver autocrudgen

I get this error when I launch the app:

GenerateCrudServices.DbFactory is not configured

It breaks on this line of the program.cs file: services.AddServiceStack(typeof(MyServices).Assembly);

What am I missing?

I'm thinking that there needs to be something like this setting of the DbFactory in the Configure.AutoQuery.cs file:

public void Configure(IWebHostBuilder builder) => builder
   .ConfigureServices(services => {
       services.AddPlugin(new AutoQueryFeature {
          MaxLimit = 1000,
           IncludeTotal = true,
           GenerateCrudServices = new GenerateCrudServices {
               DbFactory = ** [Obtain IDbConnectionFactory from app services here]
                        something like Services.GetService/ResolveService<IDbConnectionFactory> **
               AutoRegister = true,
           }
       });
   });

But I can't figure out how to get at the IDbConnectionFactory to make the above approach work.

Please advise?


Solution

  • When using Endpoint Routing with AutoGen you'd need to configure GenerateCrudServices with the same IDbConnectionFactory instance that's registered.

    Easiest way to do this would be to register the AutoQueryFeature in Configure.Db.cs:

    public class ConfigureDb : IHostingStartup
    {
        public void Configure(IWebHostBuilder builder) => builder
            .ConfigureServices((context, services) => {
                var connString = context.Configuration
                   .GetConnectionString("DefaultConnection")
                    ?? "DataSource=App_Data/app.db;Cache=Shared";
    
                var dbFactory = new OrmLiteConnectionFactory(            
                    connString,
                    SqliteDialect.Provider);
    
                container.AddSingleton<IDbConnectionFactory>(c => dbFactory);
    
                services.AddPlugin(new AutoQueryFeature {
                    MaxLimit = 1000,
                    GenerateCrudServices = new GenerateCrudServices {
                        DbFactory = dbFactory,
                    }
                });
                  
                // $ dotnet ef migrations add CreateIdentitySchema
                // $ dotnet ef database update
                services.AddDbContext<ApplicationDbContext>(o =>             
                    o.UseSqlite(connString,b=>b.MigrationsAssembly("MyApp")));
                // Enable built-in Database Admin UI at /admin-ui/database
    
                services.AddPlugin(new AdminDatabaseFeature());
            });
    }