[英]Bulk upload via REST api
I have the goal of uploading a Products CSV of ~3000 records to my e-commerce site. 我的目标是将约3000条记录的产品CSV上传到我的电子商务网站。 I want to utilise the REST API that my e-comm platform provides so I have something I can re-use and build upon for future sites that I may create. 我想利用我的电子通讯平台提供的REST API,这样我就可以重用某些东西,并在以后可能创建的网站上使用。
My main issue that I am having trouble working through is: - System.Threading.ThreadAbortException 我遇到的主要问题是: -System.Threading.ThreadAbortException
Which I can only attribute to how long it takes to process through all 3K records via a POST request. 我只能将其归因于通过POST请求处理所有3K记录所花费的时间。 My code: 我的代码:
public ActionResult WriteProductsFromFile()
{
string fileNameIN = "19107.txt";
string fileNameOUT = "19107_output.txt";
string jsonUrl = $"/api/products";
List<string> ls = new List<string>();
var engine = new FileHelperAsyncEngine<Prod1>();
using (engine.BeginReadFile(fileNameIN))
{
foreach (Prod1 prod in engine)
{
outputProduct output = new outputProduct();
if (!string.IsNullOrEmpty(prod.name))
{
output.product.name = prod.name;
string productJson = JsonConvert.SerializeObject(output);
ls.Add(productJson);
}
}
}
foreach (String s in ls)
nopApiClient.Post(jsonUrl, s);
return RedirectToAction("GetProducts");
}
}
Since I'm new to web-coding, am I going about this the wrong way? 由于我是网络编码的新手,所以我会以错误的方式进行操作吗? Is there a preferred way to bulk-upload that I haven't come across? 有没有我没有遇到过的首选的批量上传方法?
I've attempted to use the TaskCreationOptions.LongRunning flag, which helps the cause slightly but doesn't get me anywhere near my goal. 我尝试使用TaskCreationOptions.LongRunning标志,该标志可以稍有帮助,但不能使我接近目标。
Web and api controller actions are not meant to do long running tasks - besides locking up the UI/thread, you will be introducing a series of opportunities for failure that you will have little recourse in recovering from. Web和api控制器操作并不打算执行长期运行的任务-除了锁定UI /线程外,您还将引入一系列失败的机会,而这些失败的机会很少。
But it's not all bad you have a lot of options here, there is a lot of literature on async/cloud architecture - which explains how to deal with files and these sorts of scenarios. 但是您在这里有很多选择并不都是坏事,关于异步/云架构的文献很多-解释了如何处理文件和这种情况。
What you want to do is disconnect the processing of your file from the API request (in your application not the 3rd party) 您要做的是从API请求中断开文件处理(在您的应用程序中不是第三方)
It will take a little more work but will ultimately create a more reliable application. 这将需要更多的工作,但最终将创建一个更可靠的应用程序。
Step 1: 第1步:
Drop the file immediately to disk - I see you have the file on DISK already not sure how it gets there but either way it will work out the same. 立即将文件拖放到磁盘上-我看到您已经在DISK上拥有该文件,但不确定如何到达该文件,但是无论哪种方式,它都可以正常工作。
Step 2: 第2步:
Use a process running as 使用运行方式为
- a console app (easiest) -控制台应用程序(最简单)
- a service (requires some sort of install/uninstall of the service) -服务(需要某种服务的安装/卸载)
- or even a thread in your web app (but you will struggle to know when it fails) -甚至是您的网络应用中的一个线程(但是您将很难知道它何时会失败)
Which ever way you choose, the process will watch a directory for file changes, when there is a change it will kick off your method to happily process the file as you like. 无论选择哪种方式,该过程都会监视目录中文件的更改,发生更改时,它将启动您的方法来根据您的喜好愉快地处理文件。
Check out the FileSystemWatchers here is a basic example: https://www.dotnetperls.com/filesystemwatcher 在这里查看FileSystemWatchers是一个基本示例: https : //www.dotnetperls.com/filesystemwatcher
Additionally: 另外:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.