Load data from App.config and connect to property - c#

I have defined:
public static string Firstname { get; set; }
public static string Surname { get; set; }
public static bool DocBook5 { get; set; }
public static string Language { get; set; }
I stored that information in a App.config.
Then i used that to load and connect:
Firstname = ConfigurationManager.AppSettings["firstname"];
Surname = ConfigurationManager.AppSettings["surname"];
DocBook5 = Convert.ToBoolean(ConfigurationManager.AppSettings["docbook5"]);
Language = ConfigurationManager.AppSettings["language"];
But if i'm giving out the properties content with Console.WriteLine it looks like the properties are empty.
The full code of the GetConfig class can be shown there
Maybe anyone knows why?

As mentioned in my comments you are reading the wrong section not from appsetting . If you need a custom section in you config please follow this link .http://haacked.com/archive/2007/03/12/custom-configuration-sections-in-3-easy-steps.aspx/ else move them to appsetting section

Look at your App.config.
The data you stored is in UserSettings and not in appSettings, but you fetched the data from appSettings.
Either you put all your data in appSettings, or you define configSections, in which you add the name UserSettings. (Use 'GetSection' instead of '
AppSettings')
Later, in code, you need to request the specific section, and get the data from there.
Edit: Let me know if you want code example, to match your App.config file

Related

How to use localization with data annotation in a separate DTO Assembly (Asp.net Core Web Api)

I have:
a Dto.Library (.Net 5 Library)
a SharedResourceLibrary with Resource.resx (.Net 5 Library)
How can i use the Resource File Messages in conjunction with Data Annotation in my DTO.Library?
The ErrorMessage should be the text from the resx files:
public class MeterForCreationDto
{
[Required(ErrorMessage = "Name must not be empty!")]
public string Name { get; set; }
[Required(ErrorMessage = "Unit must not be empty!")]
public string Unit { get; set; }
}
SharedResourceLibrary: looks like this answer #Shiran Dror
You can create a custom attribute for the properties. Something like this:
public class LocalizedDisplayNameAttribute: DisplayNameAttribute
{
public LocalizedDisplayNameAttribute(string resourceKey)
: base(GetMessageFromResource(resourceId))
{ }
private static string GetMessageFromResource(string resourceKey)
{
// return the translation out of your .rsx files
}
}
and then you need to add
public class MeterForCreationDto
{
[LocalizedDisplayName("Name")]
public string Name { get; set; }
[LocalizedDisplayName("Unit")]
public string Unit { get; set; }
}
but you need to add exactly the same key in the attribute which is in your .rsx file. If your searching for "asp.net localizeddisplayname" there are a lot of different sites with examples.
Some help for creating custom attributes:
https://learn.microsoft.com/en-gb/dotnet/standard/attributes/writing-custom-attributes
Hopefully, it helps. :)
you can use:
[Required(ErrorMessageResourceName ="NameInRecourseFile",ErrorMessageResourceType =typeof(RecourseFileName))]
also u need to make the Recourse file public from this menu:
finally u need to have a default Resource file[for your default culture] Default resource file name shouldn't have any specific culture (.en،.fr،....)
SharedService.en.resx => SharedService.resx note .en is the default culture in your app
so it will like these:
SharedService.resx[for your default culture]
SharedService.ar.resx
SharedService.fr.resx
Hope this helped you.
Best wishes.

Backendless c# desktop application saving data but by empty or null values

I have a desktop app written in c# and I added app id and key id
and used this code to add data to database but the data is always empty or null.
var film = new Film();
film.setName(“soooft”);
film.setGenre(“aaa”);
film.setPlot(“fdgveqw”);
film.setUrl(“gdfwrw”);
var f = Backendless.Data.Of<Film>().Save(film);
I googled Backendless and it's a third-party solution. (See https://github.com/Backendless/.NET-SDK)
Usage gets explained at https://backendless.com/docs/dotnet/data_data_object.html
But I'm suspicious about why you use setName(), setGenre(), setPlot and setUrl in your code. Seems your Film class is missing properties. I would expect you'd be writing this instead:
var film = new Film();
film.Name = “soooft”;
film.Genre = “aaa”;
film.Plot = “fdgveqw”;
film.Url = “gdfwrw”;
But that would mean those fields are declared as public properties in your class like this:
public class Film
{
public string Name { get; set; }
public string Genre { get; set; }
public string Plot { get; set; }
public string Url { get; set; }
}
So I don't know why you have those setName and other methods. The Backendless API specifies that these fields need to be public properties so it can read them through reflection. Your code seems to suggests that they're not proper properties as indicated by their example and my code of the Film() class.
Make sure to use public get/set properties instead of private fields and the data will be saved properly.

