Lostaunaum
Lostaunaum

Reputation: 705

public void method in Startup.cs not running on build

This is the code in my Startup.cs file and 2 of my three methods are running on build. However I added the bottom method public void PackageRequestDataAccess and for some reason its not running.

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);
        }
    }
}

I need to run all these three methods whenever the application starts.

Upvotes: 2

Views: 1391

Answers (1)

Thangadurai
Thangadurai

Reputation: 2621

As per the MSDN docs available here only the Configure and ConfigureServices are called during startup.

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

In you case, may be you can add your logic to any one of this method or just call the method from the above method(s).

Upvotes: 2

Related Questions