不知道是否有人遇到这个问题。

我有一个非常简单的ServiceStack服务

public VersionResponse Get(VersionRequest request) {    Assembly assembly = Assembly.GetExecutingAssembly();    FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);    return new VersionResponse() { Version = fvi.FileVersion }; } 

像这样在服务栈中注册

appHost.Routes.Add<VersionRequest> ("/api/version"); appHost.RegisterService<CoreService>(); 

这一直一直有效,但是在升级到.NET Core 3.0并更改为根据Microsoft的说明使用Host Builder之后,当ServiceStack尝试将响应序列化为JSON或其他方式时,它将引发异常。

这是我们在后端遇到的错误(对图像表示歉意,但我认为很明显,在ServiceStack + .NET Core 3.0集成中有些问题)

fail: ServiceStack.HttpResponseExtensionsInternal[0]       Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true instead. System.InvalidOperationException: Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true instead.    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponseStream.Write(Byte[] buffer, Int32 offset, Int32 count)    at System.IO.Compression.DeflateStream.WriteDeflaterOutput()    at System.IO.Compression.DeflateStream.WriteCore(ReadOnlySpan`1 buffer)    at System.IO.Compression.DeflateStream.Write(Byte[] array, Int32 offset, Int32 count)    at System.IO.Compression.GZipStream.Write(Byte[] array, Int32 offset, Int32 count)    at Microsoft.AspNetCore.ResponseCompression.ResponseCompressionBody.Write(Byte[] buffer, Int32 offset, Int32 count)    at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)    at System.IO.StreamWriter.Flush()    at ServiceStack.Text.JsonSerializer.SerializeToStream(Object value, Type type, Stream stream) in C:\BuildAgent\work\912418dcce86a188\src\ServiceStack.Text\JsonSerializer.cs:line 181    at ServiceStack.Text.JsonSerializer.SerializeToStream[T](T value, Stream stream) in C:\BuildAgent\work\912418dcce86a188\src\ServiceStack.Text\JsonSerializer.cs:line 174    at ServiceStack.Serialization.JsonDataContractSerializer.SerializeToStream[T](T obj, Stream stream) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack.Client\Serialization\JsonDataContractSerializer.cs:line 64    at ServiceStack.Host.ContentTypes.<>c.<.ctor>b__36_0(IRequest r, Object o, Stream s) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Host\ContentTypes.cs:line 20    at ServiceStack.Host.ContentTypes.<>c__DisplayClass30_0.<GetStreamSerializerAsync>b__0(IRequest httpReq, Object dto, Stream stream) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Host\ContentTypes.cs:line 273    at ServiceStack.HttpResponseExtensionsInternal.WriteToResponse(IResponse response, Object result, StreamSerializerDelegateAsync defaultAction, IRequest request, Byte[] bodyPrefix, Byte[] bodySuffix, CancellationToken token) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\HttpResponseExtensionsInternal.cs:line 323 

有这个问题的人吗?

===============>>#1 票数:2 已采纳

ServiceStack在v5.7中增加了与.NET Core 3的兼容性 一个主要修复似乎与之相关:

默认情况下禁用同步写入

支持.NET Core 3的主要问题是适应默认情况下禁用同步流写入的决定,这实际上使大多数.NET序列化器无法直接写入Response OutputStream。 要解决此问题,在.NET Core 3中,首先将所有同步序列化器写入池化的MemoryStream,然后再异步写入响应的输出流。

基本上,ServiceStack中使用的除HTML View Engine(Razor / Markdown / JSON报告)和#Script Pages(从零开始编写,以支持异步写入)之外的所有Content Type Serializer(即序列化格式)目前都已存储在.NET Core 3中。 (我们将在将来的版本中研究将本机异步序列化支持扩展到我们自己的序列化器)。

.NET Core 3确实允许您根据每个请求关闭此限制,可以通过关闭同步序列化器的缓冲来控制该限制,方法是:

SetConfig(new HostConfig {     BufferSyncSerializers = false, }) 

这会将.NET Core 3的现有行为还原为直接序列化到输出流并使用AllowSynchronousIO = true标记请求。

这也是异常所谈论的:

Synchronous operations are disallowed. Call WriteAsync or  set AllowSynchronousIO to true instead. 

  ask by Yannis translate from so

本文未有回复,本站智能推荐: