161
CHAPTER 5 | Designing and Developing Multi-Container and Microservice-Based .NET Applications
try
{
// Signal cancellation to the executing method
_stoppingCts.
Cancel
();
}
finally
{
// Wait until the task completes or the stop token triggers
await Task.
WhenAny
(_executingTask, Task.
Delay
(Timeout.
Infinite
,
cancellationToken));
}
}
public
virtual
void
Dispose
()
{
_stoppingCts.
Cancel
();
}
}
When deriving from the previous
abstract base class, thanks to that inherited implementation, you just
need to implement the
ExecuteAsync()
method in your own custom hosted service class, as in the
following simplified code from eShopOnContainers which is polling a
database and publishing
integration events into the Event Bus when needed.
Dostları ilə paylaş: