这是我的 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/