XML config path through c#

I have a xml file which the path to a particular report folder hardcoded.
eg. C:\git\ProjNew\Proj\Projects_2018\ and C:\git\ProjNew\Proj\Reports\
and xml looks like this:
<SampleFiles>
<SampleFileLocation>C:\git\ProjNew\Proj\Projects_2018\ </SampleFileLocation>
<ReportFileLocation>C:\git\ProjNew\Proj\Reports\</ReportFileLocation>
</SampleFiles>
when someone gets this project from git, they have to manually go to this path and make changes. In the background code, there is a lot of logic happening, is there a way to update it in c#
The c# file looks like this:
[XmlRoot(ElementName = "SampleFiles")]
public class SampleFiles
{
public string SampleFileLocation { get; set; }
public string ReportFileLocation { get; set; }
public SampleFiles()
{
SampleFileLocation = null;
ReportFileLocation = null;
}
}
how do i modify this code in c# so that it doesnt need to be updated by each user .
Thanks,

Dotnet core MVC xml parameter binding always give null

I've read a few questions on SO but the solutions are all for ASP.NET webApi not dotnet core.
I've added xml support in my Startup.cs
services
.AddMvc()
.AddXmlSerializerFormatters();
Here's my controller method:
[Route("main")]
[HttpPost]
public string MainPost([FromBody]MessageModel msg)
{
_log.LogInformation("body msg ="+msg.Content);
return "test";
}
Here's my XML
<xml>
<ToUserName>toUser</ToUserName>
<FromUserName>FromUser</FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType>text</MsgType>
<Content>test content</Content>
<MsgId>1234567890123456</MsgId>
</xml>
Here's my Model class:
[XmlRoot("xml")]
public class MessageModel
{
public string ToUserName { get; set; }
public string FromUserName { get; set; }
public DateTime CreateTime { get; set; }
public string MessageType { get; set; }
public string Content { get; set; }
public int MsgId { get; set; }
}
When I send post request(with header "application/xml") to this route, it always gives me null for MessageModel msg
What am I missing here?
I know there's a DataContract thing but I can't control the xml format sending
to my server so I need a way to bind xml in the format stated above to my object.
Any help or lead to any document will be much appreciated.
I've found the problem thanks to #jdweng 's comment which gives me a idea on what might go wrong.
The problem is that there are some elements in the XML cannot be serialized to the designated type in my model class. They're DateTime CreateTime and int MsgId.
So changing all the fields to string solved problem.
I don't know whether why isn't dotnet core tells us about this instead of just giving us null if the binding fails due to this kind of reason, this is definitely something they can improve on.

azure table storage doesn't add new fields

I created a table in an Azure Table Storage and added a few records using my Entity class derived from TableEntity . After this I added two more properties to this class and tried to insert more records, but looks like the new fields are not added to the storage, and only the old fields are written and read.
Am I missing something? I have to do something more to change the layout of the table?
thanks for the info but in the end I found that the problem was another. I used Vs to create the properties in the class, and doing this Vs created properties add an internal setter.
Looks like that in this situation The Azure Storage client simply ignore these properties not creating the fields not writing and not reading, giving no errors at all.
Removed the internal keyword started to work correctly.
First, I want to make sure that I understand your scenario. You modified the existing entity class by adding two more properties, then added new entities in the table. You were not able to see two newly added properties, but only able to update/retrieve the old properties. If that is the scenario that you are trying to implement, check the following. You should be able to add new properties.
Add new properties in the class that you derived your entity from TableEntity class. Optional1 and Optional2 are new properties that you are trying to add.
public class CustomerEntity : TableEntity
{
public CustomerEntity() { }
public CustomerEntity(string lastName, string firstName)
{
this.PartitionKey = lastName;
this.RowKey = firstName;
}
public string Email { get; set; }
public string CellPhoneNumber { get; set; }
public string Optional1 { get; set; }
public string Optional2 { get; set; }
}
}
Make sure you set Optional1 and Optional2 values. See the sample code below.
var customer = new CustomerEntity(LastName, FirstName)
{
Email = Email,
CellPhoneNumber = cellPhoneNumber,
Optional1 = optional1,
Optional2 = optional2,
}
TableOperation insertOperation = TableOperation.Insert(customer);
TableName.Execute(insertOperation);
Note: I have not compiled the above code so there may be typos and etc.
Thanks,
Aung
In my case the fields that I define are nullable. Even though I provided values for them, the fields are still not created. Had to make them not nullable so the fields can be created.

Categories