Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET核心Web Api中的初始化步骤放在何处?_C#_Docker_Asp.net Core Webapi_Iis Express - Fatal编程技术网

C# ASP.NET核心Web Api中的初始化步骤放在何处?

C# ASP.NET核心Web Api中的初始化步骤放在何处?,c#,docker,asp.net-core-webapi,iis-express,C#,Docker,Asp.net Core Webapi,Iis Express,如果您熟悉生产代码,您总是会遇到在服务中请求/处理任何内容之前需要调用的逻辑 我个人将它包装成这样的东西,并从DI框架调用它: public interface IMigrator { Task UpdateAsync(); } 例如: 从代码迁移数据库 静态缓存初始化 种子应用于数据库 根帐户的身份验证 我目前还不太精通WebAPI框架,我需要知道,应该把这种逻辑放在哪里?在启动中。配置?是否恰当?如果需要10分钟,事情会怎样 需要您的建议: public class Star

如果您熟悉生产代码,您总是会遇到在服务中请求/处理任何内容之前需要调用的逻辑

我个人将它包装成这样的东西,并从DI框架调用它:

public interface IMigrator
{
    Task UpdateAsync();
}
例如:

  • 从代码迁移数据库
  • 静态缓存初始化
  • 种子应用于数据库
  • 根帐户的身份验证
我目前还不太精通WebAPI框架,我需要知道,应该把这种逻辑放在哪里?在
启动中。配置
?是否恰当?如果需要10分钟,事情会怎样

需要您的建议:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services
            .AddControllers();
            services.Register(new CoreModule(), new DataModule());
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "api",
                pattern: "api/v1/{controller}/{action}/{id?}");
        });

        //<- somewhere here? does it break some startup logic in IIS like winservices does when process takes longer than 1 minute?
        await _migrator.UpdateAsync();
    }
}
公共类启动
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
服务
.AddControllers();
注册(新的CoreModule(),新的DataModule());
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(端点=>
{
endpoints.MapControllerRoute(
名称:“api”,
模式:“api/v1/{controller}/{action}/{id?}”);
});

// 为了回答您的问题,我举了一个在启动时应用迁移的例子。您可以使用相同的概念

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<DataContext>(x => x.UseSqlite("Data Source=LocalDatabase.db"));

    ...
}

为了回答您的问题,我举了一个在启动时应用迁移的例子。您可以使用相同的概念

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<DataContext>(x => x.UseSqlite("Data Source=LocalDatabase.db"));

    ...
}

在上面的链接中找到答案。我们可以从.Net Core 3.0开始使用IHostedService界面,尤其是对于这些任务。重要的是,较低版本的不适合,因为它们不会在服务用户请求之前等待StartSync方法完成:

internal sealed class Boot : IHostedService
{
   public Task StartAsync(CancellationToken token){
       //migrate db or whatever here
   }

   public Task StopAsync(CancellationToken token){
       //gracefull shutdown for 5 sec, after which token will blowup. It will still wait for method return.
   }
}
然后在DI中注册并配置方法:

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddSingleton<IBoot, Boot>();
        services.AddHostedService(x=> x.GetService<IBoot>());
    }
//此方法由运行时调用。使用此方法可将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddControllers();
services.AddSingleton();
services.AddHostedService(x=>x.GetService());
}
优点:

  • 没有woo doo魔术,在同步上下文中使用异步等待。所有内容都是异步/可取消的,非死锁
  • 它已正常关闭,以防您在初始化过程中持有一些需要释放的非托管资源或其他资源
  • 它完全支持依赖注入
缺点:

  • 异常情况下不会调用StopAsync。请小心,也使用Dispose
  • 注意框架的版本,意外切换到较低版本会给你们带来痛苦

在上面的链接中找到了答案。我们可以从.Net Core 3.0开始使用IHostedService接口,尤其是对于那些任务。重要的是,较低版本的不适合,因为它们不会在服务用户请求之前等待StartSync方法完成:

internal sealed class Boot : IHostedService
{
   public Task StartAsync(CancellationToken token){
       //migrate db or whatever here
   }

   public Task StopAsync(CancellationToken token){
       //gracefull shutdown for 5 sec, after which token will blowup. It will still wait for method return.
   }
}
然后在DI中注册并配置方法:

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddSingleton<IBoot, Boot>();
        services.AddHostedService(x=> x.GetService<IBoot>());
    }
//此方法由运行时调用。使用此方法可将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddControllers();
services.AddSingleton();
services.AddHostedService(x=>x.GetService());
}
优点:

  • 没有woo doo魔术,在同步上下文中使用异步等待。所有内容都是异步/可取消的,非死锁
  • 它已正常关闭,以防您在初始化过程中持有一些需要释放的非托管资源或其他资源
  • 它完全支持依赖注入
缺点:

  • 异常情况下不会调用StopAsync。请小心,也使用Dispose
  • 注意框架的版本,意外切换到较低版本会给你们带来痛苦

所以,基本配置方法适合配置一些繁重的启动例程?我这么说是因为在大型数据库上,这种方法可能需要几天的时间。我的理解正确吗,我可以像您所展示的那样将依赖项注入配置方法?找到了更好的方法。请参阅我的答案。因此,基本配置方法是合适的ate要配置一些繁重的启动例程?我这么说是因为在大型数据库上,这种方法可能需要几天时间。我的理解正确吗,我可以像您所展示的那样将依赖项注入配置方法?找到了更好的方法。请参阅我的答案。