У меня есть следующий код, и если я запущу его с удаленным комментарием к строке 36, я получу данные из файла .json в переменную CollarSurveyPickupSettings.
Если я затем запущу строку cspvm в строке 37 (CollarSurveyPickupVM), он отправляет загруженные данные 3 DbContext, загруженные параметры, но никаких данных в CollarSurveyPickupMcollarSurveyModel, AcquireCommandLineMacquireCommandLineModel или PeggingSettingsM peggingModel... p>
Я не уверен, что делаю неправильно? Буду признателен за любую помощь.
Я также добавил репозиторий git https://github.com/JemQDev/RHOKSAutomationConsole
program.cs:
using CommandLine;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using RHOKSAutomationConsole.CollarSurvey;
using RHOKSAutomationConsole.EFACQ;
using RHOKSAutomationConsole.EFRHOKS2020;
using RHOKSAutomationConsole.EFRHOKS2024;
using RHOKSAutomationConsole.Pegging;
using RHOKSAutomationConsole.Settings;
namespace RHOKSAutomationConsole;
public class Options
{
[Option("DataSource", Required = true, Default = "TEST", HelpText = "Data Source as TEST or PROD")]
public string? DataSource { get; set; }
}
internal class Program
{
//private static AppM? ApplicationData;
//private static AcquireCommandLineM? ACQCommandLineData;
//private static CollarSurveyPickupM? CollarSurveyPickupSettings;
//private static PeggingSettingsM? PeggingSettings;
static void Main(string[] args)
{
Parser.Default.ParseArguments(args)
.WithParsed(options =>
{
using IHost host = CreateHostBuilder(options).Build();
//IConfiguration config = host.Services.GetRequiredService();
//CollarSurveyPickupSettings = config.GetSection("CollarSurveyPickups").Get() ?? new CollarSurveyPickupM();
var cspvm = host.Services.GetRequiredService();
});
}
#region HostBuilder
public static IHostBuilder CreateHostBuilder(Options optionsq) =>
Host.CreateDefaultBuilder()
.ConfigureAppConfiguration((context, config) =>
{
config
.AddJsonFile(string.Concat(Directory.GetCurrentDirectory(), "\\Settings\\appsettings.json"), optional: false, reloadOnChange: true)
.AddJsonFile(string.Concat(Directory.GetCurrentDirectory(), "\\Settings\\SerilogSettings.json"), optional: false, reloadOnChange: true)
.AddConfiguration(context.Configuration)
.AddUserSecrets
()
.AddEnvironmentVariables();
})
.ConfigureServices((hostContext, services) =>
{
IConfiguration config = hostContext.Configuration;
//services.Configure(options => config.GetSection("ApplicationSettings").Bind(options));
services.Configure(options => config.GetSection("Serilog").Bind(options));
services.Configure(options => config.GetSection("SECRET_ACQ_Credentials").Bind(options));
services.Configure(options => config.GetSection("CollarSurveyPickups").Bind(options));
services.Configure(options => config.GetSection("OreDefPegging").Bind(options));
services.AddDbContext(
options => options.UseSqlServer(
$"Data Source=" +
$"{config.GetSection("SqlConfigurations:VM.SQL:SQLServer").Get()}" +
$"\\" +
$"{config.GetSection("SqlConfigurations:VM.SQL:SQLInstance").Get()};" +
$"Initial Catalog={config.GetSection("SqlConfigurations:VM.SQL:SQLDatabaseRHOKS2020").Get()}" +
$";Trusted_Connection=True;TrustServerCertificate=True;", x => x.UseNetTopologySuite()).EnableSensitiveDataLogging(true), ServiceLifetime.Transient);
services.AddDbContext(
options => options.UseSqlServer(
$"Data Source=" +
$"{config.GetSection("SqlConfigurations:VM.SQL:SQLServer").Get()}" +
$"\\" +
$"{config.GetSection("SqlConfigurations:VM.SQL:SQLInstance").Get()};" +
$"Initial Catalog={config.GetSection("SqlConfigurations:VM.SQL:SQLDatabaseRHOKS2024").Get()}" +
$";Trusted_Connection=True;TrustServerCertificate=True;", x => x.UseNetTopologySuite()).EnableSensitiveDataLogging(true), ServiceLifetime.Transient);
services.AddDbContext(
options => options.UseSqlServer(
$"Data Source=" +
$"{config.GetSection("SqlConfigurations:ACQ.SQL:SQLServer").Get()}" +
$"\\" +
$"{config.GetSection("SqlConfigurations:ACQ.SQL:SQLInstance").Get()};" +
$"Initial Catalog={config.GetSection("SqlConfigurations:ACQ.SQL:SQLDatabaseACQ").Get()}" +
$";Trusted_Connection=True;TrustServerCertificate=True;", x => x.UseNetTopologySuite()).EnableSensitiveDataLogging(true), ServiceLifetime.Transient);
services.AddSingleton(optionsq);
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
}
//.UseSerilog((context, services, configuration) => configuration
//.ReadFrom.Configuration(context.Configuration)
//.ReadFrom.Services(services)
);
#endregion HostBuilder
}
CollarSurveyPickupVM:
using CommunityToolkit.Mvvm.ComponentModel;
using CsvHelper;
using CsvHelper.Configuration;
using Microsoft.IdentityModel.Tokens;
using RHOKSAutomationConsole.EFACQ;
using RHOKSAutomationConsole.EFRHOKS2020;
using RHOKSAutomationConsole.EFRHOKS2024;
using RHOKSAutomationConsole.Pegging;
using RHOKSAutomationConsole.Settings;
using System.Globalization;
namespace RHOKSAutomationConsole.CollarSurvey;
public partial class CollarSurveyPickupVM : ObservableObject
{
[ObservableProperty] CollarSurveyPickupM? m_CollarSurveyPickupSettings;
[ObservableProperty] AcquireCommandLineM? m_AcquireCommandLineModel;
[ObservableProperty] Options m_OptionsData;
[ObservableProperty] RHOKS_2024Context m_Rhoks2024Context;
[ObservableProperty] RHOKS_2020Context m_Rhoks2020Context;
[ObservableProperty] ACQContext m_AcquireContext;
public CollarSurveyPickupVM(
RHOKS_2024Context rhoks2024context,
RHOKS_2020Context rhoks2020context,
ACQContext acquirecontext,
Options options,
CollarSurveyPickupM? collarSurveyModel,
AcquireCommandLineM acquireCommandLineModel,
PeggingSettingsM peggingModel
)
{
Rhoks2024Context = rhoks2024context ?? throw new ArgumentNullException(nameof(rhoks2024context));
Rhoks2020Context = rhoks2020context ?? throw new ArgumentNullException(nameof(rhoks2020context));
AcquireContext = acquirecontext ?? throw new ArgumentNullException(nameof(acquirecontext));
OptionsData = options ?? throw new ArgumentNullException(nameof(options));
PeggingModel = peggingModel ?? throw new ArgumentNullException(nameof(peggingModel));
AcquireCommandLineModel = acquireCommandLineModel ?? throw new ArgumentNullException(nameof(acquireCommandLineModel));
CollarSurveyPickupSettings = collarSurveyModel ?? throw new ArgumentNullException(nameof(collarSurveyModel));
}
public PeggingSettingsM PeggingModel { get; }
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... ot-working
Данные передачи зависимостей консоли .NET 8 из JSON в класс не работают ⇐ C#
Место общения программистов C#
1732251860
Anonymous
У меня есть следующий код, и если я запущу его с удаленным комментарием к строке 36, я получу данные из файла .json в переменную CollarSurveyPickupSettings.
Если я затем запущу строку cspvm в строке 37 (CollarSurveyPickupVM), он отправляет загруженные данные 3 DbContext, загруженные параметры, но никаких данных в CollarSurveyPickupMcollarSurveyModel, AcquireCommandLineMacquireCommandLineModel или PeggingSettingsM peggingModel... p>
Я не уверен, что делаю неправильно? Буду признателен за любую помощь.
Я также добавил репозиторий git https://github.com/JemQDev/RHOKSAutomationConsole
program.cs:
using CommandLine;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using RHOKSAutomationConsole.CollarSurvey;
using RHOKSAutomationConsole.EFACQ;
using RHOKSAutomationConsole.EFRHOKS2020;
using RHOKSAutomationConsole.EFRHOKS2024;
using RHOKSAutomationConsole.Pegging;
using RHOKSAutomationConsole.Settings;
namespace RHOKSAutomationConsole;
public class Options
{
[Option("DataSource", Required = true, Default = "TEST", HelpText = "Data Source as TEST or PROD")]
public string? DataSource { get; set; }
}
internal class Program
{
//private static AppM? ApplicationData;
//private static AcquireCommandLineM? ACQCommandLineData;
//private static CollarSurveyPickupM? CollarSurveyPickupSettings;
//private static PeggingSettingsM? PeggingSettings;
static void Main(string[] args)
{
Parser.Default.ParseArguments(args)
.WithParsed(options =>
{
using IHost host = CreateHostBuilder(options).Build();
//IConfiguration config = host.Services.GetRequiredService();
//CollarSurveyPickupSettings = config.GetSection("CollarSurveyPickups").Get() ?? new CollarSurveyPickupM();
var cspvm = host.Services.GetRequiredService();
});
}
#region HostBuilder
public static IHostBuilder CreateHostBuilder(Options optionsq) =>
Host.CreateDefaultBuilder()
.ConfigureAppConfiguration((context, config) =>
{
config
.AddJsonFile(string.Concat(Directory.GetCurrentDirectory(), "\\Settings\\appsettings.json"), optional: false, reloadOnChange: true)
.AddJsonFile(string.Concat(Directory.GetCurrentDirectory(), "\\Settings\\SerilogSettings.json"), optional: false, reloadOnChange: true)
.AddConfiguration(context.Configuration)
.AddUserSecrets
()
.AddEnvironmentVariables();
})
.ConfigureServices((hostContext, services) =>
{
IConfiguration config = hostContext.Configuration;
//services.Configure(options => config.GetSection("ApplicationSettings").Bind(options));
services.Configure(options => config.GetSection("Serilog").Bind(options));
services.Configure(options => config.GetSection("SECRET_ACQ_Credentials").Bind(options));
services.Configure(options => config.GetSection("CollarSurveyPickups").Bind(options));
services.Configure(options => config.GetSection("OreDefPegging").Bind(options));
services.AddDbContext(
options => options.UseSqlServer(
$"Data Source=" +
$"{config.GetSection("SqlConfigurations:VM.SQL:SQLServer").Get()}" +
$"\\" +
$"{config.GetSection("SqlConfigurations:VM.SQL:SQLInstance").Get()};" +
$"Initial Catalog={config.GetSection("SqlConfigurations:VM.SQL:SQLDatabaseRHOKS2020").Get()}" +
$";Trusted_Connection=True;TrustServerCertificate=True;", x => x.UseNetTopologySuite()).EnableSensitiveDataLogging(true), ServiceLifetime.Transient);
services.AddDbContext(
options => options.UseSqlServer(
$"Data Source=" +
$"{config.GetSection("SqlConfigurations:VM.SQL:SQLServer").Get()}" +
$"\\" +
$"{config.GetSection("SqlConfigurations:VM.SQL:SQLInstance").Get()};" +
$"Initial Catalog={config.GetSection("SqlConfigurations:VM.SQL:SQLDatabaseRHOKS2024").Get()}" +
$";Trusted_Connection=True;TrustServerCertificate=True;", x => x.UseNetTopologySuite()).EnableSensitiveDataLogging(true), ServiceLifetime.Transient);
services.AddDbContext(
options => options.UseSqlServer(
$"Data Source=" +
$"{config.GetSection("SqlConfigurations:ACQ.SQL:SQLServer").Get()}" +
$"\\" +
$"{config.GetSection("SqlConfigurations:ACQ.SQL:SQLInstance").Get()};" +
$"Initial Catalog={config.GetSection("SqlConfigurations:ACQ.SQL:SQLDatabaseACQ").Get()}" +
$";Trusted_Connection=True;TrustServerCertificate=True;", x => x.UseNetTopologySuite()).EnableSensitiveDataLogging(true), ServiceLifetime.Transient);
services.AddSingleton(optionsq);
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
}
//.UseSerilog((context, services, configuration) => configuration
//.ReadFrom.Configuration(context.Configuration)
//.ReadFrom.Services(services)
);
#endregion HostBuilder
}
CollarSurveyPickupVM:
using CommunityToolkit.Mvvm.ComponentModel;
using CsvHelper;
using CsvHelper.Configuration;
using Microsoft.IdentityModel.Tokens;
using RHOKSAutomationConsole.EFACQ;
using RHOKSAutomationConsole.EFRHOKS2020;
using RHOKSAutomationConsole.EFRHOKS2024;
using RHOKSAutomationConsole.Pegging;
using RHOKSAutomationConsole.Settings;
using System.Globalization;
namespace RHOKSAutomationConsole.CollarSurvey;
public partial class CollarSurveyPickupVM : ObservableObject
{
[ObservableProperty] CollarSurveyPickupM? m_CollarSurveyPickupSettings;
[ObservableProperty] AcquireCommandLineM? m_AcquireCommandLineModel;
[ObservableProperty] Options m_OptionsData;
[ObservableProperty] RHOKS_2024Context m_Rhoks2024Context;
[ObservableProperty] RHOKS_2020Context m_Rhoks2020Context;
[ObservableProperty] ACQContext m_AcquireContext;
public CollarSurveyPickupVM(
RHOKS_2024Context rhoks2024context,
RHOKS_2020Context rhoks2020context,
ACQContext acquirecontext,
Options options,
CollarSurveyPickupM? collarSurveyModel,
AcquireCommandLineM acquireCommandLineModel,
PeggingSettingsM peggingModel
)
{
Rhoks2024Context = rhoks2024context ?? throw new ArgumentNullException(nameof(rhoks2024context));
Rhoks2020Context = rhoks2020context ?? throw new ArgumentNullException(nameof(rhoks2020context));
AcquireContext = acquirecontext ?? throw new ArgumentNullException(nameof(acquirecontext));
OptionsData = options ?? throw new ArgumentNullException(nameof(options));
PeggingModel = peggingModel ?? throw new ArgumentNullException(nameof(peggingModel));
AcquireCommandLineModel = acquireCommandLineModel ?? throw new ArgumentNullException(nameof(acquireCommandLineModel));
CollarSurveyPickupSettings = collarSurveyModel ?? throw new ArgumentNullException(nameof(collarSurveyModel));
}
public PeggingSettingsM PeggingModel { get; }
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79209792/net-8-console-dependency-injection-pass-data-from-json-to-class-not-working[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия