45  
aspnet
Поиск  
Always will be ready notify the world about expectations as easy as possible: job change page
Nov 22, 2021

How to use IDisposable in ASP.NET Core

How to use IDisposable in ASP.NET Core
Автор:
MBARK T3STO
Источник:
Просмотров:
2039

Dispose and Finalize are two methods you often use to release resources occupied by your .NET and .NET Core applications running in the context of the CLR. Most importantly, if you have unmanaged resources in your application, you should release the resources occupied by such resources explicitly.

Due to the non-deterministic nature of finalization and because finalizers are costly in terms of performance, the Dispose method is used much more frequently than a finalizer. Additionally, you can use the Dispose method on a type that implements the IDisposable interface.

This article talks about the many ways you can dispose of objects that implement the IDisposable interface in ASP.NET Core 6.

To work with the code examples provided in this article, you should have Visual Studio 2022 installed in your system. If you don’t already have a copy, you can download Visual Studio 2022 here.

Create an ASP.NET Core Web API project in Visual Studio 2022

First off, let’s create an ASP.NET Core project in Visual Studio 2022. Following these steps will create a new ASP.NET Core Web API 6 project in Visual Studio 2022:

  1. Launch the Visual Studio 2022 IDE.
  2. Click on “Create new project.”
  3. In the “Create new project” window, select “ASP.NET Core Web API” from the list of templates displayed.
  4. Click Next.
  5. In the “Configure your new project” window, specify the name and location for the new project.
  6. Optionally check the “Place solution and project in the same directory” check box, depending on your preferences.
  7. Click Next.
  8. In the “Additional Information” window shown next, select .NET 6.0 (Preview) as the target framework from the drop-down list at the top. Leave the “Authentication Type” as “None” (default). Ensure that the option “Use controllers …” is checked.
  9. Ensure that the check boxes “Enable Docker,” “Configure for HTTPS,” and “Enable Open API Support” are unchecked as we won’t be using any of those features here.
  10. Click Create.

This will create a new ASP.NET Core 6 Web API project in Visual Studio 2022. We’ll use this project to work with objects that implement the IDisposable interface in the subsequent sections of this article.

Create a class that implements the IDisposable interface

We’ll now create a class that implements the IDisposable interface as shown in the code snippet given below.

public class FileManager : IDisposable
{
    FileStream fileStream = new FileStream(@"C:\Test.txt", FileMode.Append);
    public async Task Write(string text)
    {
        byte[] buffer = Encoding.Unicode.GetBytes(text);
        int offset = 0;
        try
        {
            await fileStream.WriteAsync(buffer, offset, buffer.Length);
        }
        catch
        {
            // Write code here to handle exceptions.
        }
    }
   
    public void Dispose()
    {
        if (fileStream != null)
        {
            fileStream.Dispose();
        }
    }
}

The FileManager class implements the IDisposable interface and contains two methods – Write and Dispose. While the former is used to write text to a file asynchronously, the latter is used to remove the FileStream instance from memory by calling the Dispose method of the FileStream class.

Disposing IDisposable objects in ASP.NET Core 6

In this section we’ll examine various ways in which we can dispose IDisposable objects in ASP.NET Core 6.

Dispose IDisposable objects using the ‘using’ statement

The simplest way to dispose an IDisposable instance is by using the “using” statement, which calls the Dispose method on the instance automatically. The following code snippet illustrates this.

using (FileManager fileManager = new FileManager())
{
    await fileManager.Write("This is a text");
}

Dispose IDisposable objects at the end of a request

When working in ASP.NET Core or ASP.NET Core MVC applications, you might often need to dispose objects at the end of an HTTP request. The HttpResponse.RegisterForDispose method can be used to register IDisposable objects for disposal in this way. It accepts an instance of a class that implements the IDisposable interface and makes sure that the IDisposable object passed to it as a parameter is disposed automatically with each request.

The following code snippet illustrates how you can use the HttpResponse.RegisterForDispose method to register an instance of the FileManager class at the end of each HTTP request.

public class DefaultController : ControllerBase
{
    readonly IDisposable _disposable;
    public DefaultController()
    {
        _disposable = new FileManager();
    }
}

Dispose IDisposable objects using the built-in IoC container

Another approach to dispose IDisposable objects automatically is by using the built-in IoC (inversion of control) container in ASP.NET Core. You can take advantage of either Transient, Scoped, or Singleton instances to created services and add them to the built-in IoC container.

Add IDisposable objects to the IoC container in the ConfigureServices method of the Startup class so that those objects are disposed automatically with each HTTP request.

Dispose IDependency objects using IHostApplicationLifetime events

ASP.NET Core has an interface called IHostApplicationLifetime that allows you to run custom code when the application is started or shut down. You can take advantage of the Register method of this interface to register to events.

The Configure method of the Startup class can accept the following parameters:

  • IApplicationBuilder
  • IHostingEnvironment
  • ILoggerFactory
  • IHostApplicationLifetime

The following code snippet shows how you can use the IHostApplicationLifetime interface to register objects for disposal when the application shuts down.

public void Configure(IApplicationBuilder app, IHostApplicationLifetime hostApplicationLifetime)
{
    hostApplicationLifetime.ApplicationStopping.Register(OnShutdown);
}

private void OnShutdown()
{
    // Write your code here to dispose objects
}

Finally, note that Startup.cs is not created by default in ASP.NET Core 6. You’ll need to create one manually and then write the following code in the Program.cs file to specify the Startup class you’ll be using in the application.

 

var builder = WebApplication.CreateBuilder(args);
builder.Host.ConfigureWebHostDefaults(webBuilder =>
{
    webBuilder.UseStartup<Startup>();
});
using var app = builder.Build();
app.Run();

 

Unlike with Finalize, we use the Dispose method explicitly to free unmanaged resources. You should call the Dispose method explicitly on any object that implements it to free any unmanaged resources for which the object may be holding references.

In this article we’ve examined four ways of disposing such IDisposable objects. We can dispose of IDisposable objects by using the “using” statement, by disposing automatically at the end of each request, by using the IoC container, and by taking advantage of IHostApplicationLifetime events.

Похожее
Sep 7, 2020
Microservices are the face of the future. Organizations are keen to adapt to Microservices, either by creating a new setup or by transforming monolithic applications into Microservices. Though the inclination towards migrating to Microservices quite high, how to approach the...
Apr 16, 2022
Author: Matthew MacDonald
Sometimes it’s the little things that count. When .NET 6 dropped this November, a few top-line items grabbed all the attention. I’m talking about C# 10, hot reload, Blazor’s AOT compilation, and some serious inner loop performance improvements. All of...
Aug 25, 2020
In my 2019 A-Z series, I covered Blazor for ASP.NET Core while it was still experimental. As of ASP.NET Core 3.1, server-side Blazor has now been released, while client-side Blazor (currently in preview) is expected to arrive in May 2020....
Oct 12, 2020
Author: Marco De Sanctis
In a previous article we’ve introduced how authentication works in Blazor WebAssembly and we’ve seen a simple example on how to create a Blazor client, implement the login flow via Identity Server 4 and retrieve an access token to call...
Написать сообщение
Тип
Почта
Имя
*Сообщение