Skip to content

SqlConnection crashing in distribution msi, runs fine otherwise #1362

@FletcherJohnsonZ

Description

@FletcherJohnsonZ

Describe the bug

I have an application that uses the serilog and serilog-sinks-mssqlserver libraries for logging.

The MsSqlServer library works great when built and run via Visual Studio. It is using Microsoft.Data.SqlClient.

I generate a .msi file using the MS installer project (https://marketplace.visualstudio.com/items?itemName=VisualStudioClient.MicrosoftVisualStudio2017InstallerProjects&ssr=false#overview)

It is then installed (on my computer or any other Windows 10). The application runs as expected except there is no logging to the SQL Serilog database. All other database updates work as expected.

After much research, I found when I changed the serilog-sinks-mssqlserver to library to use System.Data.SqlClient, then everything worked as expected.

Using Microsoft.Data.SqlClient, the following crashes:

_sqlConnection = new SqlConnection(); 
var stateRef = _sqlConnection.State; // This returns a NullReferenceException

If I look at _sqlConnection when debugging the .exe in VS 2019, most of the fields show "xxx threw an exception of NullReferenceException" (see below for screen shot.) This was the state as soon as the first line executed.

Attempts using either of the following also resulted in NullReferenceExceptions when the properties were accessed.
_sqlConnection = new SqlConnection(validConnectionString); // This executed, but the connection string was null exception
_sqlConnection.Connection = validConnectionString; // This executed, but the connection string was null exception

This was reported on StackOverflow: https://stackoverflow.com/questions/69591896/serilog-wpf-based-app-works-but-not-when-installed-as-an-exe/69637676#69637676

There is a very complete item with all the code, etc.

To reproduce

I am trying to ship the software and don't have time to create a fully self contained application that demonstrates the issue. I am noting it here more in case someone else has a similar issue.

It may be my code, something in one of the other projects that make up the solution, etc. I just don't have time to fully test.

Expected behavior

That an instance of SqlConnection would be created without NullExceptionErrors (having null values would be fine, but any attempt to even check the properties results in exceptions, not null values.

Further technical details

Microsoft.Data.SqlClient version: (found on the nuget or Microsoft.Data.SqlClient.dll)
.NET target: .Net Framework 4.7.2
SQL Server version: SQL Server 2017
Operating system: Windows 10

Additional context
I loaded the serilog sink source into my project, then made sure all the NuGet packages were completely up to date. That did not fix the problem.

This is how _sqlConnection shows immediately after _sqlConnection = new SqlConnection(); is executed.

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    External 🔗Issue is in an external component

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions