GVKun编程网logo

asp.net-core – 如何在ApplicationStarted事件中优雅地中止Asp.Net核心启动?

14

对于asp.net-core–如何在ApplicationStarted事件中优雅地中止Asp.Net核心启动?感兴趣的读者,本文将提供您所需要的所有信息,并且为您提供关于.net-core–如何在A

对于asp.net-core – 如何在ApplicationStarted事件中优雅地中止Asp.Net核心启动?感兴趣的读者,本文将提供您所需要的所有信息,并且为您提供关于.net-core – 如何在Asp.net核心中使用Akka.Net 1.3.0配置的Appsetting配置、Application_Start ASP.NET、Application_Start不被ASP.NET网页应用程序打中、application_start意外触发了asp.net的宝贵知识。

本文目录一览:

asp.net-core – 如何在ApplicationStarted事件中优雅地中止Asp.Net核心启动?

asp.net-core – 如何在ApplicationStarted事件中优雅地中止Asp.Net核心启动?

运行Asp.Net Core 1.1

大多数时候,无论启动过程中发生什么,我都希望我的ASP.NET核心应用程序能够运行,但是在启动期间有一些关键错误,我希望它能够关闭.在Startup.cs中,我尝试在Configure方法期间将ApplicationLifetime传递给ApplicationStarted事件:

appLifetime.ApplicationStarted.Register((al) => {
               var appltime = (IApplicationLifetime)al;

               try {
                ... varIoUs startup code....

               } catch (Exception ex) {
                   appltime.StopApplication();
               }

            },appLifetime);

但这结束了这个错误:

