-
Notifications
You must be signed in to change notification settings - Fork 471
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
InvalidProgramException
when proxying MemoryStream
with .NET 7
#651
Comments
Note: I just checked, even without any interceptors the program fails. |
Here is the full repro VS 2022 solution: https://www.dropbox.com/s/4evu8rm636jv8h8/ReproInvalidProgramExceptionXmlReader.zip?dl=0 |
I haven't looked at the repro solution yet, but I suspect this will come down to .NET 7's |
I don't think we've got any tests for In general, I think proxying a |
@stakx we did it again. Only saw your comment as I clicked the green button 😆 |
Note that also on pre .NET 7 The following program throws an "InvalidProgramException: Cannot create boxed ByRef-like values." on .NET 3.1, 5 and 6 ... and "InvalidProgramException: Common Language Runtime detected an invalid program." on .NET 7 (actually the old exception message was better 🤦). var proxyGenerator = new ProxyGenerator();
using var stream = new MemoryStream(Encoding.UTF8.GetBytes("some text"));
var proxiedStream = proxyGenerator.CreateClassProxyWithTarget(stream, new MyInterceptor());
Span<byte> spanBuffer = stackalloc byte[20];
proxiedStream.Read(spanBuffer);
Console.WriteLine(Encoding.UTF8.GetString(spanBuffer));
class MyInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation) => invocation.Proceed();
} I think the reason why @drauch 's repro worked in pre .NET 7 is that they probably switched from the byte-array overload to the |
@ulrichb Ah, thanks, that makes it clear why it fails in .NET 7 and didn't fail before. @stakx @jonorossi So what you're saying is that we cannot use a proxy for a Stream at all basically? It'd be unfortunate, because we have a "whatever you call on this stream instance, if an XyzException is thrown, translate it to AbcException"-necessity (in reality it's not a MemoryStream, I just used the MemoryStream for the repro sample). Our only other option is basically to write a decorator and decorate each method manually and make sure via a test that we really have all methods decorated :-/ Best regards, |
I'll take a look at our test suite. Right now it would appear that code generation succeeds despite the invalid attempt to box a I don't see any way to support spans, at this time I can think of a few workarounds / mitigations.
|
@drauch, I still haven't looked at the repro code, nor have I recently tried to create a
|
It's a bit unfortunate, because I don't even touch the parameters in my IInterceptor, they only need to be passed on to the wrapped Stream. But I guess it's hard to integrate that in the existing API. Thank you for you quick replies. |
Proposal: A new
The last option is the most expensive one and therefore could be added later. |
@ulrichb, understood. Note that ideally, any such new option should cover all problematic (by-ref-like?) types, not just |
I agree that (I came here as a FakeItEasy user, which uses castle under the hood.) |
I've opened a new issue to discuss and plan support for by-ref-like types; see #663. |
InvalidProgramException
when proxying MemoryStream
with .NET 7
The following repro sample unfortunately fails with the .NET SDK 7.0.200 (it worked like a charm with .NET 6).
I'm using Castle.Core 5.1.1.
The exception is:
Please fix this soon, it prevents us from upgrading to .NET 7.
Best regards,
D.R.
The text was updated successfully, but these errors were encountered: