.NET
...see more

After upgrade to .NET 6.0 and trying to get deploy to Azure App Service the error 'Error: EISDIR: illegal operation on a directory, open '/home/site/wwwroot/wwwroot/Identity/lib/bootstrap/LICENSE' occured.

Solution

Previously the path to licence file was: /home/site/wwwroot/wwwroot/Identity/lib/bootstrap/LICENSE/LICENSE new file was without the LICENSE dir. So when the new version of app arrived it tried to update the LICENSE file, which was actually a directory. Removing LICENSE/LICENSE helped.

Found the solution at Stack Overflow

...see more

To get the DataGridViewRow index use the following snippet

string searchValue = "searchterm";
int rowIndex = -1;
foreach(DataGridViewRow row in dataGridView.Rows)
{
    if(row.Cells[1].Value.ToString().Equals(searchValue))
    {
        rowIndex = row.Index;
        break;
    }
}

or use a Linq query

int rowIndex = -1;

DataGridViewRow row = dataGridView.Rows
    .Cast<DataGridViewRow>()
    .Where(r => r.Cells["CellName"].Value.ToString().Equals(searchValue))
    .First();

rowIndex = row.Index;
...see more

To change the color of a cell use the following snippet

dataGridView1.Rows[rowIndex].Cells[columnIndex].Style.BackColor = Color.Green;
...see more

The following code adds rows and columns to a DataGridView. 

dataGridView1.Columns[0].Name = "Name";  
dataGridView1.Columns[2].Name = "City";  
   
dataGridView1.Rows.Add("Kate", "New York");  
dataGridView1.Rows.Add("John", "Seattle");  
...see more

In .NET, when you assign one list (list2) to another list (list1) directly, both lists will reference the same memory location. As a result, any modifications made to list2 will also affect list1. To create a separate copy of the list, you should use the List<T> constructor to initialize a new list based on the elements of the original list (list1).

...see more

To illustrate the issue the following code assigns list1 directly to list2.

List<string> list1 = new List<string>();
List<string> list2 = list1;

list2.Add("Item A");

Console.WriteLine("List1 elements:");
list1.ForEach(item => Console.WriteLine(item));

This will output the list1 elements and show 'Item A'.

List1 elements:
Item A

As you can see, modifying list2 also modified list1.

Explanation of direct assignment issue

When you assign one list to another using list2 = list1, you're not creating a new list. Instead, both list1 and list2 will point to the same list in memory. Any changes made to one list will be reflected in the other because they are essentially the same list.

...see more

The following code shows modifying of list2 does not affect list1 because list2 is a separate copy of list1.

List<string> list1 = new List<string>();
List<string> list2 = new List<string>(list1);

list2.Add("Item A");

Console.WriteLine("List1 elements:");
list1.ForEach(item => Console.WriteLine(item));

This will output list1 without any item.

List1 elements:

Explanation of copying the List

You can use the List<T> constructor with the original list as an argument to create a new list that is a separate copy of the original list. This constructor creates a new list with the same elements as the original list.

...see more

Automated testing in .NET is a crucial software development practice that involves using specialized tools and frameworks to create and execute tests automatically. It verifies the correctness of code, detects bugs early in the development process, and ensures that changes do not introduce new issues. .NET offers robust testing libraries like MSTest, NUnit, and xUnit, along with tools like Visual Studio Test Explorer, to simplify the creation and execution of unit, integration, and UI tests, enhancing the reliability and maintainability of .NET applications.

...see more

In the .Net Framework, the approach to unit testing internal methods or types was to add an InternalsVisibleTo attribute into the AssemblyInfo.cs file. For example, look at the following line of code below:

InternalsVisibleTo Attribute

[assembly: InternalsVisibleTo("Projects.Tests")]

This all works fine in the .net Framework. However, in .Net Core, most of the settings in the AssemblyInfo file have been moved to the project file. So, to test internal methods, you have two options.

Project File Change

Assuming we have a project called Projects and a unit test project called Projects.Tests. The Projects project file, which contains the internal method, can be modified in Visual Studio with the following:

AssemblyAttribute

<ItemGroup>
    <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
        <_Parameter1>Projects.Tests</_Parameter1>
    </AssemblyAttribute>
</ItemGroup>

