gpt4 book ai didi

.net-core - Net Core Worker Windows服务中的EF Core DBContext

转载 作者:行者123 更新时间:2023-12-03 14:38:32 27 4
gpt4 key购买 nike

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

当我从Visual Studio 2019运行以下代码时,一切正常,但是当我发布(Target 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
)

最佳答案

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

Release folder structure and confusing publish folder

关于.net-core - Net Core Worker Windows服务中的EF Core DBContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59749330/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com