Skip to main content

Posts

Showing posts with the label C#

Blazor component lifecycle events

Lifecycle of Blazor Components Blazor components have a lifecycle that defines the various stages a component goes through from its creation to its disposal. Understanding the component lifecycle is crucial for managing state, optimizing performance, and responding to events. Here's an overview of the Blazor component lifecycle events along with sample code to illustrate each stage: Initialization : This is the initial stage where the component's parameters and dependencies are set. It happens before rendering. @code { [Parameter] public string Message { get ; set ; } protected override void OnInitialized ( ) { // Initialization logic } } <h3>@Message</h3> Initialize a database connection Load data from a server Set up bindings Create timers or other asynchronous tasks Parameter Set : This stage occurs when component parameters are set. It can be useful for reacting to changes in parameters. @code { [Parameter] publ

WebAssembly vs Java Applets

A Detailed Comparison: WebAssembly vs. Java Applets WebAssembly and Java Applets are technologies aimed at enhancing web applications by enabling execution of code directly in the browser. However, they have significant differences in terms of architecture, performance, security, and modern web development practices. Let's compare these two technologies in-depth, along with scenarios and code samples. WebAssembly Architecture: WebAssembly is a binary instruction format that allows high-level languages to be compiled into a low-level bytecode that can run efficiently in browsers. It provides a secure sandboxed environment where code can execute without direct access to browser internals. WebAssembly modules can be integrated into web applications, enabling performance-critical components to run faster than JavaScript. Performance: WebAssembly code can achieve near-native performance, making it suitable for applications requiring complex computations or real-time interacti

Overview of WebAssembly

WebAssembly: Revolutionizing Web Application Development WebAssembly (Wasm) is a groundbreaking technology that enables developers to run high-performance code in web browsers at near-native speeds. It has the potential to significantly change the way web applications are built, offering new levels of performance, security, and cross-platform compatibility. In this article, we'll explore what WebAssembly is, how it affects web app development, and provide a detailed example of building an e-commerce application using C# and .NET Core. What is WebAssembly? WebAssembly is a binary instruction format designed as a portable compilation target for high-level programming languages like C, C++, Rust, and others. It aims to provide efficient and secure execution of code on web browsers by allowing developers to compile their applications to a bytecode format that can be executed in a sandboxed environment. This bytecode can be executed at near-native speeds, providing performance improv

Introduction to Blazor

Introduction to Blazor with C# Blazor is a modern web framework developed by Microsoft that allows developers to Build interactive web applications using C# and .NET instead of traditional web technologies like JavaScript. Blazor enables you to create web applications using the same language and tools you use to build other .NET applications, such as desktop and mobile applications. It provides a way to write client-side code that runs directly in the browser while leveraging the power of C# and the .NET runtime. How Blazor Works Blazor offers two hosting models: Blazor WebAssembly and Blazor Server. Blazor WebAssembly In the WebAssembly hosting model, the Blazor application is compiled into WebAssembly bytecode, which is a binary instruction format designed for safe and efficient execution in web browsers. This bytecode is downloaded by the browser and executed directly in a secure sandbox environment. The application communicates with the server only during initial dow

How to design a method

Introduction We have been reading articles about how to architect / design a system, microservices etc. However, I still find that there are developers that require some details on how to write / create a method. These are valid for statically typed languages like C# , Java which I have worked. though some of them apply to Javascript as a good practice. Below are some of the points that I consider worth noting The method should have a descriptive verb of the action that it is performing. ReadFromConfigurationFile SaveUserData CheckInOrderPriorToPayment 2. The arguments if any to the method should be validated prior to consumption. There are some built-in types in Java that helps in checking for nulls like  requireNonNull  from  java.util Note:  The below given method is one that i had built which checks for null and empty value and if so, throws suitable exceptions. We have been using similar built-in functionality in  Microsoft Enterprise Library  for .Net as  Guard.Null  or  Guard.Nu

Custom serialization Using Newtonsoft.Json

When using Newtonsoft. Json to serialize objects, we can avoid few properties from being included in the output for some use case and the same property be included in the serialized output in other cases. There's a feature in the Newtonsoft.Json.NET library, that lets us  determine at runtime whether or not to serialize a particular property of a class / object. During the process of creating a class, we have to include a public method named ShouldSerialize{MemberName} returning a boolean value. Json.NET will call that method during serialization to determine whether or not to serialize the corresponding property of the class. If this method returns true, the property will be serialized; otherwise, it will be ignored. The following illustration shows how this can be achieved. The following is a data definition / class that may be serialized using Newtonsoft.Json.Net public class AuthTypeClaimMapViewModel {     [JsonIgnore]     public bool? _canSerializeName { get;

Copy databases using SQL Server SMO

The following steps are required to perform the Copying a Database using SQL Server objects exposed to the .Net Framework by Microsoft. Net 1. Add reference to the project with the following DLL files, Microsoft.SqlServer.ConnectionInfo.dll Microsoft.SqlServer.Smo.dll Microsoft.SqlServer.SmoExtended.dll Microsoft.SqlServer.Management.Sdk.Sfc.dll These dlls are found in the following folder C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies 2. Define a source Db and then the target DB, the source db will be scanned and then its contents [structure & data] string sourceDB = "cs_notification_test"; string targetDB = "cs_notification_test1"; SqlConnectionStringBuilder connStringBuilder = new SqlConnectionStringBuilder(System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationConnectionString"].ConnectionString); ServerConnection conn = new ServerConnection(connStringBuilder.DataSource, connStringBuilder.UserID, connStr

Handling Enumerations in C# as Strings

In Order for an Enumeration in C# to be processed as a string, use the string formats described below, Sample: enum Colors {Red, Green, Blue, Yellow = 12}; Colors myColor = Colors.Yellow; myColor.ToString("g") = Yellow myColor.ToString("G") = Yellow myColor.ToString("x") = 0000000C myColor.ToString("X") = 0000000C myColor.ToString("d") = 12 myColor.ToString("D") = 12 myColor.ToString("f") = Yellow myColor.ToString("F") = Yellow As shown in the above samples, the following format specifiers :   g,G,x,X,d,D,f and F are used to convert a integer based enum value to string format. -- Saravanan

Comparing Strings in C#.Net

In order to compare strings for their equivalence, we have to follow this Rule from Microsoft " When you specify either  StringComparison.Ordinal  or  StringComparison.OrdinalIgnoreCase , the string comparison will be nonlinguistic. That is, the features that are specific to the natural language are ignored when comparison decisions are made. This means the decisions are based on simple byte comparisons and ignore casing or equivalence tables that are parameterized by culture." -- Saravanan

iTextSharp Documentation and Guides

Useful Resource Links for iTextSharp Pdf Library for .Net Framework. The following links are found to have useful references while consuming the iTextSharp Pdf library. Google Search Results Documentation from AfterLogic 4GuysFromRolla StackOverFlow Solutions StackOverFlow Solutions C# Samples Mikes Dotnet blog -- Saravanan

SFTP and File Upload in SFTP using C# and Tamir. SShSharp

The right choice of SFTP Server for Windows OS Follow the following steps, 1. Download the server version from here . The application is here 2. Provide the Username, password and root path, i.e. the ftp destination. 3. The screen shot is given below for reference. 4. Now download the CoreFTP client from this link 5. The client settings will be as in this screen shot: 6. Now the code to upload files via SFTP will be as follows. //ip of the local machine and the username and password along with the file to be uploaded via SFTP. FileUploadUsingSftp("172.24.120.87", "ftpserveruser", "123456", @"D:\", @"Web.config"); private static void FileUploadUsingSftp(string FtpAddress, string FtpUserName, string FtpPassword, string FilePath, string FileName) { Sftp sftp = null; try { // Create instance for Sftp to upload given files using given credentials sf

HTML to C# Color Conversion

To convert a System.Drawing.Color to the HTML color format (Hex value or HTML color name value) and back. System.Drawing.Color c = System.Drawing.ColorTranslator.FromHtml("#F5F7F8"); String strHtmlColor = System.Drawing.ColorTranslator.ToHtml(c);

C# Serialization

To use the XML Serializer in C#.NET, use as follows public static class SerializationHelper { /// /// Serializes the object and returns the serialized object's xml string /// /// The obj. /// public static string SerializeObject(Object obj) { var xmlSerializer = new System.Xml.Serialization.XmlSerializer(obj.GetType()); System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); xmlSerializer.Serialize(memoryStream, obj); memoryStream.Seek(0, System.IO.SeekOrigin.Begin); return new System.IO.StreamReader(memoryStream).ReadToEnd(); } /// /// Desserializeobjs the specified object XML string. /// /// /// The object XML string. /// public static object DeSerializeObject (string objectXmlString) { var xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T)); using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream()) { using (System.IO.StreamWriter swr = new System.IO.StreamWriter(memoryStream))