using System.Threading.Tasks;
namespace PCL.Core.App.IoC;
///
/// General service class for constructing in a more convenient way.
///
public abstract class GeneralService : ILifecycleService
{
///
public string Identifier { get; }
///
public string Name { get; }
///
public bool SupportAsync { get; }
///
/// The context of the service instance,
/// used for declaration, logging, lifecycle operation, etc.
///
protected LifecycleContext ServiceContext { get; }
///
/// Initialize a general service instance.
/// This constructor should only be extended rather than invoked directly.
///
/// see
/// see
/// see
protected GeneralService(string identifier, string name, bool asyncStart = true)
{
Identifier = identifier;
Name = name;
SupportAsync = asyncStart;
ServiceContext = Lifecycle.GetContext(this);
}
///
/// Start the service, will be invoked on the specified state
/// from of .
///
public virtual void Start() { }
///
/// Stop the service, will be invoked while program exiting,
/// or if throws an exception.
///
public virtual void Stop() { }
public Task StartAsync()
{
Start();
return Task.CompletedTask;
}
public Task StopAsync()
{
Stop();
return Task.CompletedTask;
}
}