Thursday, November 29, 2018

how to consume a web api implemented by lastest .Net framework from a web applicaton from a older .Net framework?

when i try to call a web api that implemented by a .Net framework 4.6 and above.

i always receive the error message "Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host." in response immediately.




the root cause of this error is the Web API implemented with .Net Framework 4.6 and later use the Security Protocol 1.2 by default.and Web application implemented with .net framework 4.5 and earlier will use security protocol 1.1 by default..

the fix of this issue is very easy. we just have to setup the security protocol before calling the web api.

the following snippet of code will do the job

ServicePointManager.Expect100Continue = true;
               ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                      | SecurityProtocolType.Tls11
                      | SecurityProtocolType.Tls12
                      | SecurityProtocolType.Ssl3;




Tuesday, August 14, 2018

how to quickly write a console application to delete file within period of time with .Net Core

when we a console application to clean some historical files from the folder, we will add an app settings section in the app.config file in project, which is run under .net framework. however it is much flexible to run with .Net Core.

i will show you my first sample DotNet Core Console App to handle this task.

1. Launch Visual Studio and create a  new project by selecting the Console App (.Net Core)



2.  Open the management studio and select the project, then right click to show the menu, select Add to get the New Item from slide out menu. to add JSon Config file





3.  Open the JSon file and filll with the following information

 {
  "PrintFolder": "the folder you want to delete files",

  "FileExtension": ".pdf",

  "DaysToMaintain":  "15"
}




4. Load the config information into the program using the snippet of code below

IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
var fileExtension = config["FileExtension"];
var daysToKeepFiles = -1* Convert.ToInt32(config["DaysToMaintain"]);
var ltlPrintFolder = config["LTLPrintFolder"];
Console.WriteLine($"ltl print folder path : { ltlPrintFolder}");


5. Use the LINQ to manipulate the logic on what kind of file and Periods based on the file creation time.
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(ltlPrintFolder); IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
 IEnumerable<System.IO.FileInfo> fileQuery =
                from file in fileList
                where file.Extension == fileExtension && file.CreationTime < DateTime.Now.AddMonths(daysToKeepFiles)
                orderby file.Name
                select file;




6. loop through the result from the query

foreach (System.IO.FileInfo fi in fileQuery)
            {
                Console.WriteLine(fi.FullName);
                fi.Delete();
            }


Now you can delete file with Dot Net Core App