Search code examples
c#.net-coredirectoryconsole-application

Not able to read the Connection String present in appsettings.json file when running the .net core Console application from windows task scheduler


I have a .net core console application that is reading the connection string from the appsettings.json file. It works fine when I run the application from visual studio. But when I run this console application from the task scheduler it is not able to read the connection string from the appsettings.json file.

Exception:

Exception: System.ArgumentNullException: Value cannot be null. Parameter name: connectionString at Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(String value, String parameterName) at Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer(DbContextOptionsBuilder optionsBuilder, String connectionString, Action1 sqlServerOptionsAction) at GetValueFromDBCore.TestContext.OnConfiguring(DbContextOptionsBuilder optionsBuilder) in P:\Users\vivek.nuna\Redis\GetValueFromDBCore\GetValueFromDBCore\GetValueFromDBCoreContext.cs:line 25 at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider() at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies() at Microsoft.EntityFrameworkCore.DbContext.get_Model() at Microsoft.EntityFrameworkCore.Internal.InternalDbSet1.get_EntityType() at Microsoft.EntityFrameworkCore.Internal.InternalDbSet1.get_EntityQueryable() at Microsoft.EntityFrameworkCore.Internal.InternalDbSet1.System.Linq.IQueryable.get_Provider() at Microsoft.EntityFrameworkCore.RelationalQueryableExtensions.FromSqlRaw[TEntity](DbSet`1 source, String sql, Object[] parameters) at GetValueFromDBCore.Program.SetFreshDataInCache() in P:\Users\vivek.nuna\Redis\GetValueFromDBCore\GetValueFromDBCore\Program.cs:line 30 at GetValueFromDBCore.Program.Main(String[] args) in P:\Users\vivek.nuna\Redis\GetValueFromDBCore\GetValueFromDBCore\Program.cs:line 18

DBCOnext class:

class TestContext: DbContext
    {
        public DbSet<EmployeeEntity> EmployeeEntity { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {

            var builder = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

            IConfigurationRoot configuration = builder.Build();


            optionsBuilder.UseSqlServer(configuration.GetConnectionString("db_core_ef_first"));
        }
    }

CS Proj file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.1.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.0" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
    <PackageReference Include="StackExchange.Redis" Version="2.0.601" />
  </ItemGroup>

  <ItemGroup>
    <None Update="appsettings.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>

</Project>

Note I have made the console application exe by publishing it and gave the exe path to the task scheduler.


Solution

  • I am able to solve the issue, I was using Directory.GetCurrentDirectory() in code .SetBasePath(Directory.GetCurrentDirectory()) so its working fine when I run from VS. I replaced this by Assembly.GetEntryAssembly().Location and it's working fine now.

    Reference: https://stackoverflow.com/a/56409888/6527049