I have 2 providers as you can see below. If "IsDefaultProvider" section is "true" I want to get its values while injecting IConfiguration.
I write the below code I could not work
var providers = configuration.GetSection("X:Providers");
foreach(var provider in providers.)
{
if (providers.GetSection("IsDefaultProvider").Value == "true")
{
_defaultProvider = (XProviderType)Enum.Parse(typeof(XProviderType), provider.Value.ToString());
}
}
Appsettings.json
"X": {
"Providers": [
{
"IsDefaultProvider": false,
"Name": "***",
"BaseUrl": "https://*",
"ApiKey": "*****",
"SecretKey": "****"
},
{
"IsDefaultProvider": true,
"Name": "*****",
"BaseUrl": "http://*"
}
]
}
There are two problems in the code you've shown in your question:
providers. does not compile. I expect this might just be a bad copy-paste but I'm pointing it out just in case as it should be providers.GetChildren().
When parsing a JSON boolean value, the string representation is e.g True rather than true, so you need to compare against this (or just do so case-insensitively).
Here's a working example:
var providers = configuration.GetSection("X:Providers");
foreach (var provider in providers.GetChildren())
{
if (provider.GetSection("IsDefaultProvider").Value.ToLower() == "true")
{
_defaultProvider = ...
}
}
I would also swap out the use of GetSection("IsDefaultProvider"), simply because IsDefaultProvider is not a section: it's a property. Here's an example of how that simplifies the code a little:
if (provider["IsDefaultProvider"].ToLower() == "true")
Having said all of that, Tao Zhou's answer is a much more type-safe way to handle this process if you have no objections to creating those additional classes, so I'd encourage you to consider using his approach instead.
For mapping Configuration from appsettings.json to Provider, try follow steps below:
Define model for Provider
public class Provider
{
public bool IsDefaultProvider { get; set; }
public string Name { get; set; }
public string BaseUrl { get; set; }
public string ApiKey { get; set; }
public string SecretKey { get; set; }
}
public class X
{
public List<Provider> Providers { get; set; }
}
Get Default Provider
var providers = Configuration.GetSection("X").Get<X>();
foreach (var provider in providers.Providers)
{
if (provider.IsDefaultProvider == true)
{
//var _defaultProvider = (XProviderType)Enum.Parse(typeof(XProviderType), provider.Value.ToString());
}
}
Related
I've simplified the code (below) but I cannot figure out why the Result.Data property is not getting filled; it is always null. I've used jsonlint.com to validate the JSON (both this small sample and the full content). I built a separate project (using How to Deserialize a Complex JSON Object in C# .NET) and it successfully serializes the complex object listed there. But I cannot get this one to work and I'm stumped.
using System.Text.Json;
namespace JsonTest2;
public class Result
{
public string? Total { get; set; }
public string? Limit { get; set; }
public string? Start { get; set; }
protected List<Park>? Data { get; set; }
}
public class Park
{
public string? Id { get; set; }
}
internal class Program
{
var basepath = AppDomain.CurrentDomain.BaseDirectory;
var filepath = basepath.Split("\\bin")[0];
var filename = #$"{filepath}\NPS_response_small.json";
var jsonstr = File.ReadAllText(filename);
var response = JsonSerializer.Deserialize<Result>(jsonstr, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
}
This is the content of "NPS_response_small.json":
{
"total": "468",
"limit": "50",
"start": "0",
"data": [
{
"id": "77E0D7F0-1942-494A-ACE2-9004D2BDC59E"
},
{
"id": "6DA17C86-088E-4B4D-B862-7C1BD5CF236B"
},
{
"id": "E4C7784E-66A0-4D44-87D0-3E072F5FEF43"
}
]
}
you have to chanbe a protected attribute of property Data to a public. Json deserializer doesnt have any acces to this property
public List<Park>? Data { get; set; }
it would be much easier to use Newtonsoft.Json, but if you need protected for some reason, you can try this ( but I am not sure that it is a full replacement)
public List<Park>? Data { protected get; init ; }
[System.Text.Json.Serialization.JsonConstructor]
public Result (List<Park>? Data, string? Total, string? Limit, string? Start)
{
this.Data=Data;
this.Total=Total;
this.Limit=Limit;
this.Start=Start;
}
This is the JSON im receiving, already filtered. (its coming from the google places autocomplete API)
{
"predictions": [
{
"description": "Frankfurt am Main, Deutschland",
"place_id": "ChIJxZZwR28JvUcRAMawKVBDIgQ",
},
{
"description": "Frankfurt (Oder), Deutschland",
"place_id": "ChIJb_u1AiqYB0cRwDteW0YgIQQ",
},
{
"description": "Frankfurt Hahn Flughafen (HHN), Lautzenhausen, Deutschland",
"place_id": "ChIJX3W0JgQYvkcRWBxGlm6csj0",
}
],
"status": "OK"
}
And I need to get this JSON into this format:
{
"success":true,
"message":"OK",
"data":[
{
"description":"Frankfurt Hahn Flughafen (HHN), Lautzenhausen, Deutschland",
"id":"ChIJX3W0JgQYvkcRWBxGlm6csj0"
},
{
"description":"Frankfurt Airport (FRA), Frankfurt am Main, Deutschland",
"id":"ChIJeflCVHQLvUcRMfP4IU3YdIo"
},
{
"description":"Frankfurt Marriott Hotel, Hamburger Allee, Frankfurt am Main, Deutschland",
"id":"ChIJdag3xFsJvUcRZtfKqZkzBAM"
}
]
}
I would be very g
So predictions is just renamed to "data", we change rename status to message, move it up and add a success if the http-request that happened earlier was a success or not. This does not seem so hard on the first catch, but I can't seem to find resources to transform or rearrange JSON in C#.
I would be very grateful for any tips or resources, so I can get unstuck on this probably not so difficult task. I should mention I'm fairly new to all of this.
Thank you all in advance!
First create classes thats represent your jsons
public class Prediction
{
public string description { get; set; }
public string place_id { get; set; }
}
public class InputJsonObj
{
public Prediction[] predictions { get; set; }
public string status { get; set; }
}
public class Datum
{
public string description { get; set; }
public string id { get; set; }
}
public class OutPutJsoObj
{
public bool success { get; set; }
public string message { get; set; }
public List<Datum> data { get; set; }
public OutPutJsoObj(){
data = new List<Datum>();
}
}
Then mapped objects (manually or using any of mapping libraries like AutoMapper) and create final json.
using Newtonsoft.Json;
InputJsonObj inputObj = JsonConvert.DeserializeObject<InputJsonObj >(inputJson);
OutPutJsoObj outObj = new OutPutJsoObj ();
foreach(var p in inputObj)
{
outObj.Data.Add(new Datum() { descriptions = p.descriptions , id= p.place_id }
}
string outJson = = JsonConvert.SerializeObject(outObj);
Just parse the origional json and move the data to the new json object
var origJsonObj = JObject.Parse(json);
var fixedJsonObj = new JObject {
new JProperty("success",true),
new JProperty("message",origJsonObj["status"]),
new JProperty("data",origJsonObj["predictions"])
};
it is not clear from your question what should be a success value, but I guess maybe you need this line too
if (fixedJsonObj["message"].ToString() != "OK") fixedJsonObj["success"] = false;
if you just need a fixed json
json = fixedJsonObj.ToString();
or you can create c# class (Data for example) and deserilize
Data result= fixedJsonObj.ToObject<Data>();
I like the answer from #Serge but if you're looking for a strongly typed approach we can model the input and output structure as the same set of classes and the output structure is similar, with the same relationships but only different or additional names this try this:
The process used here is described in this post but effectively we create write-only properties that will receive the data during the deserialization process and will format it into the properties that are expected in the output.
public class ResponseWrapper
{
[JsonProperty("success")]
public bool Success { get;set; }
[JsonProperty("message")]
public string Message { get;set; }
[Obsolete("This field should not be used anymore, please use Message instead")]
public string Status
{
get { return null; }
set
{
Message = value;
Success = value.Equals("OK", StringComparison.OrdinalIgnoreCase);
}
}
[JsonProperty("data")]
public Prediction[] Data { get;set; }
[Obsolete("This field should not be used anymore, please use Data instead")]
public Prediction[] Predictions
{
get { return null; }
set { Data = value; }
}
}
public class Prediction
{
public string description { get; set; }
public string place_id { get; set; }
}
Then you can deserialize and re-serialize with this code:
using Newtonsoft.Json;
...
var input = JsonConvert.DeserializeObject<ResponseWrapper>(input);
var output = JsonConvert.SerializeObject(objs, new JsonSerializerSettings
{
Formatting = Newtonsoft.Json.Formatting.Indented,
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
});
This is a fiddle you can test with: https://dotnetfiddle.net/DsI5Yc
And the output:
{
"success": true,
"message": "OK",
"data": [
{
"description": "Frankfurt am Main, Deutschland",
"place_id": "ChIJxZZwR28JvUcRAMawKVBDIgQ"
},
{
"description": "Frankfurt (Oder), Deutschland",
"place_id": "ChIJb_u1AiqYB0cRwDteW0YgIQQ"
},
{
"description": "Frankfurt Hahn Flughafen (HHN), Lautzenhausen, Deutschland",
"place_id": "ChIJX3W0JgQYvkcRWBxGlm6csj0"
}
]
}
If you were going to go to the trouble of writing a converter for the deserialization then I find this solution is a bit simpler. I tend to use this type of solution when exposing additional properties to allow legacy data to map into a the current code base.
keeps the mapping and logic contained within the class
tells developers still writing code against the deprecated structures about the change
You can also augment this and implement a global converter to omit obsolete properties which would give you full backwards compatibility until you update the source to stop sending the legacy structure. This is a fiddle of such a solution: https://dotnetfiddle.net/MYXtGT
Inspired by these posts:
JSON.Net Ignore Property during deserialization
Is there a way to make JavaScriptSerializer ignore properties of a certain generic type?
Exclude property from serialization via custom attribute (json.net)
Json.NET: Conditional Property Serialization
How to setup in Net 6 program.cs a multiple connection strings?
I want to work with Development, Staging, and Production environments, all of them pointing to different database servers.
NET 6. Program.cs:
builder.Services.AddDbContext<MyContext>(options =>
{
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));
});
Thanks in advance.
Here's what you could do.
First, create an appsettings.json like this:
appsettings.json
{
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://*:5000"
}
}
},
"WillAppConfig": {
"ActiveEnvironment": "Development",
"DevDatabase": "server:123.123.123.123, user: will, pass:1234",
"STGDatabase": "server:123.123.123.123, user: will, pass:1234",
"ProdDatabase": "server:123.123.123.123, user: will, pass:1234"
}
}
Then create a class somewhere in your project, that will serve to map the configuration to an object.
WillAppConfigurationMap.cs
public class WillAppConfigurationMap
{
public string ActiveEnvironment { get; set; }
public string DevDatabase { get; set; }
public string STGDatabase { get; set; }
public string ProdDatabase { get; set; }
}
Finally in your Program.cs, you could select the connection string to use depending on the value of ActiveEnvironment.
var builder = WebApplication.CreateBuilder(args);
WillAppConfig = builder.Configuration.GetSection("WillAppConfig").Get<WillAppConfigurationMap>();
var connectionString = "";
if (WillAppConfig.ActiveEnvironment == "Development")
{
connectionString = WillAppConfig.DevDatabase
}
else if (WillAppConfig.ActiveEnvironment == "Staging")
{
connectionString = WillAppConfig.STGDatabase
}
else if (WillAppConfig.ActiveEnvironment == "Production")
{
connectionString = WillAppConfig.ProdDatabase
}
builder.Services.AddDbContext<MyContext>(options =>
{
options.UseSqlServer(connectionString));
});
partial class Program
{
public static WillAppConfigurationMap WillAppConfig { get; private set;}
}
You can remove the "Kestrel" section from the appsettings.json if you don't use it. You can use this approach to map any appsettings.json structure.
Then, you can access your configuration object from ANYWHERE in your app doing Program.WillAppConfig.
Create multiple connection strings with different names in appsetting.json:
builder.Services.AddDbContext<MyContext>(options => {
options.UseSqlServer(builder.Configuration.GetConnectionString(UseNameofConnectionString));
});
Also you can create extension method which gives you required connectionString when you call it.
This seems like it should be really simple, I have been searching SO and a lot of other places for an answer to this, everything I have found and tried does not work.
I have an appsettings.json file that looks like this
"Email": {
"Port": "25",
"Host": "localhost",
"EnableSSL": "false",
"Credentials": {
"Username": "fakeuser",
"Password": "fakepassword"
},
"SystemFromAddress": "testsender#localhost.com",
"SystemFromDisplayName": "Test Sender",
"EmailTemplateRootDirectory": "Email\\EmailTemplates",
"EmailTemplates": [
{
"TemplateKey": "ResetPassword",
"TemplatePath": "ResetPassword.cshtml"
},
{
"TemplateKey": "NewAccount",
"TemplatePath": "NewAccount.cshtml"
},
{
"TemplateKey": "VerifyEmail",
"TemplatePath": "VerifyEmail.cshtml"
}
]
}
There are several models (EmailOptions being the parent) that I am trying to bind to, the EmailOptions class is expecting to have it's EmailTemplates list populated from the EmailTemplates list in the appsettings.json as seen above.
The parent class is being populated by the appsettings.json file as expected, the Child List of Email Templates in this class is always coming up empty.
Here are the classes I am binding to.
public class EmailOptions
{
public int Port { get; set; }
public string Host { get; set; }
public bool EnableSSL { get; set; }
public EmailCredentials Credentials { get; set; }
public string SystemFromAddress { get; set; }
public string SystemFromDisplayName { get; set; }
public string EmailTemplateRootDirectory { get; set; }
public IEnumerable<EmailTemplate> EmailTemplates { get; set; } = new List<EmailTemplate>();
}
public class EmailTemplate
{
public string TemplateKey { get; set; }
public string TemplatePath { get; set; }
}
public class EmailCredentials
{
public string Username { get; set; }
public string Password { get; set; }
}
I am using the following call I am making in my startup class in ASP.NET Core.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddOptions();
services.Configure<EmailOptions>( _configuration.GetSection("Email" ));
...
For some reason the IEnumerable property in my EmailOptions is not being deserialized from the appsettings.json into my options - when I attempt to use it anywhere in my controllers - the list is always set to an empty array.
FWIW: I have this working in a console application where I have more control over setting up my options from the appsettings.json. Here is what I am doing in the console app, (I am leaving out the code where I set up the options with the DI container for brevity)
var emailSection = configuration.GetSection( "Email" );
var emailOptions = emailSection.Get<EmailOptions>();
emailOptions.EmailTemplates = configuration.GetSection( "Email:EmailTemplates" ).Get<List<EmailTemplate>>();
as expected - in the console application, I get my Email Templates because i have the ability to get the child list separately and add it to the options before handing it over to the DI container. I don't seem to have that flexibility in the ASP.NET Core IServiceCollection.Configure() extension method (so maybe use another method to do this? which one? After a couple hours of searching I am crying uncle and asking for help).
So how does one get this to work using the ASP.NET Core "IServiceCollection.Configure()" method? Is there a better way to do this?
Thank you Joe for pointing out what needed to happen!
I made the false assumption that the serializer would happily create it's list from the json and assign that list to my IEnumerable - rather - you need to make sure to use List if you intend to deserialize a list of json objects into your Options (and other concrete dotnet types where applicable).
so instead of this
IEnumerable<EmailTemplate> EmailTemplates { get; set; }
I should have had this...
List<EmailTemplate> EmailTemplates { get; set; }
In ASP.NET 4 to organize settings, I am prefixing the setting key with a small word that indicates where this config is used (e.g. key="dms:url", "sms:fromNumber" .... etc).
In ASP.NET 5, the AppSettings configuration is mapped to a strongly typed class.
what is the property that i need to build for "dms:url"? How could map dashes & special chars to a C# property in ASP.NET 5?
You can organize your configuration file within a hierarchy in the config.json
{
"AppSettings": {
"SiteTitle": "PresentationDemo.Web",
"Dms": {
"Url": "http://google.com",
"MaxRetries": "5"
},
"Sms": {
"FromNumber": "5551234567",
"APIKey": "fhjkhededeudoiewueoi"
}
},
"Data": {
"DefaultConnection": {
"ConnectionString": "MyConnectionStringHere. Included to show you can use the same config file to process both strongly typed and directly referenced values"
}
}
}
We defined the AppSettings as a POCO class.
public class AppSettings
{
public AppSettings()
{
Dms = new Dms(); // need to instantiate (Configuration only sets properties not create the object)
Sms = new Sms(); // same
}
public string SiteTitle { get; set; }
public Dms Dms { get; set; }
public Sms Sms { get; set; }
}
public class Dms
{
public string Url { get; set; }
public int MaxRetries { get; set; }
}
public class Sms
{
public string FromNumber { get; set; }
public string ApiKey { get; set; }
}
We then load the configuration into an instance of IConfigurationSourceRoot and then set values of AppSettings using GetSubKey. The best practice would be to do this in ConfigureServices and add it to the DI Container.
public class Startup
{
public Startup(IHostingEnvironment env)
{
// Setup configuration sources.
var configuration = new Configuration()
.AddJsonFile("config.json")
.AddJsonFile($"config.{env.EnvironmentName}.json", optional: true);
}
public void ConfigureServices(IServiceCollection services)
{
// Add Application settings to the services container.
services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));
//Notice we can also reference elements directly from Configuration using : notation
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
}
}
We can now provide access in a controller through the constructor. I set the setting values explicitly the constructor but you could use the entire IOptions
public class HomeController : Controller
{
private string _title;
private string _fromNumber;
private int _maxRetries;
public HomeController(IOptions<AppSettings> settings)
{
_title = settings.Options.SiteTitle;
_fromNumber = settings.Options.Sms.FromNumber;
_maxRetries = settings.Options.Dms.MaxRetries;
}
If you wanted to keep everything flat and use a pseudo hierarchy like you have been doing, you can, but ":" isn't a valid symbol for a variable name. You would need to use a valid symbol like "_" or "-" instead.