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

Exception serialization with .NET Core 2.0? #5018

Closed
SebastianStehle opened this issue Sep 23, 2018 · 3 comments
Closed

Exception serialization with .NET Core 2.0? #5018

SebastianStehle opened this issue Sep 23, 2018 · 3 comments

Comments

@SebastianStehle
Copy link
Contributor

Hi,

I use Orleans since one of one of the earliest alphas for 2.0 with .NET Core and I think that I never had a problem with exceptions, although I am not sure.

Recently we discovered a bug where validation details where not displayed correctly in the UI. It does not happen very often because there is also a lot of client side validation. Validation errors are transported with a custom validation exception, something like:

[Serializable]
class ValidationException : Exception
{
   LIst<ValidationError> Details;
}

Details was always Empty and the reason was that our serialization logic was not correct. So we fixed it: https://github.com/Squidex/squidex/blob/master/src/Squidex.Infrastructure/ValidationException.cs#L53

The docu says:

Fallback Serialization: .NET Core uses the ILBasedSerializer by default, whereas .NET 4.6 uses BinaryFormatterSerializer by default.

But obviously it cannot be correct for exceptions and I am not sure if this behavior has been changed recently.

Futhermore the documentation says nothing about exceptions and serialization.

Some clarification would be great.

@ReubenBond
Copy link
Member

The documentation is out-of-date. That's my fault.

ILBasedSerializer was developed as an option for .NET Core when BinaryFormatterSerializer was not going to be enabled, but before we flicked the switch on Orleans 2.0, we changed the default to make it BinaryFormatterSerializer on all platforms (primarily to keep as much compatibility as possible).

I've considered whether it would be worth removing ILBasedSerializer. There's no real benefit to removing it now, but if we revise the internal serializer architecture in future (which is likely) then it might not be worth bringing ILBasedSerializer along for the ride immediately (or at all, we'll see).

So, to your question: what do you do when you want a custom exception to be serializable? The answer is that you should implement ISerializable. That means adding a serialization constructor and implementing GetObjectData. See here as an example:

public class OrleansTransactionAbortedException : OrleansTransactionException

@ReubenBond
Copy link
Member

I opened #5019 with another example and updates to the docs - please feel free to comment

@sergeybykov
Copy link
Contributor

Fixed via #5019.

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

No branches or pull requests

3 participants