.Net Framework 类库未启动 Startup.cs [关闭]

问题描述 投票:0回答:0

我创建了这个 Startup.cs 文件:

[assembly: OwinStartup(typeof(ExcelAddIn.Startup))]
namespace ExcelAddIn
{
    public partial class Startup {

        public static IServiceProvider Services { get; set; }
        public static IConfiguration Config { get; set; }
        public void Configuration(IAppBuilder app) {           
            var builder = new ConfigurationBuilder()
            .AddJsonFile($"{AppDomain.CurrentDomain.BaseDirectory}appsettings.json", optional: true, reloadOnChange: true);
            Config = builder.Build();
            var services = new ServiceCollection();            
            ConfigureServices(services);
            Services = services.BuildServiceProvider();            
        }

        public void ConfigureServices(IServiceCollection services)
        {

            services.AddDbContext<AppDbContext>(options =>
               options.UseSqlServer(Config["Connections:AppConnection"]), ServiceLifetime.Transient);

            
            //services.AddTransient<DAL.Business.Home>();
        }
    }
}

但是这个 startup.cs 在我的项目中不起作用并且 AppDbContext 配置不起作用。 如何引发这堂课?

注意:这个启动在 Asp.Net Web Forms 中工作,我已经有了 EntityFrameworkCore 2 和 .Net Framework 4.8 的这个文件,那么为什么它不能与 .Net Framework 类库一起工作。

我通过从这个方法调用 Owin Startup 类解决了我的问题:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {           
            Microsoft.Owin.Hosting.StartOptions options = new Microsoft.Owin.Hosting.StartOptions()
            { AppStartup = "ExcelAddIn.Startup" };
            Microsoft.Owin.Hosting.WebApp.Start<ExcelAddIn.Startup>(options);
            context = ExcelAddIn.Startup.Services.GetService<AppDbContext>();
            if (context.Database.GetPendingMigrations().Any())
            {
                context.Database.Migrate();
            }
            this.Application.WorkbookNewSheet += Application_WorkbookNewSheet1;
                      
        }

在类库项目中。

c# owin startup class-library
© www.soinside.com 2019 - 2024. All rights reserved.