So this is my Main Program i have separated classes but they do not matter for this problem (Im sorry the Programm outputs are German)

using System;
using System.IO;
using System.Net;
using System.Text.Json;
using Microsoft.Extensions.Configuration;
using RestSharp;
using Serilog;
using Serilog.Events;
using Klassen;

namespace ProjekterweiterungTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .WriteTo.Console()
                .WriteTo.File("log.txt", rollingInterval: RollingInterval.Day)
                .CreateLogger();

            try
            {
              
                ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

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

                IConfigurationRoot configuration = configurationBuilder.Build();

                Console.WriteLine($"Aktuelle Api Url: {configuration["ApiUrl"]}");
                Console.WriteLine("Möchtest du eine neue Api Url angeben und die Alte überschreiben? (Ja/Nein)");
                string neueApiUrlAntwort = Console.ReadLine();
                if (neueApiUrlAntwort.Equals("Ja", StringComparison.OrdinalIgnoreCase)) 
                {
                    Console.WriteLine("Gib die neue Api Url ein:");
                    string neueApiUrl = Console.ReadLine();
                    configuration["ApiUrl"] = neueApiUrl;


                    Environment.SetEnvironmentVariable("ApiUrl", neueApiUrl);
                }

                Console.WriteLine($"Aktueller EndPoint: {configuration["EndPoint"]}");
                Console.WriteLine("Möchtest du einen neuen EndPoint angeben und den Alten überschreiben? (Ja/Nein)");
                string neuerEndPointAntwort = Console.ReadLine();
                if (neuerEndPointAntwort.Equals("Ja", StringComparison.OrdinalIgnoreCase))
                {
                    Console.WriteLine("Gib den neuen EndPoint ein:");
                    string neuerEndPoint = Console.ReadLine();
                    configuration["EndPoint"] = neuerEndPoint;


                    Environment.SetEnvironmentVariable("EndPoint", neuerEndPoint);
                }

                Console.WriteLine($"Aktueller BearerToken: {configuration["BearerToken"]}");
                Console.WriteLine("Möchtest du einen neuen BearerToken angeben und den Alten überschreiben? (Ja/Nein)");
                string neuerBearerTokenAntwort = Console.ReadLine();
                if (neuerBearerTokenAntwort.Equals("Ja", StringComparison.OrdinalIgnoreCase))
                {
                    Console.WriteLine("Gib den neuen BearerToken ein:");
                    string neuerBearerToken = Console.ReadLine();
                    configuration["BearerToken"] = neuerBearerToken;

                    Environment.SetEnvironmentVariable("BearerToken", neuerBearerToken);
                }

                string apiUrl = configuration.GetSection("ApiUrl").Value;
                string endPoint = configuration.GetSection("EndPoint").Value;
                string bearerToken = configuration.GetSection("BearerToken").Value;

                var client = new RestClient(apiUrl);
                var request = new RestRequest(endPoint, Method.POST);

                request.AddHeader("Authorization", $"Bearer {bearerToken}");

                var body = new
                {
                    filters = new
                    {
                        jobInPlanFilter = new { }
                    },
                    sorters = new
                    {
                        jobInPlanSorter = new
                        {
                            jobstreamScheduledTime = new
                            {
                                descending = false,
                                priority = 1
                            }
                        }
                    }
                };

                var jsonBody = JsonSerializer.Serialize(body);
                request.AddParameter("application/json", jsonBody, ParameterType.RequestBody);

                var response = client.Execute(request);

                if (response.IsSuccessful)
                {
                    ApiResponse[] apiResponses = JsonSerializer.Deserialize<ApiResponse[]>(response.Content);
                    Console.WriteLine("Name, Workstation, Status");

                    using (StreamWriter sw = new StreamWriter("Test.csv"))
                    {
                        sw.WriteLine("Name,Workstation,Status");

                        foreach (var apiResponse in apiResponses)
                        {
                            Console.WriteLine($"{apiResponse.Name}, {apiResponse.JobDefinition.JobDefinitionInPlanKey.WorkstationInPlanKey.Name}, {apiResponse.NodeStatus.InternalStatus}");

                            sw.WriteLine($"{apiResponse.Name},{apiResponse.JobDefinition.JobDefinitionInPlanKey.WorkstationInPlanKey.Name},{apiResponse.NodeStatus.InternalStatus}");
                        }
                    }
                }
                else
                {
                    Console.WriteLine($"Fehler bei der Anfrage. Statuscode: {response.StatusCode}");
                    Console.WriteLine($"Antwortinhalt: {response.Content}");
                    Console.WriteLine($"Fehlermeldung: {response.ErrorMessage}");
                    Console.WriteLine($"Fehlerausnahme: {response.ErrorException}");
                }

                Console.Read();
            }
            catch (NullReferenceException ex)
            {
                Log.Error(ex, "NullReferenceException: {Message}", ex.Message);
                Console.WriteLine(ex.Message);
                Console.WriteLine("A null reference was encountered. Please check that all required objects are properly initialized.");
            }
            catch (ArgumentNullException ex)
            {
                Log.Error(ex, "ArgumentNullException: {Message}", ex.Message);
                Console.WriteLine(ex.Message);
                Console.WriteLine("One or more parameters are null. Please check the input parameters.");
            }
            catch (UnauthorizedAccessException ex)
            {
                Log.Error(ex, "UnauthorizedAccessException: {Message}", ex.Message);
                Console.WriteLine(ex.Message);
                Console.WriteLine("Seems like you´re not authorized, please Check if you gave me the right Bearer Token, Url & Endpoint and if you gave the request the Right Method(Get,Post,Put etc.)");
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Exception: {Message}", ex.Message);
                Console.WriteLine(ex.Message);
                Console.WriteLine("Something went wrong.");
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }
    }
}

{
  "ApiUrl": "https://ar-hwa-mdm02:31116",
  "EndPoint": "twsd/plan/current/job/query",
  "BearerToken": "MYBEARERTOKEN"
}

And this is my appsettings.json

So in my programm i ask the user if he wants to perform the program with the date of the appsettings.json or if he wants to give me like for example a new apiurl a new endpoint or a new bearer token, and if he decides to give me a new information it should Overwrite the old Value of the appsettings. your text

I tried like every thing i found on the internet i tried to use .net version 4.7.2 to make an defaultsettings file named Settings.settings but when i did this visual studio told me i have to use a newer c# version, so i changed the version from 7.3 to 8.0 and 10.0 and everything vs gave me was more errors.

0

There are 0 best solutions below