更长服务器调用的性能指示符 ASP.Net MVC

我只想在冗长的服务器调用期间创建一个执行指示符。 我无法创建一个请求 ajax post 到控制器,而控制器执行长效程。

我想创建一个额外的动作来获得当前长期任务的实际配方。
我试图在请求中创建调查 ajax, 然后我可以从服务器返回状态并在客户端的执行指示符中显示它。 有任何想法吗 ?
已邀请:

窦买办

赞同来自:

正确和最简单的方法是使用 SignalR. 请下载 Microsoft SignalR 在
https://www.nuget.org/packages ... 2.1.2
在名为Hubs的项目路径中的单独文件夹中创建集线器类,在集线器文件夹中添加两个类文件

Startup.cs


using Owin;
using Microsoft.Owin;
[assembly: OwinStartup/typeof/SignalRChat.Startup//]
namespace SignalRChat
{
public class Startup
{
public void Configuration/IAppBuilder app/
{
// Any connection or hub wire up and configuration should go here
app.MapSignalR//;
}
}
}


ProgressHub.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using Microsoft.AspNet.SignalR;

namespace RealTimeProgressBar
{
public class ProgressHub : Hub
{

public string msg = "Initializing and Preparing...";
public int count = 1;

public static void SendMessage/string msg , int count/
{
var message = "Process completed for " + msg;
var hubContext = GlobalHost.ConnectionManager.GetHubContext<progresshub>//;
hubContext.Clients.All.sendMessage/string.Format/message/, count/;
}

public void GetCountAndMessage//
{
Clients.Caller.sendMessage/string.Format/msg/, count/;
}
}
}


在控制器中


// assemblies
using Microsoft.AspNet.SignalR;
using RealTimeProgressBar;


//call this method inside your working action
ProgressHub.SendMessage/"initializing and preparing", 2/;


洞察力,


<!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
<!--Reference the SignalR library. -->
<script src="~/Scripts/jquery.signalR-2.1.2.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="~/signalr/hubs"></script>
<!--SignalR script to update the chat page and send messages.-->
<script type="text/javascript">
$/document/.ready/function // {

$/"#progressBar"/.kendoProgressBar/{
min: 0,
max: 100,
type: "percent",
}/;
}/;

function StartInvoicing//
{
var progressNotifier = $.connection.progressHub;

// client-side sendMessage function that will be called from the server-side
progressNotifier.client.sendMessage = function /message, count/ {
// update progress
UpdateProgress/message, count/;
//alert/message/;
};

// establish the connection to the server and start server-side operation
$.connection.hub.start//.done/function // {
// call the method CallLongOperation defined in the Hub
progressNotifier.server.getCountAndMessage//;
}/;
}

// Update the progress bar
function UpdateProgress/message, count/ {
var result = $/"#result"/;
result.html/message/;
$/"#progressBar"/.data/"kendoProgressBar"/.value/count/;
}
</script>


有关更多信息,请联系一些现有文章 google
</progresshub>

郭文康

赞同来自:

我有很长的服务,我给了下面的基本想法。 使用它以满足您的要求。

我弥补了进步论点的结构
ProgressArgs


在一个长期的工作服务中 LongRunningProcess// 定期更新进度值并以格式保存 JSON 在数据库中

创建了一种行动方法
getProgress//

, 这将返回该行 JSON progress by ajax.

创建了一个功能 Javascript
getProgress//

, 启动后将以定期的间隔调用服务器以进行进度,直到进程完成。

我带来了一个实施例的示例。 我希望它能帮到你。

课程进展参数的结构


public class ProgressArgs
{
public int Completed { get; set; }
public int Total { get; set; }
public int Percentage { get; set; }
public string Status { get; set; }
}


在此过程中,我继续更新数据库中的统计信息


public void LongRunningProcess//
{

ProgressArgs result = new ProgressArgs//;
result.Completed = 0;
result.Total = userList.Count;
foreach /var item in itemList/
{
//Do Some processing which u want to do

result.Total++;
result.Percentage = /result.Completed * 100/ / result.Total;
result.Status = "Processing";
string strToSave = Newtonsoft.Json.JsonConvert.SerializeObject/result/;
//update the strToSave to the database somewhere.
}

//after completing of processing
result.Total++;
result.Percentage = /result.Completed * 100/ / result.Total;
result.Status = "Completed";
string strToSave = Newtonsoft.Json.JsonConvert.SerializeObject/result/;
//update the strToSave to the database somewhere.

}


行为 C#, 进步 ajax


public string getProgress//
{
string strJSON = config.GetValue/"progress"/; //Get stats from the database which is saved in json
return strJSON;
}


代码 Javascript


//Ajax Get Progress function
function getProgress// {
var dataToSend = '';
$.ajax/{
url: '@Url.Action/"getProgress"/', //Link to the action method
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: dataToSend,
success: function /response/ {
console.log/response/;
if /response != null/ {
data = JSON.parse/response/;
console.log/data/;
//update the progressbar
progressbar.progressbar/"value", data.Percentage/;
//When the jobalert status is completed clear the interval
if /data.Status == 0/ {
setTimeout/getProgress, 800/; //TImout function to call the respective function at that time
}
serviceCompleted/data/; function to call after completing service
}
},
error: function /xhr/ {
alert/'Error: There was some error while posting question. Please try again later.'/;
}
}/;
}

要回复问题请先登录注册