Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when serializing IAsyncEnumerable in .NET 6 #155

Open
silkfire opened this issue Nov 16, 2021 · 7 comments
Open

Error when serializing IAsyncEnumerable in .NET 6 #155

silkfire opened this issue Nov 16, 2021 · 7 comments

Comments

@silkfire
Copy link

I've had a piece of code that serializes an IAsyncEnumerable since this type was introduced in .NET Core 3.1 and it's been working great together with SpanJson.

Recently, I upgraded to the newly released .NET 6 and after testing my application I got a long stack of errors at the location that serializes this type.

Would you mind checking what's causing the issue?

ArgumentException: Type 'System.Runtime.InteropServices.StructLayoutAttribute' does not have a default constructor (Parameter 'type')
System.Linq.Expressions.Expression.New(Type type)
SpanJson.Formatters.ComplexFormatter.BuildDeserializeDelegate<T, TSymbol, TResolver>()
SpanJson.Formatters.ComplexClassFormatter<T, TSymbol, TResolver>..cctor()

System.RuntimeFieldHandle.GetValue(RtFieldInfo field, object instance, RuntimeType fieldType, RuntimeType declaringType, ref bool domainInitialized)
System.Reflection.RtFieldInfo.GetValue(object obj)
SpanJson.Resolvers.ResolverBase.GetDefaultOrCreate(Type type)
SpanJson.Resolvers.ResolverBase<TSymbol, TResolver>.BuildFormatter(Type type)
SpanJson.Resolvers.ResolverBase<TSymbol, TResolver>.<GetFormatter>b__3_0(Type x)
System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue>.GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
SpanJson.Resolvers.ResolverBase<TSymbol, TResolver>.GetFormatter(Type type)
SpanJson.Resolvers.ResolverBase<TSymbol, TResolver>.GetFormatter(JsonMemberInfo memberInfo, Type overrideMemberType)
SpanJson.Formatters.ComplexFormatter.BuildSerializeDelegate<T, TSymbol, TResolver>()
SpanJson.Formatters.ComplexClassFormatter<T, TSymbol, TResolver>..cctor()

(The chain of errors is much longer than this but looks very similar to above)

@Tornhoof
Copy link
Owner

Honestly I'm surprised that IAsyncEnumerable works at all.

@silkfire
Copy link
Author

silkfire commented Nov 17, 2021

Oh, why is that?

A clarification, this was actually the return type of an action in ASP.NET Core. It used to serialize it as a regular IEnumerable<>, but doesn't work in .NET 6.

@Tornhoof
Copy link
Owner

IAsnycEnumerable only works properly if you have a full async pipeline. I guess that in 5.0 it was actually sync or fully synchronously buffered and in 6.0 it's not anymore.

@silkfire
Copy link
Author

silkfire commented Nov 17, 2021

Okay, I see then. I converted it to a Task<ActionResult<IEnumerable<>>> to make it work. The error occurs when SpanJson is scanning and reflecting the controller actions to determine how to serialize each respective response.

@Tornhoof
Copy link
Owner

It would be quite useful if you could provide a repro for your problem, this problem is likely dependent on one of the concrete types.

@silkfire
Copy link
Author

silkfire commented Nov 17, 2021

Just create an ASP.NET Core project with a controller containing an action with the return type IAsyncEnumerable<> targeting .NET 6, and you should hit the error as soon as you launch the app.

@Tornhoof
Copy link
Owner

Okay, you still return IAsnycEnumerable, I was confused as you wrote about Ienumerable in between.
As I previously said, this can't work properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants