Данные передачи зависимостей консоли .NET 8 из JSON в класс не работаютC#

Место общения программистов C#
Ответить
Anonymous
 Данные передачи зависимостей консоли .NET 8 из JSON в класс не работают

Сообщение 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; }
}


Подробнее здесь: https://stackoverflow.com/questions/792 ... ot-working
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»