c# - Startup.cs 中的 public void 方法未在构建时运行

标签 c# asp.net-core

这是我的 Startup.cs 文件中的代码,我的三个方法中的两个正在构建上运行。但是,我添加了底部方法 public void PackageRequestDataAccess ,但由于某种原因它没有运行。

namespace Company.Shipping.Service
{
    public class Startup
    {
        private IHostingEnvironment _environment;
        private IConfigurationRoot _configurationRoot;

        public Startup(IHostingEnvironment env)
        {
            _environment = env;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            //Code Ran successfully here 
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
        {
           //Code running successfully here
        }

        //Method below not running 
        public void PackageRequestDataAccess(Common.ServiceHost.WebHost.ServiceConfiguration configuration, IServiceCollection services)
        {
            IMongoCollection<PackageDataEntity> _reqrespcollection;
            MongoDBRepository<PackageDataEntity> _repo = new MongoDBRepository<PackageDataEntity>(configuration.ConnectionStrings["MongoDB"]);

            _reqrespcollection = _repo.Collection;

            int _expiry = Convert.ToInt32(configuration.Settings["ShippingReqRespDataTTL"]);
            TimeSpan _ttl = new TimeSpan(0, 0, _expiry);
            CreateIndexOptions index = new CreateIndexOptions();
            index.ExpireAfter = _ttl;

            var _key = Builders<PackageDataEntity>.IndexKeys.Ascending("RequestSentOn");
            _reqrespcollection.Indexes.CreateOneAsync(_key);
        }
    }
}

每当应用程序启动时,我都需要运行所有这三个方法。

最佳答案

根据 MSDN 文档 available here启动期间仅调用Configure 和ConfigureServices。

The Startup class must include a Configure method and can optionally include a ConfigureServices method, both of which are called when the application starts.

在您的情况下,您可能可以将逻辑添加到此方法的任何一个中,或者仅从上述方法中调用该方法。

关于c# - Startup.cs 中的 public void 方法未在构建时运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41726008/

相关文章:

c# - 在 C# 中从两个整数创建百分比值的最佳方法是什么?

c# - 重复使用带有 `using` 语句的局部变量与重复调用非局部变量?

c# - 如何从命令行创建新的 dotnetcore EMPTY web 项目模板

asp.net-core - 如何将 couchdb 与 Web api 核心连接

c# - 如何使用 Angular 取消 .Net Core Web API 请求?

c# - XNA 按字符串设置颜色

c# - 当前上下文中不存在名称 Membership

c# - 迁移在 Visual Studio CTP 6 vNext 项目中不起作用

azure - 升级到 ASP.NET 5 beta5 后如何进一步调试 500 内部服务器错误

c# - 在 EF 6 中设置值和处理并发