如何修复RemoteJSDataStream NullReferenceException



我在从并行上传文件作为StreamContent时出错。NET 6/Blazor服务器端通过HttpClient连接到API。以下是精简的代码和错误的全文:

p。S.框架传递给Microsoft.AspNetCore.Components.Server.Circuits.RemoteJSDataStream.ReceiveDataRemoteJSRuntimeNULL,因此抛出错误。但是,我不明白NULL为什么会通过。我无法抓住它,扑灭它,以某种方式影响它。

p。S.S.它与ApiClient无关,运行良好。错误只发生在第一个页面加载后的启动时,奇怪的是,在重新加载页面并重新启动后,一切都正常。

源代码:

https://github.com/abberdeen/BlazorServer.ParallelFileUpload

相关问题:

https://github.com/dotnet/aspnetcore/issues/38854

public void AddFilesToUploadQueue(List<FileUploadDto> files)
{
foreach (var item in files)
{
fileUploadQueue.Enqueue(item);
}
for (int i = 0; i < files.Count; i++)
{
Task t = factory.StartNew(() => UploadOne());
tasks.Add(t); 
}
} 
private async Task UploadOne()
{ 
semaphore.WaitOne();
FileUploadDto item;
if (!fileUploadQueue.TryDequeue(out item))
{
return;
} 
// Make HttpRequest
await UploadFileAsync(item);

semaphore.Release();
}
blazor.server.js:1 [2021-12-06T19:07:53.123Z] Error: System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.AspNetCore.Components.Server.Circuits.RemoteJSDataStream.ReceiveData(RemoteJSRuntime runtime, Int64 streamId, Int64 chunkId, Byte[] chunk, String error)
at Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.<>c__11`1.<<InvokeAsync>b__11_0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost.ReceiveJSDataChunk(Int64 streamId, Int64 chunkId, Byte[] chunk, String error)

输出窗口错误:

System.TimeoutException: Did not receive any data in the allotted time.
at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result)
at System.IO.Pipelines.Pipe.GetReadAsyncResult()
at System.IO.Pipelines.PipeReaderStream.ReadAsyncInternal(Memory`1 buffer, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Components.Server.Circuits.RemoteJSDataStream.ReadAsync(Memory`1 buffer, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Components.Forms.BrowserFileStream.CopyFileDataIntoBuffer(Int64 sourceOffset, Memory`1 destination, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Components.Forms.BrowserFileStream.ReadAsync(Memory`1 buffer, CancellationToken cancellationToken)
at System.IO.Stream.<CopyToAsync>g__Core|29_0(Stream source, Stream destination, Int32 bufferSize, CancellationToken cancellationToken)
at System.Net.Http.StreamToStreamCopy.<CopyAsync>g__DisposeSourceAsync|1_0(Task copyTask, Stream source)
at System.Net.Http.HttpContent.<CopyToAsync>g__WaitAsync|56_0(ValueTask copyTask)
at System.Net.Http.MultipartContent.SerializeToStreamAsyncCore(Stream stream, TransportContext context, CancellationToken cancellationToken)
at System.Net.Http.HttpContent.<CopyToAsync>g__WaitAsync|56_0(ValueTask copyTask)
at System.Net.Http.HttpContent.<CopyToAsync>g__WaitAsync|56_0(ValueTask copyTask)
at System.Net.Http.HttpConnection.SendRequestContentAsync(HttpRequestMessage request, HttpContentWriteStream stream, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at ~~~~~BlazorApp.Services.AuthHttpClientHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) in ~~~~~BlazorAppServicesAuthHttpClientHandler.cs:line 27
at System.Net.Http.Handlers.ProgressMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
at ~~~~~BlazorApp.Services.FileUploadApiService.PostAndReportProgress(String url, MultipartFormDataContent formData, String guid, EventHandler`1 progressHandler, CancellationToken cancellationToken) in ~~~~~BlazorAppServicesFileUploadApiService.cs:line 106
--- End of inner exception stack trace ---
at ~~~~~BlazorApp.Services.FileUploadApiService.PostAndReportProgress(String url, MultipartFormDataContent formData, String guid, EventHandler`1 progressHandler, CancellationToken cancellationToken) in ~~~~~BlazorAppServicesFileUploadApiService.cs:line 122
at ~~~~~BlazorApp.Services.FileUploadApiService.ChatUploadFileAsync(StreamContent file, Int32 chatId, String guid, EventHandler`1 progressHandler, CancellationToken cancellationToken) in ~~~~~BlazorAppServicesFileUploadApiService.cs:line 59
at ~~~~~BlazorApp.Services.FileUploadManager.UploadFileAsync(FileUploadDto item) in ~~~~~BlazorAppServicesFileUploadManager.cs:line 171
at ~~~~~BlazorApp.Services.FileUploadManager.<StartUploadTask>b__18_0() in ~~~~~BlazorAppServicesFileUploadManager.cs:line 108 

感谢您的样品。我能够复制System.TimeoutException

我无法用此代码重现错误(仅FileUploadManager更改(。你能试试这个吗?

public async void AddFilesToUploadQueue(IReadOnlyList<IBrowserFile> files)
{
lock (fileUploadQueue)
{
foreach (var item in files)
{
fileUploadQueue.Enqueue(item);
}
}
for (int i = 0; i < files.Count; i++)
{              
IBrowserFile? file = null;
lock(fileUploadQueue)
{
fileUploadQueue.TryDequeue(out file);
}
tasks.Add(UploadOneFile(file));
}

//await Task.WhenAll(tasks);
}
protected async Task UploadOneFile(IBrowserFile file)
{
await semaphoreSlim.WaitAsync();

try
{
using (var readStream = file.OpenReadStream(int.MaxValue))
{
var streamContent = fileUploadService.CreateStreamContent(readStream, file.Name, file.ContentType);
var result = await fileUploadService.UploadFileAsync(streamContent);
}
}
finally
{
semaphoreSlim.Release();
}
}

相关内容

  • 没有找到相关文章

最新更新