gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:17:05 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com