Unhandled Exception: System.ObjectdisposedException: The
CancellationTokenSource has been disposed. at
System.Threading.CancellationTokenSource.ThrowObjectdisposedException()
at
Microsoft.AspNetCore.Hosting.WebHostExtensions.<>c__displayClass0_0.b__0()
at System.Action`1.Invoke(T obj)

我猜这是因为我在Start()中调用了Stop(),但我不知道其他任何方式.该应用程序确实关闭所以也许没关系,但如果有人有任何建议我真的很感激.

谢谢,
蜂鸣器

@H_301_32@解决方法
我知道这是一个古老的问题,但由于它还没有回答,我只是经历了同样的过程,我想也许我可以帮助其他人在同一条船上.

我正在使用ASP.NET Core 2.1,但解决方案似乎也适用于1.x.

通过向其添加IApplicationLifetime参数并在最后添加ApplicationStarted委托来修改您的Configure方法,如此…

public void Configure(IApplicationBuilder app,IHostingEnvironment env,IApplicationLifetime applicationLifetime)
{
    // existing code

    applicationLifetime.ApplicationStartup.Register(() => OnStartUp(applicationLifetime));
}

在Startup.cs文件中创建一个OnStartUp方法,就像这样……

public void OnStartUp(IApplicationLifetime applicationLifetime)
{
    try
    {
        // Do your startup magic
    }
    catch(Exception ex)
    {
        applicationLifetime.StopApplication();
    }
}

如果OnStartUp代码出现问题,它现在将成功关闭应用程序,而不会抛出ObjectdisposedException.

.net-core – 如何在Asp.net核心中使用Akka.Net 1.3.0配置的Appsetting配置

.net-core – 如何在Asp.net核心中使用Akka.Net 1.3.0配置的Appsetting配置

我一直在玩Visual Studio 2017中支持.NetStandard 1.6的新版Akka.Net.由于Akka.Net配置的特性,它使用HOCON格式进行配置.之前的版本在app.config或Web.config中嵌入了易于阅读的HOCON配置.另一种选择是使用接受字符串对象的ConfigurationFactory.ParseString方法.但是从字符串解析HOCON对于小配置部分来说很方便.在我的情况下,我留下了这个ParseString配置,甚至没有按预期工作.
我想出了这个:

var configString = @"akka {
        log-config-on-start = on
        stdout-loglevel = INFO
        loglevel = DEBUG
        loggers= ""[Akka.Logger.Serilog.SerilogLogger,Akka.Logger.Serilog]""
        actor {

                    debug {
                        receive = on
                        autoreceive = on
                        lifecycle = on
                        event-stream = on
                        unhandled = on
                    }
              }

    akka.persistence {
        journal {
                    plugin = ""akka.persistence.journal.sqlite""

                    sqlite {Akka.Persistence.sqlite.Journal.sqliteJournal,Akka.Persistence.sqlite""
                                        plugin-dispatcher = ""akka.actor.default-dispatcher""
                                        connection-string = ""Data Source = F:\\sqliteDb\\Sample.db3""
                                      table-name = event_journal
                                    Metadata-table-name = journal_Metadata
                                    auto-initialize = on

                            }
                    }

    snapshot-store {
        plugin = ""akka.persistence.snapshot-store.sqlite""
        sqlite {[Akka.Persistence.sqlite.Snapshot.sqliteSnapshotStore,Akka.Persistence.sqlite]""
            connection-string = ""Data Source = F:\\sqliteDb\\Sample.db3""
            table-name = snapshot_store
            auto-initialize = on

        }
}

}

     ";
        var config = ConfigurationFactory.ParseString(configString);
         ActorSystem.Create("AkkaSystem",config);

哪个没有按预期工作.
我们如何使用appsetting.json在Asp.Net核心中配置akka.net?或者有更好的方法吗?

解决方法

我将hocon转换为json并使用ConfigurationFactory.FromObject和一些具有我感兴趣的属性的类来从appsettings中读取akka-config.匿名对象模拟hocon根.

var config = ConfigurationFactory.FromObject(new { akka = configuration.GetSection("Akka").Get<AkkaConfig>() });

actorSystem = ActorSystem.Create("Stimpy",config);

请注意,我没有费心去弄清楚如何从appsettings解析kebab-case属性.所以我刚刚重命名了连字符以外的属性.然后将JsonProperty属性设置为正确的名称,以便FromObject可以正确地反序列化它.

public class AkkaConfig
{
    [JsonProperty(PropertyName = "log-config-on-start")]
    public string logconfigonstart { get; set; }
    [JsonProperty(PropertyName = "stdout-loglevel")]
    public string stdoutloglevel { get; set; }
    public string loglevel { get; set; }
    public string[] loggers { get; set; }
    public ActorConfig actor { get; set; }

    public class ActorConfig
    {
        public DebugConfig debug { get; set; }
        public Dictionary<string,string> serializers { get; set; }
        [JsonProperty(PropertyName = "serialization-bindings")]
        public Dictionary<string,string> serializationbindings { get; set; }

        public class DebugConfig
        {
            public string receive { get; set; }
            public string autoreceive { get; set; }
            public string lifecycle { get; set; }
            [JsonProperty(PropertyName = "event-stream")]
            public string eventstream { get; set; }
            public string unhandled { get; set; }
        }
    }
}

appsettings.json:

{
  "Logging": {
    "IncludeScopes": false,"LogLevel": {
      "Default": "Trace"
    }
  },"Akka": {
    "logconfigonstart":"on","stdoutloglevel":"INFO","loglevel": "DEBUG","loggers": [ "Akka.Logger.NLog.NLogLogger,Akka.Logger.NLog" ],"actor": {
      "debug": {
        "receive": "on","autoreceive": "on","lifecycle": "on","eventstream": "on","unhandled": "on"
      },"serializers": {
        "hyperion": "Akka.Serialization.HyperionSerializer,Akka.Serialization.Hyperion"
      },"serializationbindings": {
        "System.Object": "hyperion"
      }
    }
  }
}

Application_Start ASP.NET

Application_Start ASP.NET

如果从Microsoft官方 documentation获取此信息:

The Application_Start and Application_End methods are special methods that do not represent HttpApplication events. ASP.NET calls them once for the lifetime of the application domain,not for each HttpApplication instance.

我确信Application_Start与HttpApplication有关,这里this guy explains为什么我错了.
哪个对象/实体与Application_Start事件有关?

解决方法

正如文档所述,事件链接到 application domain的生命周期,实际上这意味着应用程序正在运行的应用程序池.如果应用程序池因任何原因被回收,Application_Start将再次触发 – 更改为web.config,例如.

Application_Start不被ASP.NET网页应用程序打中

Application_Start不被ASP.NET网页应用程序打中

我正在ASP.NET Web应用程序中的global.asax.cs文件中调试某些东西,并在Application_Start()事件中设置了一个断点,但是当我启动VS2008中的Web应用程序时,该事件不会被触发.我的目标是3.5框架.

什么可以防止这个事件被解雇?或者我怎么搞砸了这个项目,这样的事件就不再有线了?

解决方法

如果我记得正确,Application_Start会在调试器连接到应用程序之前运行.

尝试做其他事情来检查Application_Start方法是否运行,就像设置应用程序变量一样:

Application("app") = "started"

然后在页面中显示应用程序变量,看看它是否被设置.

application_start意外触发了asp.net

application_start意外触发了asp.net

我正在查看一个asp.net应用程序,该应用程序调用应用程序中的数据库在global.asax中启动.这些调用大约需要3秒钟(取决于sql缓存计划)才能运行.我注意到第一次重新部署时应用程序运行缓慢.我把它放在应用程序启动时对数据库的调用.但是几分钟后,应用程序再次需要时间来加载.

为了弄清楚发生了什么,我写了一个日志文件(见下文).从这个文件中你可以看到,当多个请求第一次到达应用程序时,应用程序启动多次.然后看起来application_start以随机方式触发(任何时间为2-10分钟).

根据我对application_starts如何工作的理解,它应该只在应用程序FirsT启动时被触发,但它似乎是从多个客户端随机触发的!根据MSDN“应用程序启动时第一次触发Application_Start事件.”

任何人都可以帮我解决发生的事情吗?我正在使用IIS6和.net 2.0.它是我缺少的应用程序池设置.

非常感谢
Rippo

[12/02/2009 16:16:58] 91.84.25.241 - Application started,Sub MyRules started
[12/02/2009 16:17:06] 65.55.51.34 - Application started,Sub MyRules started
[12/02/2009 16:17:07] 91.84.25.241 - Application started,Sub MyRules completed
[12/02/2009 16:17:10] 65.55.51.34 - Application started,Sub MyRules completed
[12/02/2009 16:17:26] 212.115.51.229 - Application started,Sub MyRules started
[12/02/2009 16:17:30] 212.115.51.229 - Application started,Sub MyRules completed
[12/02/2009 16:17:30] 212.115.51.229 - Application started,Sub MyRules started
[12/02/2009 16:17:33] 212.115.51.229 - Application started,Sub MyRules completed
[12/02/2009 16:17:33] 212.115.51.229 - Application started,Sub MyRules started
[12/02/2009 16:17:36] 212.115.51.229 - Application started,Sub MyRules completed
[12/02/2009 16:17:36] 212.115.51.229 - Application started,Sub MyRules started
[12/02/2009 16:17:39] 212.115.51.229 - Application started,Sub MyRules completed
[12/02/2009 16:17:42] 212.115.51.229 - Application started,Sub MyRules started
[12/02/2009 16:17:48] 212.115.51.229 - Application started,Sub MyRules completed
[12/02/2009 16:17:48] 212.115.51.229 - Application started,Sub MyRules started
[12/02/2009 16:17:52] 212.115.51.229 - Application started,Sub MyRules completed
[12/02/2009 16:17:52] 212.115.51.229 - Application started,Sub MyRules started
[12/02/2009 16:17:54] 212.115.51.229 - Application started,Sub MyRules completed
[12/02/2009 16:18:27] 91.84.25.241 - Application started,Sub MyRules started
[12/02/2009 16:18:31] 91.84.25.241 - Application started,Sub MyRules completed
[12/02/2009 16:18:31] 212.115.51.229 - Application started,Sub MyRules started
[12/02/2009 16:18:33] 212.115.51.229 - Application started,Sub MyRules completed
[12/02/2009 16:29:26] 167.230.38.115 - Application started,Sub MyRules started
[12/02/2009 16:29:28] 167.230.38.115 - Application started,Sub MyRules completed
[12/02/2009 16:29:34] 82.110.42.84 - Application started,Sub MyRules started
[12/02/2009 16:29:37] 82.110.42.84 - Application started,Sub MyRules completed
[12/02/2009 16:29:38] 82.110.42.84 - Application started,Sub MyRules started
[12/02/2009 16:29:41] 82.110.42.84 - Application started,Sub MyRules completed
[12/02/2009 16:32:53] 99.142.1.97 - Application started,Sub MyRules started
[12/02/2009 16:32:57] 99.142.1.97 - Application started,Sub MyRules completed
[12/02/2009 16:32:57] 99.142.1.97 - Application started,Sub MyRules started
[12/02/2009 16:33:01] 99.142.1.97 - Application started,Sub MyRules completed
[12/02/2009 16:33:01] 99.142.1.97 - Application started,Sub MyRules started
[12/02/2009 16:33:06] 99.142.1.97 - Application started,Sub MyRules completed
[12/02/2009 16:33:06] 99.142.1.97 - Application started,Sub MyRules started
[12/02/2009 16:33:10] 99.142.1.97 - Application started,Sub MyRules completed
[12/02/2009 16:33:11] 99.142.1.97 - Application started,Sub MyRules started
[12/02/2009 16:33:16] 99.142.1.97 - Application started,Sub MyRules completed
[12/02/2009 16:36:15] 65.55.51.34 - Application started,Sub MyRules started
[12/02/2009 16:36:17] 65.55.51.34 - Application started,Sub MyRules completed
[12/02/2009 16:41:37] 119.123.226.156 - Application started,Sub MyRules started
[12/02/2009 16:41:37] 62.49.121.122 - Application started,Sub MyRules started
[12/02/2009 16:41:40] 62.49.121.122 - Application started,Sub MyRules completed
[12/02/2009 16:41:40] 119.123.226.156 - Application started,Sub MyRules completed
[12/02/2009 16:41:41] 62.49.121.122 - Application started,Sub MyRules started
[12/02/2009 16:41:44] 62.49.121.122 - Application started,Sub MyRules completed
[12/02/2009 16:45:17] 84.70.249.242 - Application started,Sub MyRules started
[12/02/2009 16:45:19] 84.70.249.242 - Application started,Sub MyRules completed
[12/02/2009 16:47:03] 80.41.121.68 - Application started,Sub MyRules started
[12/02/2009 16:47:05] 80.41.121.68 - Application started,Sub MyRules completed
[12/02/2009 16:47:29] 81.179.26.249 - Application started,Sub MyRules started
[12/02/2009 16:47:31] 81.179.26.249 - Application started,Sub MyRules completed
[12/02/2009 16:47:59] 81.136.179.170 - Application started,Sub MyRules started
[12/02/2009 16:47:59] 81.136.179.170 - Application started,Sub MyRules started
[12/02/2009 16:48:02] 81.136.179.170 - Application started,Sub MyRules completed
[12/02/2009 16:48:02] 81.136.179.170 - Application started,Sub MyRules started
[12/02/2009 16:48:04] 81.136.179.170 - Application started,Sub MyRules completed
[12/02/2009 16:48:04] 81.136.179.170 - Application started,Sub MyRules completed
[12/02/2009 16:52:35] 69.34.161.80 - Application started,Sub MyRules started
[12/02/2009 16:52:35] 69.34.161.80 - Application started,Sub MyRules started
[12/02/2009 16:52:38] 69.34.161.80 - Application started,Sub MyRules completed
[12/02/2009 16:52:38] 69.34.161.80 - Application started,Sub MyRules started
[12/02/2009 16:52:39] 69.34.161.80 - Application started,Sub MyRules completed
[12/02/2009 16:52:39] 69.34.161.80 - Application started,Sub MyRules completed
[12/02/2009 16:52:40] 69.34.161.80 - Application started,Sub MyRules completed

解决方法

检查应用程序重新启动的情况. 最近我们写了一些日志条目到bin文件夹,发生了完全相同的事情,我们不明白原因.然后终于意识到对bin文件夹进行任何修改都会导致应用程序重启.

关于asp.net-core – 如何在ApplicationStarted事件中优雅地中止Asp.Net核心启动?的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于.net-core – 如何在Asp.net核心中使用Akka.Net 1.3.0配置的Appsetting配置、Application_Start ASP.NET、Application_Start不被ASP.NET网页应用程序打中、application_start意外触发了asp.net等相关内容,可以在本站寻找。

本文标签: