Categories
.NET C#

async/await: Why don’t use Task.Wait()?

In short explained why you should never use Task.Wait() to wait for an asynchronous task to complete from a synchronous context.

The Wait() method always returns an AggregateException if an error occurs within the task execution.

It would be hard to handle the real exception which is wrapped within the AggregateException.

Use GetAwaiter().GetResult() instead. It will return the real exception.

Categories
.NET C#

Built-in .NET-function to generate passwords

Recently I’ve found a method GeneratePassword() in the good old .NET full framework, in the System.Web.Security-namespace. It has two parameters to specify the complexity of the password.

public static string GeneratePassword (int length, int numberOfNonAlphanumericCharacters);

Categories
.NET Allgemein ASP.NET C# Web

Challenge authentication using a custom middleware.

Sometimes you write an ASP.NET Core application without using MVC. For example an microservice or an application serving static files.

Anyhow you might want to secure your application using authentication. I was facing the problem, how I could challenge authentication when I’m not using MVC.

The solution is writing a custom middleware that challenges authentication.

Categories
.NET C#

Lazy Loading with the Lazy class

Did you know the Lazy class in the System namespace?

This little useful helper was introduced with .NET 4. You can use it to lazy initialize properties.

The ResolvePackagePath()-method gets called if the value is needed the first time.
The property IsValueCreated lets you check if the value is already initialized.

Categories
.NET C#

Sending all e-mails in development environment to a local folder

In the app.config/web.config you can use the configuration section <system.net> <mailSettings> <smtp> to configure how to deliver e-mails which are sent using the MailMessage and SmtpClient classes in your program.

In development environment you usually don’t want the e-mails to be delivered. Today I’ve found a awesome configuration option, which enables that all outgoing e-mails go to an local folder or file share as eml files.

app.config / web.config

Sending an e-mail with .NET

Local pickup folder

Categories
.NET C#

How to get a temporary file on the fly

Did you already know the methods Path.GetTempPath() and Path.GetTempFileName() in the System.IO namespace.

The very useful method GetTempFileName() creates a temporary file for you in a location you have write access. You then can write temporary data to this file.

Categories
.NET C# SharePoint

How to get the Correlation ID of the current request in SharePoint

SharePoint generates for each request a random GUID which correlates all log messages in the ULS log which are printed during the current request. The Correlation ID is also printed to error pages. If you want more information about an error page you can use the Correlation ID to find the right log messages in the ULS log.

Recently I wanted to get the current request’s Correlation ID to share it with an external webservice to have the same correlation ID in all depending systems.

You can add following code to your ASP page to get the Correlation ID.

 

Categories
.NET C#

Creating thumbnail images with C#

Today, I’ve been searching for a .NET solution for resizing images to thumbnail sizes. I’ve found a solution using the System.Drawing classes.

You can use this method for creating thumbnails:

ImageManipulation.SaveThumbnail(srcImageFilePath, thumbnailOutFilePath, maxWidth, maxHeight);