実行時間の長い Windows サービスを作成しようとしているので、実際のワーカー クラスを別のスレッドで実行して、右クリックして Windows サービスで開始を選択したときに「サービスがタイムリーに応答しませんでした」というエラーを回避する必要があります。マネジャー。
ワーカー クラス ("NotificationProcess") には多数の依存関係があり、これらを満たすために Autofac を使用しています。
ワーカー クラスに Autofac を設定する方法がよくわかりません。現時点では、ワーカー クラスの「Execute」メソッドで DbContext を使用しようとすると、DbContext が破棄されたというエラーが表示されます。
Windows サービスを記述し、autofac によって満たされた依存関係を持つワーカー クラスに新しいスレッドを使用する方法を探していると思います。
私はグーグルで検索しましたが、この例は見つかりません。
どんな提案も素晴らしいでしょう。
これが私がこれまでに持っているものです...
Program.cs:
    static class Program
{
    static void Main()
    {
        using (var container = ServiceStarter.CreateAutoFacContainer())
        {
            var service = container.Resolve<NotificationService>();
            if (Environment.UserInteractive)
            {
                service.Debug();
            }
            else
            {
                ServiceBase.Run(container.Resolve<NotificationService>());
            }
        }
サービス クラス:
public partial class NotificationService : ServiceBase
{
    private NotificationProcess _app;
    readonly ILifetimeScope _lifetimeScope;
    public NotificationService(ILifetimeScope lifetimeScope)
    {
        _lifetimeScope = lifetimeScope;
        InitializeComponent();
    }
    protected override void OnStart(string[] args)
    {
        _app = _lifetimeScope.Resolve<NotificationProcess>();
        _app.Start();
    } 
ワーカークラス:
    public class NotificationProcess
{
    private Thread _thread;
    private readonly IBankService _bankService;
    private readonly IRateService _rateService;
    private readonly IEmailService _emailService;
    private readonly IRateChangeSubscriberService _rateChangeSubscriberService;
    private readonly IRateChangeNotificationService _rateChangeNotificationService;
    private readonly ILogManager _logManager;
    public NotificationProcess(IBankService bankService, ILogManager logManager, IRateService rateService, IEmailService emailService, 
        IRateChangeSubscriberService rateChangeSubscriberService, IRateChangeNotificationService rateChangeNotificationService)
    {
        _bankService = bankService;
        _rateService = rateService;
        _emailService = emailService;
        _rateChangeSubscriberService = rateChangeSubscriberService;
        _rateChangeNotificationService = rateChangeNotificationService;
        _logManager = logManager;
    }
    public void Start()
    {
        _thread = new Thread(new ThreadStart(Execute));
        _thread.Start();
    }
    public void Execute()
    {
        try
        {
            var rateChangeToNotify = _rateService.GetRateChangesForNotification();
            foreach (var rateChange in rateChangeToNotify)
            {
                 //do whatever business logic.....
            }
        }
    }