Skip to main content

Posts

Showing posts from April, 2024

How to write Unit Tests in .net

Unit tests are automated tests that verify the behavior code like methods and functions. Writing unit tests is crucial to clean coding, as they help ensure your code works as intended and catches bugs early in the development process. I can share some tips for writing effective unit tests: Write tests for all public methods Every public method in your code should have a corresponding unit test. This helps ensure that your code behaves correctly and catches any unexpected behavior early. public class Calculator { public int Add(int a, int b) { return a + b; } } [TestClass] public class CalculatorTests { [TestMethod] public void Add_ShouldReturnCorrectSum() { // Arrange Calculator calculator = new Calculator(); int a = 1; int b = 2; // Act int result = calculator.Add(a, b); // Assert Assert.AreEqual(3, result); } } Test boundary conditions  Make sure to test boundary conditions, such as nu

What to choose between .NET Core and .NET?

 .NET Framework is a better choice if you: you don't have enough time time to learn new technology. Need a stable environment to work in. Have nearer release schedules. you are already working on an existing app and extending its functionality. If already have an existing team with .NET expertise and building production-ready software. Do not want to deal with continuous upgrades and changes. Building Windows client applications using Windows Forms or WPF .NET Core is a better choice if you: IF you wanted to deploy your app on Windows, Linux, and Mac operating systems. Are not afraid of learning new things. Are not afraid of breaking and fixing things since .NET Core is not fully matured yet. When you are learning .NET. work on open source.

How To Deploy .net Core Application On Linux

Here, I can explain steps to deploy .net core application on linux machine. Step 1 - Publish your .net Core application: First, create a .net core application on VS; you can make an MVC project or Web API project and if you already have an existing project, then open it. Right Click on your project Click on publish Now create a new publish profile, and browse the folder where you want to publish your project dll Click on publish so it will create your dll in the folder Step 2 - Install required .net Module on Linux: Now we have our web application dll and now we need to host it on the Linux environment. First, we need to understand how the deployment works in Linux. .Net applications run on Kestrel servers and we run Apache or Nginx server in Linux environments, which acts as a proxy server and handles the traffic from outside the machine and redirects it to the Kestrel server so we will have Apache or Nginx server as the middle layer. In this article, we will use Apache as a proxy ser

Avoiding Cross-Site Scripting (XSS) attacks in C# and .NET Core

In the real world of web development, security is paramount. Cross-site scripting (XSS) remains a prevalent threat, capable of compromising the integrity and confidentiality of web applications. For developers working with C# and .NET Core, fortifying against XSS vulnerabilities is imperative. In this article, we'll delve into practical techniques and examples to safeguard your web applications against XSS attacks. Understanding Cross-Site Scripting (XSS) XSS occurs when attackers inject malicious scripts into web pages viewed by other users. These scripts exploit vulnerabilities in the application's handling of user inputs, leading to unauthorized access, data theft, or manipulation. Understanding the types of XSS (e.g., reflected, stored, DOM-based) is crucial for devising effective defence strategies. Example Scenario Consider a simple web application—a comment section where users can post messages. Without proper validation and sanitization, this application is susceptible

Extracting Values from PDFs in .NET Core 8 without ASP.NET

Extracting data from PDF files is a common necessity for various tasks such as data analysis, content indexing, and information retrieval. While ASP.NET Core 8 offers robust tools for PDF manipulation, there are instances where developers may prefer alternatives for flexibility or specific project requirements. In this article, we'll explore how to extract values from PDF files within the .NET Core 8 ecosystem without relying on ASP.NET, using the PdfSharpCore library. We'll provide a step-by-step guide along with examples in C# to demonstrate how to accomplish this task effectively. Understanding PdfSharpCore: PdfSharpCore is a popular .NET library for PDF document manipulation. It provides functionalities to create, modify, and extract content from PDF files. In this guide, we'll focus on utilizing PdfSharpCore to extract text from PDF documents. Installing PdfSharpCore: Before we can start using PdfSharpCore in our .NET Core application, we need to install the PdfSharpCo

Real-Time Data Transfer with Web Sockets and SignalR in .NET Core

 In dynamic web applications, real-time data transfer has become a crucial aspect of providing a responsive and interactive user experience. Web Socket technology, along with frameworks like SignalR in .NET Core, enables bidirectional communication between clients and servers, allowing seamless data exchange. In this article, we'll explore how to implement Web Socket functionality using SignalR in a .NET Core application to retrieve data from a concurrent dictionary and push it to clients in real time. Understanding Web Sockets and SignalR Web Socket is a communication protocol that provides full-duplex communication channels over a single TCP connection, allowing for low-latency, bidirectional communication between clients and servers. It enables real-time data transfer without the overhead of HTTP polling. SignalR is a high-level library built on top of Web Socket that simplifies real-time web functionality implementation in .NET applications. It abstracts away the complexities o

How to Manage State in .NET Core

 In any application development, managing state effectively is crucial for maintaining data consistency and providing a seamless user experience. In .NET Core, various techniques and libraries are available for managing state, each catering to different scenarios and requirements. This article explores different state management techniques in .NET Core and provides code examples to illustrate their usage. Built-in State Management Techniques Session State : Session state allows storing user-specific data across multiple requests. It's useful for maintaining user authentication, shopping cart items, etc. Session state can be managed using ASP.NET Core's HttpContext.Session property. // Setting session value HttpContext.Session.SetString("UserName", "JohnDoe"); // Retrieving session value var userName = HttpContext.Session.GetString("UserName"); View State: View state stores page-specific data that needs to be persisted across postbacks. It'

Journey from ASP.NET Framework to .NET Core

Initial development with ASP.NET framework Initially, projects were developed using ASP.NET Framework, which provided various options for user interfaces. Windows forms (WinForms):  For desktop applications with rich UI controls. Windows presentation foundation (WPF):  For desktop applications with advanced graphics and multimedia support. ASP.NET web forms:  For web applications with a component-based UI model and event-driven programming. Transition to ASP.NET MVC As web development evolved, ASP.NET MVC (Model-View-Controller) emerged as a popular framework for building web applications using the ASP.NET Framework. ASP.NET MVC introduced a more structured approach to web development, separating concerns into models, views, and controllers. ASP.NET MVC allowed developers to create web applications with cleaner code architecture, better testability, and improved control over HTML markup. Introduction of .NET Core With the advent of .NET Core, Microsoft introduced ASP.NET Core, a cross-