gpt4 book ai didi

.NET Core Controller Run Dapper Query and write to a file in background(.NET核心控制器在后台运行Dapper查询和写入文件)

转载 作者:bug小助手 更新时间:2023-10-24 20:39:02 25 4
gpt4 key购买 nike



Our use case requires huge processing of data extraction and writing to a file to be further processed by other processors.

我们的用例需要进行大量的数据提取和写入文件的处理,以供其他处理器进一步处理。


Due to the fact that there is a lot of data involved, it's impossible for the the service that request to wait for a response and then pass the data to be processed by the worker processor.

由于涉及大量数据,请求的服务不可能等待响应,然后将数据传递给工作处理器进行处理。


Trying to achieve "fire and forget" type of mechanism here, but it doesn't seem to be working.

试图在这里实现“火和忘记”类型的机制,但它似乎不起作用。


[HttpPost]
[Route("request/ret")]
public async Task<IActionResult> PostData([FromBody][BindRequired] modelnh request,
[Required] string code, [FromServices]IServiceScopeFactory serviceScopeFactory, CancellationToken cancellationToken)
{
var result = new List<rbt>();

_ = Task.Run(async () =>
{
using (var scope = serviceScopeFactory.CreateScope())
{
qrest = (List<rbt>)await _bkgservice.GetData(request,code, cancellationToken).ConfigureAwait(false);;
await _filehelper.SaveToFile(qrest ,tgtdirec, append);

}
});

return Ok;
}

BKGService:

BKGService:


public async Task<IEnumerable<rbt>> GetData(rbt request, string code)
{
string query = "SELECT * ........";
result = await _dbprov.RunQueryAsync<rbt>(query);
return result;
}

I have not included the SaveToFile method code, but I think the question is understood here.

我没有包括SaveToFile方法代码,但我认为这里已经理解了这个问题。


更多回答

What's not working? What issues are you getting?

什么不管用?你有什么问题

@Orifjon , the problem I am running into is that the program is doing fire and forget , but the dapper query is not being executed (traced on the sql side) and subsequently no file is created

@Orifjon,我遇到的问题是该程序正在执行一次又一次的遗忘,但是没有执行漂亮的查询(在SQL端跟踪),因此没有创建文件

Any recommendation or suggestions that i can try.

任何我可以尝试的建议或建议。

It may be the problem from database not returning any data and hence its not able to write any file. Need to debug the program.

这可能是数据库没有返回任何数据的问题,因此它无法写入任何文件。需要对程序进行调试。

I have verified two things, one it has data and the other thing is when executed with task.run I don’t even see the connection being established in the session logs in the database

我已经验证了两件事,一件事是它有数据,另一件事是在使用任务执行时。Run我甚至没有在数据库的会话日志中看到建立的连接

优秀答案推荐

The problem is that your code is defining a scope, but it's not actually using it. E.g., _bkgservice is the one injected into the controller, not a new one retrieved from the scope:

问题是您的代码正在定义一个作用域,但实际上并没有使用它。例如,_bkgservice是注入到控制器中的服务,而不是从作用域检索到的新服务:


using (var scope = serviceScopeFactory.CreateScope())
{
qrest = (List<rbt>)await _bkgservice.GetData(request,code, cancellationToken).ConfigureAwait(false);;
await _filehelper.SaveToFile(qrest ,tgtdirec, append);
}

Since the code is using fire-and-forget, if _bkgservice and _filehelper are scoped services, they may be disposed before the code in the Task.Run actually uses them. Also, since the code is using fire-and-forget, any exceptions are ignored, so it's not obvious at all what is happening.

由于代码使用的是即发即忘,如果_bkgservice和_filehelper是作用域服务,则它们可能会在Task.Run中的代码实际使用它们之前被释放。此外,由于代码使用的是即发即忘,任何异常都会被忽略,因此发生了什么并不明显。


A quick solution is to enforce the Task.Run code to use services resolved from the scope, e.g., using a static local method:

一个快速的解决方案是强制执行Task.Run代码以使用从作用域解析的服务,例如,使用静态本地方法:


public async Task<IActionResult> PostData([FromBody][BindRequired] modelnh request,
[Required] string code, [FromServices]IServiceScopeFactory serviceScopeFactory, CancellationToken cancellationToken)
{
var result = new List<rbt>();

// Code in a static local method cannot accidentally access members.
// When adding parameters, ensure that you do not add any values that are dependency-injected.
static async Task DoWorkAsync()
{
using (var scope = serviceScopeFactory.CreateScope())
{
var bkgService = scope.ServiceProvider.GetRequiredService<TBkgService>();
var fileHelper = scope.ServiceProvider.GetRequiredService<FileHelper>();
qrest = (List<rbt>)await bkgservice.GetData(request,code, cancellationToken).ConfigureAwait(false);;
await filehelper.SaveToFile(qrest ,tgtdirec, append);
}
}

_ = Task.Run(() => DoWorkAsync());

return Ok;
}

However, even if you get it compiling (and - for some definition of the term - "working"), you still have the problems of fire-and-forget. Specifically:

然而,即使你把它编译了(并且--对于术语的某种定义--“工作”),你仍然会有“火就是忘”的问题。具体地说,就是:



  1. Fire-and-forget work may occasionally be lost, e.g., when you do rolling upgrades.

  2. Fire-and-forget work silently ignores errors.


Fire-and-forget is inherently dangerous, and generally unsuitable for any real-world production application. Instead of using fire-and-forget, I recommend either:

火和忘记本质上是危险的,通常不适合任何现实世界的生产应用程序。我的建议是,不要使用“一发不可收拾”的方式:



  1. Not using fire-and-forget. Just let PostData complete naturally after it has completed its work. OR:

  2. Develop a proper basic distributed architecture, as I describe on my blog.



you can run a Dapper query and write the results to a file in the background by using asynchronous programming and background tasks. Here's a step-by-step guide on how to achieve this:
Install Required NuGet Packages
Create a Dapper Query
Write Data to a File
Background Task

您可以运行Dapper查询,并使用异步编程和后台任务在后台将结果写入文件。以下是如何实现这一点的逐步指南:安装所需的NuGet包创建Dapper查询将数据写入文件后台任务


更多回答

This is more of a summary of an answer than an actual answer. It is lacking detail and does not address any of the specific problems described in the original question.

这更多的是一个答案的总结,而不是一个实际的答案。它缺乏细节,也没有涉及原问题中所述的任何具体问题。

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

您的答案可以通过其他支持信息来改进。请编辑以添加更多详细信息,如引用或文档,以便其他人可以确认您的答案是正确的。你可以在帮助中心找到更多关于如何写出好答案的信息。

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