Add free search for your website. Sign up now! https://webveta.alightservices.com/
Categories
.Net Architecture C#

Some C# reusable code snippets – Video

Some C# reusable code snippets – Video

Categories
.Net C#

File tailer in C#

As mentioned in a previous post – Cancelling all activities at ALight Technology And Services Limited. I am not seriously working on anything within ALight Technology And Services Limited, but I am still .Net developer. I have some internal logging and monitoring implemented. I have used Cloudwatch Agent and Promtail for ingesting logs into AWS Cloudwatch and Grafana. Both are great platforms but I wanted to implement a similar tool in C#. Here is some sample code, explanation capable of doing something similar.

First store the name of file, Creation Date, Size, Last Modified Date. These can be accessed from FileInfo object.

var fi = new FileInfo("path");
fi.CreationTimeUtc	
fi.LastWriteTimeUtc	
fi.Length

Now, in a loop read the file, if new file i.e CreationTime is different read from beginning. If Length or LastWriteTime are different but same CreationTime, do a seek.

long seek = 0;

while(<some condition>)
{
   using(sr = new StreamReader(fi.OpenRead()){
      sr.BaseStream.Seek(seek, SeekOrigin.Begin);
      var data = await sr.ReadToEndAsync();
      seek += data.Length;
      // Do something with data
      // Store filename, seek in some persistent storage like a file
}

The above code block shows some sample code for using seek, we would store the variable seek along with FileCreation, Update, Length. Even if the application is restarted, the application would work properly.

Mr. Kanti Kalyan Arumilli

Arumilli Kanti Kalyan, Founder & CEO
Arumilli Kanti Kalyan, Founder & CEO

B.Tech, M.B.A

Facebook

LinkedIn

Threads

Instagram

Youtube

Founder & CEO, Lead Full-Stack .Net developer

ALight Technology And Services Limited

ALight Technologies USA Inc

Youtube

Facebook

LinkedIn

Phone / SMS / WhatsApp on the following 3 numbers:

+91-789-362-6688, +1-480-347-6849, +44-07718-273-964

kantikalyan@gmail.com, kantikalyan@outlook.com, admin@alightservices.com, kantikalyan.arumilli@alightservices.com, KArumilli2020@student.hult.edu, KantiKArumilli@outlook.com and 3 more rarely used email addresses – hardly once or twice a year.

Categories
.Net C# DailyReads

DailyReads 20/12/2022

How to Secure Passwords with BCrypt.NET

“In this article, we are going to learn how to secure passwords with BCrypt.NET to ensure we are up to the industry standards when it comes to security in our .NET environment.”

Schedule Jobs with Quartz.NET

“Recurring, background tasks are widespread and very common when building applications. These tasks can be long-running or repetitive and we don’t want to run them in the foreground, as they could affect the user’s experience of the application. So instead we must schedule these jobs to run in the background somewhere. To achieve this in .NET, we can use the Quartz.NET library to manage the creation and scheduling of these jobs.”

Mr. Kanti Kalyan Arumilli

Arumilli Kanti Kalyan, Founder & CEO
Arumilli Kanti Kalyan, Founder & CEO

B.Tech, M.B.A

Facebook

LinkedIn

Threads

Instagram

Youtube

Founder & CEO, Lead Full-Stack .Net developer

ALight Technology And Services Limited

ALight Technologies USA Inc

Youtube

Facebook

LinkedIn

Phone / SMS / WhatsApp on the following 3 numbers:

+91-789-362-6688, +1-480-347-6849, +44-07718-273-964

kantikalyan@gmail.com, kantikalyan@outlook.com, admin@alightservices.com, kantikalyan.arumilli@alightservices.com, KArumilli2020@student.hult.edu, KantiKArumilli@outlook.com and 3 more rarely used email addresses – hardly once or twice a year.

Categories
.Net C# DailyReads

Daily Reads 19/12/2022

Nullable Types in C#

In this article, we will extensively break down the concept of Nullable types in C#. We will discuss the kinds of Nullable types in C#, their characteristics, and how we can use them when building applications...”

How To Structure Your .NET Solutions: Architecture And Trade-Offs

“How should you design the structure of your .NET solutions? Microservices? Monolith? Feature folders? Clean architecture? Shared databases?…”

Frozen collections in .NET 8

“.NET 7 was freshly released but Microsoft does not sleep. .NET 8 is already in the making and I want to showcase to you one new area where the dotnet team is working on Frozen collections…”

PriorityQueues on .NET 7 and C# 11

“Starting from .NET 6 and C# 10, we finally have built-in support for PriorityQueues…”

Mr. Kanti Kalyan Arumilli

Arumilli Kanti Kalyan, Founder & CEO
Arumilli Kanti Kalyan, Founder & CEO

B.Tech, M.B.A

Facebook

LinkedIn

Threads

Instagram

Youtube

Founder & CEO, Lead Full-Stack .Net developer

ALight Technology And Services Limited

ALight Technologies USA Inc

Youtube

Facebook

LinkedIn

Phone / SMS / WhatsApp on the following 3 numbers:

+91-789-362-6688, +1-480-347-6849, +44-07718-273-964

kantikalyan@gmail.com, kantikalyan@outlook.com, admin@alightservices.com, kantikalyan.arumilli@alightservices.com, KArumilli2020@student.hult.edu, KantiKArumilli@outlook.com and 3 more rarely used email addresses – hardly once or twice a year.

Categories
.Net Architecture C# gRPC

Little bit of live coding for PodDB January 1st 2023 update

PodDB has been decommissioned in November 2023!

https://www.youtube.com/watch?v=o5lhNu6vwbk
Categories
.Net

Nuget Repository for ALight Technology And Services Limited

The Founder & CEO of ALight Technology And Services Limited, Mr. Kanti Kalyan Arumilli is committed to share I.T knowledge, code snippets and some open source development.

A new nuget account has been created for publishing nuget packages. As of now there are no published packages yet. Sitemap Generator package can be expected soon. I do have some plans of NLog logger packages but cannot confirm yet. The URL of the nuget package repository is: https://www.nuget.org/profiles/ALightTechnologyAndServicesLimited.

Mr. Kanti Kalyan Arumilli

Arumilli Kanti Kalyan, Founder & CEO
Arumilli Kanti Kalyan, Founder & CEO

B.Tech, M.B.A

Facebook

LinkedIn

Threads

Instagram

Youtube

Founder & CEO, Lead Full-Stack .Net developer

ALight Technology And Services Limited

ALight Technologies USA Inc

Youtube

Facebook

LinkedIn

Phone / SMS / WhatsApp on the following 3 numbers:

+91-789-362-6688, +1-480-347-6849, +44-07718-273-964

kantikalyan@gmail.com, kantikalyan@outlook.com, admin@alightservices.com, kantikalyan.arumilli@alightservices.com, KArumilli2020@student.hult.edu, KantiKArumilli@outlook.com and 3 more rarely used email addresses – hardly once or twice a year.

Categories
.Net ASP.Net C# DailyReads

Daily Reads 17/12/2022

Build a web API with minimal API, ASP.NET Core, and .NET 6

20 minutes module on Microsoft Learn

Use a database with minimal API, Entity Framework Core, and ASP.NET Core

35 minutes module on Microsoft Learn

Create a full stack application by using React and minimal API for ASP.NET Core

28 minutes module on Microsoft Learn

The above 3 are part of Create web apps and services with ASP.NET Core, minimal API, and .NET 6 learning path.

Canceling abandoned requests in ASP.NET Core – Blog article on how to stop processing of abandoned requests in ASP.Net MVC i.e if a browser loads a page but clicks stop or presses escape key. This way, some server-side resources can be saved.

Two of my favorite and absolutely free desktop based (downloadable) software for diagrams – cloud architecture, UML, Database ER etc… Very useful for small startups and developers.

  1. Diagrams.net
  2. Dia

Mr. Kanti Kalyan Arumilli

Arumilli Kanti Kalyan, Founder & CEO
Arumilli Kanti Kalyan, Founder & CEO

B.Tech, M.B.A

Facebook

LinkedIn

Threads

Instagram

Youtube

Founder & CEO, Lead Full-Stack .Net developer

ALight Technology And Services Limited

ALight Technologies USA Inc

Youtube

Facebook

LinkedIn

Phone / SMS / WhatsApp on the following 3 numbers:

+91-789-362-6688, +1-480-347-6849, +44-07718-273-964

kantikalyan@gmail.com, kantikalyan@outlook.com, admin@alightservices.com, kantikalyan.arumilli@alightservices.com, KArumilli2020@student.hult.edu, KantiKArumilli@outlook.com and 3 more rarely used email addresses – hardly once or twice a year.

Categories
.Net ASP.Net AWS C# DailyReads

Coming soon Daily Reads

As a knowledge worker, I.T developer I have a need to stay up to date with the latest technologies. Similarly millions of I.T developers. I cannot cater to the needs of every knowledge workers or I.T developers but for .Net developers, I am planning to post recommended reads every day. The daily recommended reads would be posted almost every day. The links would be for 3rd party blog posts. The topics would be more about I.T computing, cloud computing, .Net, web development etc…

Subscribe…

Categories
.Net C#

C# Code snippets for generating XML and sitemaps

I have explained sitemaps in an earlier blog post – Sitemaps an intro! In this blog post we would look at some code snippets for generating XML sitemaps. Two approaches are mentioned. This blog post is about sample code snippets. Read the comments in the code.

I might consider creating a small library for generating XML sitemaps and open sourcing the library.

The general structure of a valid XML document contains a XML declaration i.e the <?xml….>. Then there would be one and exactly one root node and the root node would contain other child nodes.

With XML sitemap, we would have the XML declaration, the <urlset> root node. The <urlset> root node would have one or more <url> child nodes upto a maximum of 50,000. The <url> node would have one and only one mandatory <loc> node. The <lastmod>, <changefreq>, <priority> are optional. If the optional <lastmod>, <changefreq>, <priority> nodes are present, there can be a maximum of one child node per <url> node.

Business logic validations would be:

  1. The <url> nodes can be between 1 and 50,000.
  2. Unique URL’s
  3. <loc> is mandatory and the value must be a valid http or https URL.
  4. If present, <lastmod> must be in valid w3c format, there can be 0 or 1 <lastmod> per <url> node.
  5. <changefreq> can be 0 or 1 per <url> node and must be one of the following:
    • always
    • hourly
    • daily
    • weekly
    • monthly
    • yearly
    • never
  6. <priority> can be 0 or 1 per <url> node and must be a valid number between 0 and 1 such as 0.1 or 0.2 etc…

Approach – 1:

In the following code snippets we would not be dealing with the business logic. This is for purely creating.

XmlDocument xmlDoc = new XmlDocument();
var declaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
xmlDoc.AppendChild(declaration);

XmlNode urlSet = xmlDoc.CreateElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");

var url = xmlDoc.CreateElement("url");
var loc = xmlDoc.CreateElement("loc");
loc.InnerText = "https://www.google.com";

url.AppendChild(loc);

var lastMod = xmlDoc.CreateElement("lastmod");
lastMod.InnerText = $"{DateTime.UtcNow.ToString("s")}Z";
url.AppendChild(lastMod);

urlSet.AppendChild(url);

xmlDoc.AppendChild(urlSet);


// Append more <url> nodes.

// Move the logic of generating url nodes into a separate method, call the method repetitively, apply business logic etc...




xmlDoc.Save(@"C:\temp\sitemap.xml");

Approach – 2:

In this also we won’t be applying business logic, this is a sample code snippet.

UrlSet.cs

[XmlRoot(ElementName = "urlset", Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")]
    public class Urlset
    {
        [XmlElement(ElementName = "url", Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")]
        public List<Url> Url { get; set; }
        [XmlAttribute(AttributeName = "xmlns")]
        public string Xmlns { get; set; }

        public Urlset()
        {
            Xmlns= "http://www.sitemaps.org/schemas/sitemap/0.9";
            Url = new List<Url>();
        }
    }
Url.cs

public class Url
    {
        [XmlElement(ElementName = "loc", Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")]
        public string Loc { get; set; }
        [XmlElement(ElementName = "lastmod", Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")]
        public string Lastmod { get; set; }
        [XmlElement(ElementName = "changefreq", Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")]
        public string Changefreq { get; set; }
        [XmlElement(ElementName = "priority", Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")]
        public double Priority { get; set; }

        public Url()
        {
            Lastmod = DateTime.Now.ToLongDateString();
            Priority = 0.5;
            Changefreq = "Never";
        }
    }
// Generation

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

//Adding an empty namespace
ns.Add("", "");

var urlSet = new Urlset();
urlSet.Url.Add(new Url { Loc = "https://www.sample.com" });
urlSet.Url.Add(new Url { Loc = "https://www.sample.com/page1" });
XmlSerializer serializer = new XmlSerializer(typeof(Urlset));
string utf8;
using (StringWriter writer = new Utf8StringWriter())
{
    serializer.Serialize(writer, urlSet, ns);
    utf8 = writer.ToString();
    Console.WriteLine(utf8);
}
// DeSerialization - useful for modifications and deletions
XmlSerializer serializer = new XmlSerializer(typeof(Urlset));

var deserializedUrlSet = serialization.Deserialize(new StringReader(utf8));

// Here utf8 is a string with the xml, refer to the overloads of Deserialize for other ways of passing XML

Mr. Kanti Kalyan Arumilli

Arumilli Kanti Kalyan, Founder & CEO
Arumilli Kanti Kalyan, Founder & CEO

B.Tech, M.B.A

Facebook

LinkedIn

Threads

Instagram

Youtube

Founder & CEO, Lead Full-Stack .Net developer

ALight Technology And Services Limited

ALight Technologies USA Inc

Youtube

Facebook

LinkedIn

Phone / SMS / WhatsApp on the following 3 numbers:

+91-789-362-6688, +1-480-347-6849, +44-07718-273-964

kantikalyan@gmail.com, kantikalyan@outlook.com, admin@alightservices.com, kantikalyan.arumilli@alightservices.com, KArumilli2020@student.hult.edu, KantiKArumilli@outlook.com and 3 more rarely used email addresses – hardly once or twice a year.

Categories
.Net AWS C# Cloudwatch NIST Security

Some Monitoring & Alerting tips for AWS workloads

This blog post is about some monitoring and alerting tips for AWS workloads.

  1. AWS Console Logins – Root or IAM user
  2. SSH into an EC2 instance

The above mentioned are considered primary. In addition the following monitoring are necessary:

3. What actions were performed by users and/or AWS such as launching EC2 instances (manual or autoscaling) or configuring Route53 or Security Groups etc…

4. Web logs, Load Balancer logs, Cloudfront logs in rare cases of DDOS attacks by the baddies.

5. Application logs

6. Database logs

7. System logs

In the next few weeks, I would be writing or even live videos / tutorials on how to monitor and alert for 1, 2 and 3. Some of these are based on using existing systems and in some cases, I would show manual and programmatic (C# preferred language of choice) approaches.

I would also share some blog posts on how to ingest logs into AWS Cloudwatch (5 GB ingestion free and some other costs) and Grafana (50GB ingestion free), discuss advantages and disadvantages of both.

As part of implementing NIST cyber security framework at ALight Technology And Services Limited, I am implementing these. I like sharing my knowledge with others as I come across new things, learn new things, even existing knowledge when appropriate, sometimes a blend of existing knowledge and new things.

Mr. Kanti Kalyan Arumilli

Arumilli Kanti Kalyan, Founder & CEO
Arumilli Kanti Kalyan, Founder & CEO

B.Tech, M.B.A

Facebook

LinkedIn

Threads

Instagram

Youtube

Founder & CEO, Lead Full-Stack .Net developer

ALight Technology And Services Limited

ALight Technologies USA Inc

Youtube

Facebook

LinkedIn

Phone / SMS / WhatsApp on the following 3 numbers:

+91-789-362-6688, +1-480-347-6849, +44-07718-273-964

kantikalyan@gmail.com, kantikalyan@outlook.com, admin@alightservices.com, kantikalyan.arumilli@alightservices.com, KArumilli2020@student.hult.edu, KantiKArumilli@outlook.com and 3 more rarely used email addresses – hardly once or twice a year.