Skip to content

Create a Job

Create a class that implements IJob. The Run method will be called by the job manager when conditions are met.

public class MyJob : IJob
{
readonly ILogger<MyJob> logger;
readonly IConnectivity connectivity;
public MyJob(ILogger<MyJob> logger, IConnectivity connectivity)
{
// Full dependency injection support
this.logger = logger;
this.connectivity = connectivity;
}
public async Task Run(JobInfo jobInfo, CancellationToken cancelToken)
{
this.logger.LogInformation("Job {Identifier} running", jobInfo.Identifier);
// Access job parameters
if (jobInfo.Parameters?.TryGetValue("key", out var value) == true)
{
this.logger.LogInformation("Parameter: {Value}", value);
}
// Do your work here
await Task.Delay(1000, cancelToken);
}
}

JobInfo is a record that configures how and when a job runs.

PropertyTypeDefaultDescription
IdentifierstringrequiredUnique name for this job
JobTypeTyperequiredThe IJob implementation type
RunOnForegroundboolfalseAlso run when app is in foreground
ParametersDictionary<string, string>?nullKey/value pairs passed to the job
RequiredInternetAccessInternetAccessNoneNone, Any, or Unmetered
DeviceChargingboolfalseOnly run while charging
BatteryNotLowboolfalseOnly run when battery is adequate

Register jobs during service configuration so they are set up when the app starts.

// Using JobInfo directly
services.AddJob(new JobInfo(
"MyJob",
typeof(MyJob),
RequiredInternetAccess: InternetAccess.Any,
BatteryNotLow: true
));
// Using the fluent overload
services.AddJob(
typeof(MyJob),
identifier: "MyJob",
requiredNetwork: InternetAccess.Any
);

If you need to register jobs dynamically after startup, use IJobManager.

// First, register the job infrastructure during startup
services.AddJobs();
// Then, at runtime:
IJobManager jobManager; // injected
jobManager.Register(new JobInfo(
"DynamicJob",
typeof(MyJob),
Parameters: new Dictionary<string, string>
{
{ "userId", "123" }
}
));

When RunOnForeground is true, the job will also execute periodically while your app is in the foreground (on a timer).

services.AddJob(new JobInfo(
"SyncJob",
typeof(MySyncJob),
RunOnForeground: true,
RequiredInternetAccess: InternetAccess.Any
));

For jobs that need to persist state between runs, see Stateful Services.