现在可以从Windows任务计划程序运行.net核心控制台应用程序时读取appsettings.json文件中存在的连接字符串

时间:2019-12-17 10:20:38

标签: c# .net-core

我有一个.net核心控制台应用程序,该应用程序正在从appsettings.json文件中读取连接字符串。当我从Visual Studio运行应用程序时,它运行良好。但是,当我从任务计划程序运行此控制台应用程序时,它无法从appsettings.json文件中读取连接字符串。

例外

  

异常:System.ArgumentNullException:值不能为null。   参数名称:at的connectionString   Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(字符串值,   字符串parameterName)在   Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer(DbContextOptionsBuilder   optionsBuilder,字符串connectionString,操作1 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.InternalDbSet 1.get_EntityType()   在   Microsoft.EntityFrameworkCore.Internal.InternalDbSet 1.get_EntityQueryable() at Microsoft.EntityFrameworkCore.Internal.InternalDbSet 1.System.Linq.IQueryable.get_Provider()   在   Microsoft.EntityFrameworkCore.RelationalQueryableExtensions.FromSqlRaw [TEntity](DbSet`1   源,字符串sql,Object []参数)位于   中的GetValueFromDBCore.Program.SetFreshDataInCache()   P:\ Users \ vivek.nuna \ Redis \ GetValueFromDBCore \ GetValueFromDBCore \ Program.cs:line   30在GetValueFromDBCore.Program.Main(String [] args)中   P:\ Users \ vivek.nuna \ Redis \ GetValueFromDBCore \ GetValueFromDBCore \ Program.cs:line   18

DBCOnext类:

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文件:

<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>

注意,我已经通过发布控制台应用程序exe并为任务计划程序提供了exe路径。

1 个答案:

答案 0 :(得分:0)

我能够解决问题,我在代码Directory.GetCurrentDirectory()中使用了.SetBasePath(Directory.GetCurrentDirectory()),所以当我从VS运行时,它的工作正常。我用Assembly.GetEntryAssembly().Location代替了它,现在工作正常。

参考:https://stackoverflow.com/a/56409888/6527049