ASP.NET 从 jquery 调用 ASMX 中的异步 Web 方法请求超时

ASP.NET Request time out calling an async webmethod in ASMX from jquery

我几天都弄不明白。我已经尝试了一切。我必须更改我的云存储,所以我正在尝试重构。首先,我在中间使用 MinIO class 来连接存储。

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 不是。

index.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')));
...