Starting with .NET 5, you can use the <InternalsVisibleTo> without adding any NuGet package:

<ItemGroup>
    <InternalsVisibleTo Include="Projects.Tests" />
</ItemGroup>

Source File change

The alternative way is to use an attribute in the source file containing the internal method. For example, see the code below:

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Projects.Test")]

namespace Projects
{
    public class User
    {
        internal string Hello(string username)
        {
            return $"Hello {username}";
        }
    }
}

Additional Resources:

...see more

.NET releases a new major version every November. Even-numbered releases, such as .NET 6 or .NET 8, are long-term supported (LTS). LTS releases get free support and patches for three years. Odd-numbered releases are standard-term support. Standard-term support releases get free support and patches for 18 months.

...see more

This is a unit test method written in C# using the xunit framework. It is testing the behavior of a method named GetFolderAsync from a class called FileSystem.

[Fact( DisplayName = "get folder async should throw exception for non-existent folder" )]
public async Task GetFolderAsyncShouldThrowExceptionForNonExistentFolder()
{
    var fileSystem = new FileSystem();
    var ex = await Assert.ThrowsAsync<DirectoryNotFoundException>( () => fileSystem.GetFolderAsync( @"C:\blah" ) );
}

This xUnit test method is named "GetFolderAsyncShouldThrowExceptionForNonExistentFolder." It is an asynchronous test that checks whether calling the GetFolderAsync method on a fileSystem object with the path @"C:\blah" results in a DirectoryNotFoundException being thrown. The [Fact] attribute marks it as a fact test case, and the DisplayName attribute provides a custom name for the test. This test aims to ensure that the GetFolderAsync method correctly throws an exception when trying to access a non-existent folder.

...see more

Use C# reflection to get all methods with a custom attribute.

Method[] methodInfos = Assembly.GetCallingAssembly()
   .GetTypes()
   .SelectMany(t => t.GetMethods())
   .Where(m => Attribute.IsDefined(m, typeof(CustomAttribute)))
   .ToArray();

Additional Resources:

...see more

.NET APIs include classes, interfaces, delegates, and value types that expedite and optimize the development process and provide access to system functionality.

.NET types use a dot syntax naming scheme that connotes a hierarchy. This technique groups related types into namespaces, which can be searched and referenced more easily. The first part of the full name—up to the rightmost dot—is the namespace name.

The System namespace is the root namespace for fundamental types in .NET. This namespace includes classes representing the base data types used by all applications.

.NET includes a set of data structures that are the workhorses of many .NET apps. These are mostly collections, but also include other types.

.NET includes a set of utility APIs that provide functionality for many important tasks.

There are many app models that can be used with .NET.

Further reading at .NET class library overview - .NET | Microsoft Learn.

...see more

URI stands for Uniform Resource Identifier. A URI is a string of characters that identifies a particular resource. Resources can be anything with an identity, such as a document, image, service, or concept. URIs are used to uniquely identify and locate resources on the internet or within a network.

...see more

URIs are categorized into two main types: URLs (Uniform Resource Locators) and URNs (Uniform Resource Names).

URL (Uniform Resource Locator)

  • A URL is a type of URI that specifies the location of a resource on the internet or a network.
  • It typically consists of several components, including the scheme (e.g., "http" or "https"), the host (domain name or IP address), and the path to the resource.
  • Example: https://www.example.com/path/to/resource

URN (Uniform Resource Name)

  • A URN is a type of URI used to identify a resource by name in a particular namespace uniquely.
  • Unlike URLs, URNs are meant to identify resources even if their location changes persistently.
  • Example: urn:isbn:0451450523
...see more

In .NET, the System.Uri class is used to represent URIs. You can create a Uri object by passing a URI string to its constructor. The Uri class provides various properties and methods for working with different components of the URI, such as the scheme, host, path, query, and fragment.

Here's a simple example in C#:

// Creating a Uri object
Uri uri = new Uri("https://www.example.com/path/to/resource");

// Accessing components of the URI
Console.WriteLine($"Scheme: {uri.Scheme}");
Console.WriteLine($"Host: {uri.Host}");
Console.WriteLine($"Path: {uri.AbsolutePath}");
Console.WriteLine($"Query: {uri.Query}");

This example demonstrates creating a Uri object from a URL string and accessing different components of the URI using properties like Scheme, Host, AbsolutePath, and Query.

...see more

OriginalString and AbsoluteUri have different behaviors.

AbsoluteUri does escaping

Uri uri = new Uri("http://www.example.com/test.aspx?v=hello world");

Console.WriteLine(uri.OriginalString);
// http://www.example.com/test.aspx?v=hello world

Console.WriteLine(uri.AbsoluteUri);
// http://www.example.com/test.aspx?v=hello%20world  <--  different

AbsoluteUri doesn't support relative URIs

var uri = new Uri("/test.aspx?v=hello world", UriKind.Relative);

Console.WriteLine(uri.OriginalString);
// /test.aspx?v=hello world

Console.WriteLine(uri.AbsoluteUri);
// InvalidOperationException: This operation is not supported for a relative URI.
...see more

Serialization is the process of converting the state of an object into a form that can be persisted or transported. The complement of serialization is deserialization, which converts a stream into an object. Together, these processes allow data to be stored and transferred.

...see more

In C#, you can replace multiple white spaces with a single white space using regular expressions. You can use the Regex class from the System.Text.RegularExpressions namespace to achieve this. Here's an example:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string inputString = "This   is   a    sample    string   with   multiple   spaces.";

        // Use regular expression to replace multiple white spaces with a single white space
        string result = Regex.Replace(inputString, @"\s+", " ");

        Console.WriteLine("Original string: " + inputString);
        Console.WriteLine("Modified string: " + result);
    }
}

In this example, the \s+ regular expression pattern matches one or more white spaces, and the Regex.Replace method replaces these occurrences with a single white space.

Additional Resources

...see more

The Task asynchronous programming model (TAP) provides an abstraction over asynchronous code. You write code as a sequence of statements, just like always. You can read that code as though each statement completes before the next begins. The compiler performs many transformations because some of those statements may start work and return a Task representing the ongoing work.

...see more

In C#, Task.Delay and Thread.Sleep are both used to introduce delays or pauses in the execution of your code, but they have different use cases and implications.

In summary, use Task.Delay when working with asynchronous code and you want to avoid blocking the current thread. Use Thread.Sleep when you explicitly want to block the current thread, but be cautious about using it in scenarios where responsiveness is important, such as in GUI applications. In modern C# applications, with the widespread use of async/await, Task.Delay is often the more appropriate choice.

Further Resources:

...see more

The Task class is a key component of the Task Parallel Library, providing a framework for asynchronous programming. It represents a unit of work that can run concurrently with other tasks, offering efficient and scalable execution. Programmers use it to create, manage, and coordinate asynchronous operations, enhancing application responsiveness.

Namespace: System.Threading.Tasks

...see more

The Task.Delay Method creates a task that will complete after a time delay.

  • Task.Delay is generally preferred when working with asynchronous programming using async and await.
  • It returns a Task that represents a time delay.
  • It doesn't block the calling thread. Instead, it allows the thread to be released and continue with other work while waiting for the specified time to elapse.
  • It's more suitable for scenarios where you want to introduce a delay without blocking the execution of the current method or freezing the user interface in GUI applications.

Example:

async Task MyMethod()
{
    // Do something before the delay
    await Task.Delay(1000); // Delay for 1000 milliseconds (1 second)
    // Do something after the delay
}
...see more

The Thread class allows programmers to create and manage multithreaded applications. It enables concurrent code execution, allowing tasks to run independently for improved performance. Developers can utilize methods like Start, Join, and Sleep to control thread execution, facilitating efficient parallel processing in .NET applications.

Namespace: System.Threading

...see more

The Thread.Sleep Method Suspends the current thread for the specified amount of time.

  • Thread.Sleep is a synchronous method that blocks the current thread for the specified amount of time.
  • It's generally used in non-async scenarios or when dealing with multi-threading where you explicitly want to pause the execution of the current thread.
  • It can introduce responsiveness issues, especially in GUI applications, as it will freeze the UI during the sleep period.

Example:

void MyMethod()
{
    // Do something before the delay
    Thread.Sleep(1000); // Sleep for 1000 milliseconds (1 second)
    // Do something after the delay
}
...see more

Basic commands

  • dotnet run command provides a convenient option to run your application from the source code with one command. It's useful for fast iterative development from the command line

Resources

...see more

This code snippet demonstrates configuring and retrieving custom settings from appsettings.json. It defines a ClientSettings class to manage client-specific configurations such as name and URL. The appsettings.json file is structured to hold these settings under a "Clients" section. The code includes validation checks to ensure that the required settings are provided and the URL is valid.

// Configuration in appsettings.json
{
   "Clients": {
      "Client": {
         "Name": "Acme Corporation",
         "Url": "https://acme.example.com"
      }
   }
}

// Settings class
internal class ClientSettings
{
   public const string ConfigSection = "Clients.Client";
   public string ClientName { get; set; } = "DefaultClientName";
   public string ClientUrl { get; set; } = string.Empty;

   public static ClientSettings Load(IConfiguration configuration)
   {
      ClientSettings settings = configuration.GetSection(ConfigSection).Get<ClientSettings>() ?? throw new ConfigurationErrorsException($"'{ConfigSection}' section not found. Add configuration to appsettings.json");
      if (string.IsNullOrWhiteSpace(settings.ClientName)) throw new ConfigurationErrorsException("ClientName is null or empty");
      if (string.IsNullOrWhiteSpace(settings.ClientUrl)) throw new ConfigurationErrorsException("ClientUrl is null or empty");

      if (!(Uri.TryCreate(settings.ClientUrl, UriKind.Absolute, out Uri? uri) && (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps)))
      {
         throw new ConfigurationErrorsException("ClientUrl is not a valid URL");
      }
      return settings;
   }
}

// Using setting
public Client(IConfiguration configuration)
{
   _clientSettings = ClientSettings.Load(configuration);
}

Links and Explanation:

...see more

In a console apps, there is often a need to obtain user input from the console while ensuring that the input is not empty or only whitespace characters.

In this sample, we define a method GetUserInput that takes an optional message parameter. It continuously prompts the user until a non-empty, non-whitespace input is provided.

static string GetUserInput(string message = "Please enter some input:")
{
    string input;
    do
    {
        Console.WriteLine(message);
        input = Console.ReadLine()?.Trim();
    } while (string.IsNullOrWhiteSpace(input));
    return input;
}

Explanation:

  • message parameter allows customizing input prompt message.
  • Console.ReadLine()?.Trim() reads user input and trims leading/trailing whitespace.
  • The ?. operator is used for null-conditional access, ensuring that Console.ReadLine() doesn't throw a null reference exception if the input is null.
  • do-while loop ensures user input is not empty or whitespace.
...see more

Logging is an essential part of application development for debugging, monitoring, and understanding the flow of execution, especially in complex systems. When logging in a C# method with parameters that need validation, it's crucial to follow best practices to ensure clear and useful log messages. Below is a sample demonstrating how to log and validate parameters in a C# method:

public bool ValidateAndProcessData(string data)
{
    // Log the start of the method
    _logger.LogInformation("ValidateAndProcessData method started");

    // Validate input data
    if (string.IsNullOrEmpty(data))
    {
        _logger.LogError("Input data is null or empty");
        throw new ArgumentException("Input data cannot be null or empty", nameof(data));
    }

    try
    {
        // Process data
        _logger.LogInformation("Processing data: {data}", data);

        // Simulating processing time
        System.Threading.Thread.Sleep(1000);

        _logger.LogInformation("Data processed successfully");
        return true;
    }
    catch (Exception ex)
    {
        // Log any exceptions that occur during processing
        _logger.LogError(ex, "Error processing data: {data}", data);
        throw; // Re-throw the exception for higher-level handling
    }
    finally
    {
        // Log the end of the method
        _logger.LogInformation("ValidateAndProcessData method completed");
    }
}

By following this sample, you ensure that your method logs relevant information about parameter validation and method execution, making it easier to debug and monitor your application's behavior.

Add to Set
  • .NET
  • Agile
  • AI
  • ASP.NET Core
  • Azure
  • C#
  • Cloud Computing
  • CSS
  • EF Core
  • HTML
  • JavaScript
  • Microsoft Entra
  • PowerShell
  • Quotes
  • React
  • Security
  • Software Development
  • SQL References
  • Technologies
  • Testing
  • Visual Studio
  • Windows
 
Sets