Exercise: Async Chat Logger

Implement asynchronous resource disposal in a utility logger class to safely close a simulated social media chat connection.

Problem statement

A social media backend server handles thousands of live chat connections. When a user disconnects, the server must cleanly close the connection and write a final log entry. Because network operations take time, this cleanup process must be handled asynchronously within a dedicated logger class so it doesn’t block the main server threads.

Task requirements

  • In ChatLogger.cs, implement the appropriate asynchronous disposal interface on the class inside the CodingExercise namespace.

  • Inside the cleanup method, print “Closing chat connection asynchronously...”.

  • Simulate a network disconnect by waiting for 1 second.

  • Print “Chat connection closed safely.” after the delay.

  • In Program.cs, instantiate the logger safely and print “User is typing a message...” while it is active.

Constraints

  • The ChatLogger utility class must explicitly implement IAsyncDisposable.

  • You must define the DisposeAsync method, which returns a ValueTask.

  • Use await Task.Delay(1000) to simulate the network delay.

  • In Program.cs, use the await using var syntax to instantiate the ChatLogger and automatically trigger the asynchronous cleanup at the end of the script.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • IAsyncDisposable requires exactly one method: public async ValueTask DisposeAsync().

  • Because DisposeAsync involves an await call (the Task.Delay), ensure the method signature includes the async keyword.

  • When instantiating an object that implements IAsyncDisposable in your Program.cs, you must combine the await keyword and the using keyword before the variable declaration.

Exercise: Async Chat Logger

Implement asynchronous resource disposal in a utility logger class to safely close a simulated social media chat connection.

Problem statement

A social media backend server handles thousands of live chat connections. When a user disconnects, the server must cleanly close the connection and write a final log entry. Because network operations take time, this cleanup process must be handled asynchronously within a dedicated logger class so it doesn’t block the main server threads.

Task requirements

  • In ChatLogger.cs, implement the appropriate asynchronous disposal interface on the class inside the CodingExercise namespace.

  • Inside the cleanup method, print “Closing chat connection asynchronously...”.

  • Simulate a network disconnect by waiting for 1 second.

  • Print “Chat connection closed safely.” after the delay.

  • In Program.cs, instantiate the logger safely and print “User is typing a message...” while it is active.

Constraints

  • The ChatLogger utility class must explicitly implement IAsyncDisposable.

  • You must define the DisposeAsync method, which returns a ValueTask.

  • Use await Task.Delay(1000) to simulate the network delay.

  • In Program.cs, use the await using var syntax to instantiate the ChatLogger and automatically trigger the asynchronous cleanup at the end of the script.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • IAsyncDisposable requires exactly one method: public async ValueTask DisposeAsync().

  • Because DisposeAsync involves an await call (the Task.Delay), ensure the method signature includes the async keyword.

  • When instantiating an object that implements IAsyncDisposable in your Program.cs, you must combine the await keyword and the using keyword before the variable declaration.

C# 14.0
using System.Threading.Tasks;
using CodingExercise;
// Instantiate the logger safely and simulate chat activity