Skip to content

Fix | Enable serialization of SqlException - #4622

Open
edwardneal wants to merge 2 commits into
dotnet:mainfrom
edwardneal:fix/sqlexception-serialization
Open

edwardneal wants to merge 2 commits into
dotnet:mainfrom
edwardneal:fix/sqlexception-serialization

Conversation

@edwardneal

@edwardneal edwardneal commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Description

This fixes two longstanding bugs when serializing a SqlException using .NET Framework's SerializableAttribute-based infrastructure - via DataContractSerializer, BinaryFormatter or SoapFormatter.

The underlying bug was simply that we weren't providing a value for the Errors collection in GetObjectData, or reading it back in the deserialization constructor. Since properties such as SqlException.Number just read from the first entry in Errors, populating that collection automatically lights up those property values.

The broader picture here is that there are a few separate requests for changes to SqlException - issues #648 and #649 request SqlState and IsTransient support, issue #35 requests a public-facing constructor. Before adding new fields, I want to be sure that the underlying type serializes and deserializes correctly.

Issues

Fixes #1940.
Fixes #2150.

Testing

New unit test added to highlight regressions.

This new test uses BinaryFormatter, which is a bad practice in user code: it's known to be insecure when used with untrusted payloads. The alternative (DataContractSerializer) doesn't exactly mimic the original use case of .NET Framework Remoting though - DataContractSerializer requires a list of known types. I don't believe that the test introduces a security bug (we've got full control of the payloads we serialize and deserialize) but I'm not sure whether it'll be flagged as a risk.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

paulmedynski
paulmedynski previously approved these changes Sep 29, 2026
@paulmedynski

Copy link
Copy Markdown
Contributor

/azp run

@paulmedynski
paulmedynski requested a balanced review from Copilot September 29, 2026 13:31
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The new test class and method lack required XML summary documentation.

Review effort: Balanced
Findings: 1 Low severity

Open (1)
What changed in this PR

Fixes SqlException serialization so SQL error details survive .NET Framework formatter round trips.

Changes:

  • Serializes and restores SqlErrorCollection.
  • Adds a BinaryFormatter regression test.
File Description
SqlException.cs Persists errors during serialization.
SqlExceptionTests.cs Tests serialized exception state.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@paulmedynski

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

Error info is lost during serialization SqlException.Number is always 0 when using .NET framework remoting

5 participants