如何使用 c# asp.net MVC 每 10 秒调用一次方法?
How to call a Method every 10 seconds with c# asp.net MVC?
我一直试图每 10 秒调用一个方法,我阅读了有关计时器和线程的信息,但问题是我不知道将要触发的方法放在哪里,MVC 项目有 Main?
private async Task ActualizarPrecios()
{
Entities model = new Entities();
var transaciones = model.Transacciones.Where(a => a.ESTADO ==
true).ToList();
string Url = ObtenerUrl(model);
ApiForex.IniciarCliente();
ProcesarRequest procesar = new ProcesarRequest();
JObject divisas = await procesar.CargarJson(Url);
foreach(Transacciones item in transaciones)
{
item.PRECIO_ACTUAL = (string)divisas["rates"][item.DIVISA]
["rate"];
}
model.SaveChanges();
}
上传数据库中表的某些列的代码。我不知道如何每隔 x 秒触发此方法
您可能想要研究调度 API 和第三方工具。 Hangfire(https://www.hangfire.io/)是我用过成功的
我会使用 JavaScript 来实现这一点。
在视图文件 (*.cshtml) 的底部添加此代码:
<script type="text/javascript">
var interval = 10000;
setInterval(function() { Update() }, interval);
function Update(){
$.get("/[ControllerName]/ActualizarPrecios", function(){
alert( "Load was performed.")}
);
}
</script>
解释:
<script type="text/javascript">
This is HTML denodes you are writing scripts in the JavaScript language
var interval = 10000;
setInterval(function() { Update() }, interval);
Sets the interval to 10 seconds (in milliseconds)
Then uses the JavaScript function for repeating executions on an interval see more on W3 Schools
function Update(){
$.get("/[ControllerName]/ActualizarPrecios", function(){
alert( "Load was performed.")}
);
}
This function uses Ajax in jQuery to call an API. Which exists in your controller. You haven't shared your controller name so replace the code accordingly, the method looks like a get method to me however it is post
then change $.get
to $.post
. The function after the url path is executed on callback to your request. For now, to make it clear during testing it is running the line alert( "Load was performed.")
which will pop up a dialog box in your browser; if you want to write something out to the html after running the request you can do that here.
*注意 jQuery 包含在 MVC C# 项目中。在您的 \shared\_Layout.cshtml
文件中,您会在页面底部的一个部分中看到它的引用。
1) 如果您的应用程序要托管在服务器或虚拟机 (VM) 上,那么您可以创建 window 服务并在其上注册服务。
2) 如果您的应用程序要托管在 Azure pass 上,那么您可以使用 Azure 网络作业。
Url : https://docs.microsoft.com/en-us/azure/app-service/webjobs-create
我一直试图每 10 秒调用一个方法,我阅读了有关计时器和线程的信息,但问题是我不知道将要触发的方法放在哪里,MVC 项目有 Main?
private async Task ActualizarPrecios()
{
Entities model = new Entities();
var transaciones = model.Transacciones.Where(a => a.ESTADO ==
true).ToList();
string Url = ObtenerUrl(model);
ApiForex.IniciarCliente();
ProcesarRequest procesar = new ProcesarRequest();
JObject divisas = await procesar.CargarJson(Url);
foreach(Transacciones item in transaciones)
{
item.PRECIO_ACTUAL = (string)divisas["rates"][item.DIVISA]
["rate"];
}
model.SaveChanges();
}
上传数据库中表的某些列的代码。我不知道如何每隔 x 秒触发此方法
您可能想要研究调度 API 和第三方工具。 Hangfire(https://www.hangfire.io/)是我用过成功的
我会使用 JavaScript 来实现这一点。
在视图文件 (*.cshtml) 的底部添加此代码:
<script type="text/javascript">
var interval = 10000;
setInterval(function() { Update() }, interval);
function Update(){
$.get("/[ControllerName]/ActualizarPrecios", function(){
alert( "Load was performed.")}
);
}
</script>
解释:
<script type="text/javascript">
This is HTML denodes you are writing scripts in the JavaScript language
var interval = 10000;
setInterval(function() { Update() }, interval);
Sets the interval to 10 seconds (in milliseconds)
Then uses the JavaScript function for repeating executions on an interval see more on W3 Schools
function Update(){
$.get("/[ControllerName]/ActualizarPrecios", function(){
alert( "Load was performed.")}
);
}
This function uses Ajax in jQuery to call an API. Which exists in your controller. You haven't shared your controller name so replace the code accordingly, the method looks like a get method to me however it is
post
then change$.get
to$.post
. The function after the url path is executed on callback to your request. For now, to make it clear during testing it is running the linealert( "Load was performed.")
which will pop up a dialog box in your browser; if you want to write something out to the html after running the request you can do that here.
*注意 jQuery 包含在 MVC C# 项目中。在您的 \shared\_Layout.cshtml
文件中,您会在页面底部的一个部分中看到它的引用。
1) 如果您的应用程序要托管在服务器或虚拟机 (VM) 上,那么您可以创建 window 服务并在其上注册服务。
2) 如果您的应用程序要托管在 Azure pass 上,那么您可以使用 Azure 网络作业。 Url : https://docs.microsoft.com/en-us/azure/app-service/webjobs-create