Exercise: Async Chat Logger
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 theCodingExercisenamespace.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
ChatLoggerutility class must explicitly implementIAsyncDisposable.You must define the
DisposeAsyncmethod, which returns aValueTask.Use
await Task.Delay(1000)to simulate the network delay.In
Program.cs, use theawait using varsyntax to instantiate theChatLoggerand 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
IAsyncDisposablerequires exactly one method:public async ValueTask DisposeAsync().Because
DisposeAsyncinvolves anawaitcall (theTask.Delay), ensure the method signature includes theasynckeyword.When instantiating an object that implements
IAsyncDisposablein yourProgram.cs, you must combine theawaitkeyword and theusingkeyword before the variable declaration.
Exercise: Async Chat Logger
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 theCodingExercisenamespace.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
ChatLoggerutility class must explicitly implementIAsyncDisposable.You must define the
DisposeAsyncmethod, which returns aValueTask.Use
await Task.Delay(1000)to simulate the network delay.In
Program.cs, use theawait using varsyntax to instantiate theChatLoggerand 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
IAsyncDisposablerequires exactly one method:public async ValueTask DisposeAsync().Because
DisposeAsyncinvolves anawaitcall (theTask.Delay), ensure the method signature includes theasynckeyword.When instantiating an object that implements
IAsyncDisposablein yourProgram.cs, you must combine theawaitkeyword and theusingkeyword before the variable declaration.