[英]Calling a ASP.net WebMethod from the success function of another WebMethod
[英]ASP.NET Request time out calling an async webmethod in ASMX from jquery
這幾天我都想不通。 我已經嘗試了一切。 我必須更改我的雲存儲,所以我正在嘗試重構。 首先,我在中間 class 中使用 MinIO 來連接存儲。
S3ObjectStorage.cs
public class S3ObjectStorage
{
private MinioClient _client;
private string _bucketName;
public S3ObjectStorage(string key, string secret, string region, string httpsEndPoint, string bucketName) {
_client = new MinioClient(httpsEndPoint, key, secret, region).WithSSL();
_bucketName = bucketName;
}
public async Task<string> ListFilesAsync(string path)
{
return await ListObjects(path, false);
}
private async Task<string> ListObjects(string path, bool isDir) {
var files = await _client.ListObjectsAsync(_bucketName, path)
.Where<Item>(f => (isDir) ? (f.Key.EndsWith("/") && f.IsDir) : !f.Key.EndsWith("/"))
.ToList();
return Newtonsoft.Json.JsonConvert.SerializeObject(files);
}
}
TestPage.aspx.vb(正常運行)
Public Class TestPage
Inherits Page
Protected Async Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
cs = New S3ObjectStorage(CS_KEY, CS_SECRET, CS_REGION, HTTPS_ENDPOINT, BUCKET_NAME)
End Sub
Private Async Function List(path As String) As Task
Dim resultFiles As String = Await cs.ListFilesAsync(path)
Dim files = Newtonsoft.Json.JsonConvert.DeserializeObject(resultFiles)
Me.rptFiles.DataSource = files
Me.rptFiles.DataBind()
End Function
End Class
稍后我會嘗試使用另一種技術,但現在,我需要在 ASMX 中使用 webmethod 來盡可能少地修改代碼。 使用其他雲存儲,所有方法都是同步的,但 MinIO 沒有。
索引.aspx
<script type="text/javascript" language="javascript">
function listFiles() {
$.ajax({
type: 'POST', contentType: "application/json; charset=utf-8", dataType: 'json', async: true,
url: '/WS/WSFileManager.asmx/ListFiles',
...);
WSFileManager.asmx.vb(嘗試兩種方式)
<WebMethod(EnableSession:=True)>
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
Public Async Function ListFiles(signature As String, requestCode As String, previousDeleted As String) As Threading.Tasks.Task(Of String)
'First way
Dim minioClient As New MinioClient(GlobalConstants.S3_HTTPS_ENDPOINT,
GlobalConstants.S3_KEY,
GlobalConstants.S3_KEY,
GlobalConstants.S3_REGION)
minioClient.WithSSL()
Dim files As New List(Of DataModel.Item)
Dim observable As IObservable(Of DataModel.Item) = minioClient.ListObjectsAsync(GlobalConstants.S3_BUCKET_NAME, pathApp, False)
Dim suscription As IDisposable = observable.Subscribe(
Sub(item)
files.Add(item)
End Sub)
suscription.Dispose()
'files.Count always is 0
'Second way
Dim s3Storage As New S3ObjectStorage(GlobalConstants.S3_KEY, GlobalConstants.S3_SECRET,
GlobalConstants.S3_REGION, GlobalConstants.S3_HTTPS_ENDPOINT,
GlobalConstants.S3_BUCKET_NAME)
Dim resultFiles As String = Await s3Storage.ListFilesAsync(pathApp)
'**Never reach this line**
....
我目前的雲存儲將於明年 1 月 31 日結束,因此我需要盡快運行新的雲存儲。
非常感謝!
非常非常簡單!
我已經回到將 ASMX 中的方法作為同步
WSFileManager.asmx.vb
<WebMethod(EnableSession:=True)>
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
Public Function ListFiles(signature As String, requestCode As String, previousDeleted As String) As String
Dim task = Threading.Tasks.Task.Run(Async Function() Await s3Storage.ListFiles(pathApp))
task.Wait()
Dim lFiles = Newtonsoft.Json.JsonConvert.DeserializeObject(Of List(Of DataModel.Item))(task.Result)
...
End Function
在前端,我也借此機會停止使用 jQuery index.aspx
<script type="text/javascript" language="javascript">
function listFiles() {
const fetchOptions = {
method: 'POST', headers: {'Content-Type': 'application/json; charset=utf-8'},
body: JSON.stringify({ /* parameters */ })
};
const result = fetch('/WS/WSFileManager.asmx/ListFiles', fetchOptions)
.then(response => response.json())
.catch(error => console.error('Error:', error))
.then(data => console.log(JSON.parse(data.d).legth+' files')));
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.