Net Core Worker Windows 服务中的 EF Core DBContext

Posted

技术标签:

【中文标题】Net Core Worker Windows 服务中的 EF Core DBContext【英文标题】:EF Core DBContext inside Net Core Worker Windows service 【发布时间】:2020-05-02 02:19:51 【问题描述】:

我想要实现的是编写一个简单的 .net core 后台工作程序 (.net core 3.1),当这个工作程序作为 Windows 服务运行时,我将数据写入 SQL Server 数据库(通过 EF Core 3.1)。

当我从 Visual Studio 2019 运行以下代码时,一切正常,但是当我发布(目标 win-x64)并将 .exe 注册为我的 win10 机器上的服务时,我收到以下异常:

Microsoft.Data.SqlClient is not supported on this platform.

对造成这种情况的原因以及如何解决它有任何想法吗?

Program.cs

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Soteria.Common.Database;

namespace Soteria.Service

    public class Program
    
        public static void Main(string[] args)
        
            CreateHostBuilder(args).Build().Run();
        

        public static IHostBuilder CreateHostBuilder(string[] args)
        
            var host = Host.CreateDefaultBuilder(args)
                .UseWindowsService()
                .ConfigureServices((hostContext, services) =>
                
                    var optionsBuilder = new DbContextOptionsBuilder<SoteriaDbContext>();
                    optionsBuilder.UseSqlServer("Server=.\\SQLEXPRESS;Database=Soteria;Trusted_Connection=True;");//,
                    services.AddScoped<SoteriaDbContext>(s => new SoteriaDbContext(optionsBuilder.Options));

                    services.AddHostedService<Worker>();
                );

            return host;
        
    

Worker.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Soteria.Common.Database;
using Soteria.Common.Messaging;
using Soteria.Common.Models;

namespace Soteria.Service

    public class Worker : BackgroundService
    
        private readonly ILogger<Worker> _logger;
        private readonly IServiceScopeFactory _serviceScopeFactory;

        public Worker(ILogger<Worker> logger, IServiceScopeFactory serviceScopeFactory)
        
            _logger = logger;
            _serviceScopeFactory = serviceScopeFactory;
        

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        
            while (!stoppingToken.IsCancellationRequested)
            
                using var scope = _serviceScopeFactory.CreateScope();

                var dbContext = scope.ServiceProvider.GetRequiredService<SoteriaDbContext>();

                dbContext.Tests.Add(new Test() Date = DateTime.Now);
                dbContext.SaveChanges();

                await Task.Delay(1000, stoppingToken);               
            
          
    

SoteriaDbContext.cs

using Microsoft.EntityFrameworkCore;
using Soteria.Common.Models;

namespace Soteria.Common.Database

    public class SoteriaDbContext: DbContext
    
        public SoteriaDbContext(DbContextOptions<SoteriaDbContext> options)
            : base(options)
        
        

        public DbSet<Test> Tests  get; set; 

        protected override void OnModelCreating(ModelBuilder modelBuilder)
                    
            modelBuilder.Entity<Test>().ToTable("Tests");
        

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        
            optionsBuilder.EnableSensitiveDataLogging(true);
        
    

Test.cs

using System;

namespace Soteria.Common.Models

    public class Test
    
        public int? Id  get; set; 
        public DateTime Date  get; set; 
    

Tests.sql

CREATE TABLE [dbo].[Tests]
(
    [Id] INT NOT NULL PRIMARY KEY IDENTITY, 
    [Date] DATETIME NOT NULL
)

【问题讨论】:

像您一样注册我的 DbContext 时遇到此异常。你知道解决办法吗? InvalidOperationException:无法使用单例“Microsoft.Extensions.Hosting.IHostedService”中的作用域服务“DbContext”。 【参考方案1】:

我必须安装 win-x64 文件夹的已发布源,而不是“发布”文件夹才能使其真正起作用。即使我的目标运行时是 win-x64,这也会导致我期望发布的结果会在“发布”文件夹中。

Release folder structure and confusing publish folder

【讨论】:

以上是关于Net Core Worker Windows 服务中的 EF Core DBContext的主要内容,如果未能解决你的问题,请参考以下文章

[翻译] 使用 .NET Core 3.0 创建一个 Windows 服务

[翻译] 使用 .NET Core 3.0 创建一个 Windows 服务

如何为 .NET Core 3.0 Worker 服务设置事件日志

.net core 5 worker 服务的无密码连接字符串

集成测试 .NET Core 3 Worker

.NET Core 3 Worker 服务设置依赖注入