繁体   English   中英

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

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

我有一个 .net 核心控制台应用程序,它正在从 appsettings.json 文件中读取连接字符串。 当我从 Visual Studio 运行应用程序时,它工作正常。 但是当我从任务调度程序运行这个控制台应用程序时,它无法从 appsettings.json 文件中读取连接字符串。

例外

异常:System.ArgumentNullException:值不能为空。 参数名称:Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(String value, String parameterName) at Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer(DbContextOptionsBuilder optionsBuilder, String connectionString, Action 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.RelationalQueryabl eExtensions.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在 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 项目文件:

<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 路径。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM