Monday, September 3, 2018

Serialize C# DataTable

WriteToXml
Read Xml

When using WritetoXml, do use the overload that requires a file path and writes the xml schema (XmlWriteMode.WriteSchema).
ReadXml will throw an exception saying it does not support schema inference from xml.
In addition, don't forget to set the datatable name with the same name the datatable that was used to write to xml.

Sunday, August 26, 2018

Preferred way to format fractional numbers in C#

double myDouble1 = 0.32;
double myDouble2 = 321.5235;

var currentCulture = CultureInfo.InvariantCulture.Clone() as CultureInfo;
var numberFormat = currentCulture.NumberFormat;
numberFormat.NumberDecimalDigits = 2;

Console.WriteLine(myDouble1.ToString("F", numberFormat));
Console.WriteLine(myDouble2.ToString("F", numberFormat));

Wednesday, August 15, 2018

JwtSecurityTokenHandler.ValidateToken throws exception and mentions setting IdentityModelEventSource.cs's ShowPII to true

Matthew Steeples here solved the mystery:

ShowPII is a static property, and here is how you can set it:
Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true;

Sunday, August 12, 2018

Properly handle insertion of values of type Double into database, independently of CurrentCulture, in C#

if (row[column] is double)
{
    //properly deal with rational numbers - ALWAYS use the same (english - culture invariant) numbering format rules - otherwise data will differ between cultures
    //for example, portuguese uses , as thousand separator, while english uses a dot. This causes mySql to calculate different values form SUM(myColumn).

    var d = Convert.ToDouble(row[column]);
    value = d.ToString(CultureInfo.InvariantCulture);
}

Monday, July 30, 2018

MsBuild generating obj folder only - bin folder not created

I have a jenkins job that calls msbuild.exe like

/t:Clean /p:WarningLevel=3 /P:Configuration=Release /p:AllowUnsafeBlocks=true

only when I dropped the /t:Clean option, did the folder bin finally get back being created again.

Wednesday, July 25, 2018

Workaround for the inability to assign a name to a Task in C#

There is no way to set a name to a Task. Sometimes seems this would be useful, like we used to do with Threads, so we could know which threads are still running, i.e., which pieces of our whole processing are still left to do.

I thought of using a dictionary to relate Task ids and names we want to assign them.
Here is a sample.