gpt4 book ai didi

c# - 如何在类/独立线程 ASP.Net Core 中使用 ApplicationDbContext

转载 作者:行者123 更新时间:2023-11-30 14:45:22 25 4
gpt4 key购买 nike

我试图让一个单独的线程启动一个循环过程,以从 api 调用中收集和导入数据,并将其插入到本地数据库中。我尝试以与在 Controller 中相同的方式创建数据库连接,但它一直给我各种错误。

我试过很多方法。对于下面的设置,它给了我这个错误:

System.ObjectDisposedException: 'Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.'

设置用于测试目的。

    public class HomeController : Controller
{
public ApplicationDbContext db;

public HomeController(ApplicationDbContext context)
{
db = context;
}

public IActionResult Index()
{
// This executes the code and inserts the test row no problem.
BinanceData binanceData = new BinanceData();
binanceData.Symbol = "Test";
binanceData.RealTime = DateTime.Now;
db.BinanceData.Add(binanceData);
db.SaveChanges();
// End

// Where I create the new thread and start the process
Thread doThis = new Thread(delegate ()
{
DataCollection dataCollection = new DataCollection(db);
dataCollection.InsertData(DateTime.Now);
});
doThis.Start();
// End

return View();
}
}

这是我尝试创建连接(或传递现有连接)并开始循环和收集/插入数据的类。

    public class DataCollection
{
public ApplicationDbContext db;

public DataCollection(ApplicationDbContext context)
{
db = context;
}

public void InsertData(DateTime nextTime)
{
List<string> tokens = new List<string> { "ETHBTC", "LTCBTC", "BNBBTC", "NEOBTC", "GASBTC", "BTCUSDT", "MCOBTC", "WTCBTC", "LRCBTC", "QTUMBTC", "YOYOBTC", "OMGBTC", "ZRXBTC", "STRATBTC", "SNGLSBTC", "BQXBTC", "KNCBTC", "FUNBTC", "SNMBTC", "IOTABTC", "LINKBTC", "XVGBTC", "SALTBTC", "MDABTC", "MTLBTC", "SUBBTC", "EOSBTC", "SNTBTC", "ETCBTC", "MTHBTC", "ENGBTC", "DNTBTC", "ZECBTC", "BNTBTC", "ASTBTC", "DASHBTC", "OAXBTC", "BTGBTC", "EVXBTC", "REQBTC", "VIBBTC", "TRXBTC", "POWRBTC", "ARKBTC", "XRPBTC", "MODBTC", "ENJBTC", "STORJBTC", "KMDBTC", "RCNBTC", "NULSBTC", "RDNBTC", "XMRBTC", "DLTBTC", "AMBBTC", "BATBTC", "BCPTBTC", "ARNBTC", "GVTBTC", "CDTBTC", "GXSBTC", "POEBTC", "QSPBTC", "BTSBTC", "XZCBTC", "LSKBTC", "TNTBTC", "FUELBTC", "MANABTC", "BCDBTC", "DGDBTC", "ADXBTC", "ADABTC", "PPTBTC", "CMTBTC", "XLMBTC", "CNDBTC", "LENDBTC", "WABIBTC", "TNBBTC", "WAVESBTC", "GTOBTC", "ICXBTC", "OSTBTC", "ELFBTC", "AIONBTC", "NEBLBTC", "BRDBTC", "EDOBTC", "WINGSBTC", "NAVBTC", "LUNBTC", "APPCBTC", "VIBEBTC", "RLCBTC", "INSBTC", "PIVXBTC", "IOSTBTC", "STEEMBTC", "NANOBTC", "VIABTC", "BLZBTC", "AEBTC", "NCASHBTC", "POABTC", "ZILBTC", "ONTBTC", "STORMBTC", "XEMBTC", "WANBTC", "WPRBTC", "QLCBTC", "SYSBTC", "GRSBTC", "CLOAKBTC", "GNTBTC", "LOOMBTC", "REPBTC", "TUSDBTC", "ZENBTC", "SKYBTC", "CVCBTC", "THETABTC", "IOTXBTC", "QKCBTC", "AGIBTC", "NXSBTC", "DATABTC", "SCBTC", "NPXSBTC", "KEYBTC", "NASBTC", "MFTBTC", "DENTBTC", "ARDRBTC", "HOTBTC", "VETBTC", "DOCKBTC", "POLYBTC", "PHXBTC", "HCBTC", "GOBTC", "PAXBTC", "RVNBTC", "DCRBTC", "USDCBTC", "MITHBTC", "BCHABCBTC" };

foreach (string token in tokens)
{
BinanceData binance = new BinanceData();
using (var client = new BinanceClient())
{
var data = client.GetKlines(token, Binance.Net.Objects.KlineInterval.OneMinute, null, null, 2);
var kline = data.Data[0];
binance.Symbol = token;
binance.Close = kline.Close;
binance.CloseTime = kline.CloseTime;
binance.High = kline.High;
binance.Low = kline.Low;
binance.Open = kline.Open;
binance.OpenTime = kline.OpenTime;
binance.QuoteAssetVolume = kline.QuoteAssetVolume;
binance.TakerBuyBaseAssetVolume = kline.TakerBuyBaseAssetVolume;
binance.TakerBuyQuoteAssetVolume = kline.TakerBuyQuoteAssetVolume;
binance.TradeCount = kline.TradeCount;
binance.Volume = kline.Volume;
binance.RealTime = DateTime.Now;
}
db.BinanceData.Add(binance);
db.SaveChanges();
}
CountUntilNextMin(nextTime);
}

public void CountUntilNextMin(DateTime nextTime)
{
while (DateTime.Now < nextTime)
{
System.Threading.Thread.Sleep(10000);
}
DateTime passTime = DateTime.Now.AddMinutes(1);
InsertData(passTime);
}
}

这是我的 ApplicationDbContext 类

    public class ApplicationDbContext : IdentityDbContext
{
public DbSet<ApplicationUser> ApplicationUsers { get; set; }
public DbSet<BinanceData> BinanceData { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}

public ApplicationDbContext()
: base()
{
}
}

还有我的 StartUp.cs ConfigureServices 方法

    public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});

services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<IdentityUser, IdentityRole>()
.AddDefaultUI()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();

services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

最佳答案

默认情况下,您的 DI 容器将 DbContext 的范围限定为请求。如果你想在长时间运行的后台进程中使用一个,直接创建它即可。例如:

Thread doThis = new Thread(delegate ()
{
using (var db = new ApplicationDbContext())
{
DataCollection dataCollection = new DataCollection();
dataCollection.InsertData(DateTime.Now);
}
});

需要配置 DbContext,最简单的方法是重写 OnConfiguring 以连接到您的数据库。至少,像这样:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=YourServer;Database=YourDatabase;Trusted_Connection=True;MultipleActiveResultSets=true");
}

尽管您可能希望从您的配置中读取连接字符串。

关于c# - 如何在类/独立线程 ASP.Net Core 中使用 ApplicationDbContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53988779/

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