Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/samhammerag/samhammer.timedhostedservice
hostedservice that can run periodically
https://github.com/samhammerag/samhammer.timedhostedservice
net-core-project-lib
Last synced: 23 days ago
JSON representation
hostedservice that can run periodically
- Host: GitHub
- URL: https://github.com/samhammerag/samhammer.timedhostedservice
- Owner: SamhammerAG
- License: mit
- Created: 2020-04-22T11:08:13.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-03-08T10:22:25.000Z (10 months ago)
- Last Synced: 2024-11-05T03:09:36.463Z (about 2 months ago)
- Topics: net-core-project-lib
- Language: C#
- Homepage:
- Size: 29.3 KB
- Stars: 0
- Watchers: 5
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Samhammer.TimedHostedService
This package provides a hosted service that can run periodically.
The concept is from this documentation:
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-3.1&tabs=visual-studio#### How to add this to your project:
- reference this nuget package: https://www.nuget.org/packages/Samhammer.TimedHostedService/#### How to use:
Implement a class that inherits TimedHostedService.
```csharp
public class MyHostedService : TimedHostedService
{
protected override TimeSpan StartDelay => TimeSpan.FromSeconds(3);protected override TimeSpan ExecutionInterval => TimeSpan.FromDays(1);
public MyHostedService(ILogger logger, IServiceScopeFactory services)
: base(logger, services)
{
}
protected override Task RunScoped(IMyService myService)
{
myService.DoSomething();
return Task.CompletedTask;
}
}
```Register the hosted service in startup:
```csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddHostedService()
}
```Possible configurations:
- ExecutionInterval: How often the job ticks after starting (default: 60 seconds)
- StartDelay: Delay the first tick for a specific time after starting (default: 0)
- OnlySingleInstance: If the last tick is still no other tick is started even if the executionInterval is over (default: false)Possible hook points:
- RunScoped: Execute your logic here (mandatory)
- OnStartup: Is called before the timer starts ticking (optional)
- OnRunSuccessful: Is executed after the current tick is over (optional)
- OnError: Can be used to handle errors that occur within a running tickNote:
- Errors inside "ExecutionInterval" are catched and can be handled with "OnError"
- "RunScoped" is running in it's ioc scope. The other hook points are outside of this scope.## Contribute
#### How to publish a nuget package
- Create a tag and let the github action do the publishing for you