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

Fix S3925 FP: serialization infrastructure breaking change in .NET 8 #8065

Closed
IeuanWalker opened this issue Sep 25, 2023 · 6 comments
Closed
Labels
Area: C# C# rules related issues. Area: C#12 C#12 related issues Type: False Positive Rule IS triggered when it shouldn't be.

Comments

@IeuanWalker
Copy link

I have this custom exception -

[Serializable]
public class CalendarDateChangedException : Exception
{
	public CalendarDateChangedException()
	{
	}

	public CalendarDateChangedException(string? message) : base(message)
	{
	}

	public CalendarDateChangedException(string? message, Exception? innerException) : base(message, innerException)
	{
	}

	protected CalendarDateChangedException(SerializationInfo info, StreamingContext context) : base(info, context)
	{
	}
}

Retarged the application from .net 7 to 8, i get this warning -

Warning	SYSLIB0051	'Exception.Exception(SerializationInfo, StreamingContext)' is obsolete: 'This API supports obsolete formatter-based serialization. It should not be called or extended by application code.' (https://aka.ms/dotnet-warnings/SYSLIB0051)	

Removing the last method, i then get this warning -

Warning	S3925	Update this implementation of 'ISerializable' to conform to the recommended serialization pattern.
@duncanp-sonar duncanp-sonar transferred this issue from SonarSource/sonarlint-visualstudio Sep 25, 2023
@cristian-ambrosini-sonarsource cristian-ambrosini-sonarsource changed the title S3925: .NET 8 warnings Fix S3925 FP: serialization infrastructure breaking change in .NET 8 Sep 26, 2023
@cristian-ambrosini-sonarsource cristian-ambrosini-sonarsource added Type: False Positive Rule IS triggered when it shouldn't be. Area: C# C# rules related issues. Area: C#12 C#12 related issues labels Sep 26, 2023
@cristian-ambrosini-sonarsource
Copy link
Contributor

Hi @IeuanWalker!

I think the warning that you are experiencing is caused by this .Net 8 breaking change. Do you need your custom exception type to be serializable? If not, you can follow the recommended action (see "If you're creating a custom System.Exception-derived type" section), and remove the [Serializable] attribute, this way S3925 should not raise.

@IeuanWalker
Copy link
Author

thanks @cristian-ambrosini-sonarsource, thats good to know 🙂

@Wouter29
Copy link

This is not working @cristian-ambrosini-sonarsource
If I make a custom Exception like this

    public class CustomException : Exception
    {
    }

It still triggers a "S3925:"ISerializable" should be implemented correctly" that only can be solved by adding a suppression.

@cristian-ambrosini-sonarsource
Copy link
Contributor

Hi @Wouter29! We are starting a dedicated effort over the next 2 weeks to address serialization rules post-Net8 (see #8377). Please follow the mentioned issue to stay updated on the progress.

cheers!

@cristian-ambrosini-sonarsource cristian-ambrosini-sonarsource removed their assignment Jan 17, 2024
@martin-strecker-sonarsource
Copy link
Contributor

The [Serializable] attribute applied in the code example is an indication, that binary formatting support is intended. Therefore S3925 is raised if the serialization constructor is missing.
You need to remove the [Serializable] attribute and the serialization constructor from the type.
See also #7673

@AlexEngblom
Copy link

Cannot get rid of warning S3925 with these recommendations (also documented here https://learn.microsoft.com/en-us/dotnet/fundamentals/syslib-diagnostics/syslib0051).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Area: C#12 C#12 related issues Type: False Positive Rule IS triggered when it shouldn't be.
Projects
None yet
Development

No branches or pull requests

5 participants