Core: Connection string issue with PublishSingleFile flag from shared project.

Created on 24 Sep 2019  ·  15Comments  ·  Source: dotnet/core

Issue Title

SQL server connection string not working when compiling application with command: dotnet publish /p:PublishSingleFile=true and connection string is in shared project

General

I have a worker project which has SQL related code in Shared project. Everything is working fine when running code from Visual Studio or compiling code without /p:PublishSingleFile=true flag. But with PublishSingleFile flag i get error when running application:

Unhandled exception. System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SqlConnection' threw an exception.
 ---> System.InvalidOperationException: Failed to read the configuration section for enclave providers. Make sure the section is correctly formatted in your application configuration file. Error Message: Configuration system failed to initialize
 ---> System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize
 ---> System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: Could not load type 'System.ComponentModel.AsyncCompletedEventArgs' from assembly 'System.Net.WebHeaderCollection, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. (C:\Users\user\AppData\Local\Temp\.net\app\5juw15bs.4n3\Config\machine.config)
 ---> System.TypeLoadException: Could not load type 'System.ComponentModel.AsyncCompletedEventArgs' from assembly 'System.Net.WebHeaderCollection, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
   at System.Configuration.ClientConfigurationHost.OpenStreamForRead(String streamName)
   at System.Configuration.Internal.DelegatingConfigHost.OpenStreamForRead(String streamName)
   at System.Configuration.ImplicitMachineConfigHost.OpenStreamForRead(String streamName)
   at System.Configuration.BaseConfigurationRecord.InitConfigFromFile()
   --- End of inner exception stack trace ---
   at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
   at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
   at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
   at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
   --- End of inner exception stack trace ---
   at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
   at System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(String sectionName)
   at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at Microsoft.Data.SqlClient.SqlConnection..cctor()
   --- End of inner exception stack trace ---
   at Microsoft.Data.SqlClient.SqlConnection..cctor()
   --- End of inner exception stack trace ---
   at Microsoft.Data.SqlClient.SqlConnection..ctor(String connectionString)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.CreateDbConnection()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.get_DbConnection()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenAsync(CancellationToken cancellationToken, Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.<>c__DisplayClass20_0.<<ExistsAsync>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureCreatedAsync(CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureCreatedAsync(CancellationToken cancellationToken)
   at app.Worker.ExecuteAsync(CancellationToken stoppingToken)
   at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
   at app.Program.Main(String[] args)

Connection string used: (@"Server=DB;Database=db;Integrated Security=true;"); or with username and password combination: (@"Server=DB;Database=db;User Id=user;Password=Password1;");

Dotnet info:

.NET Core SDK (reflecting any global.json):
 Version:   3.0.100
 Commit:    04339c3a26

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.18985
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\3.0.100\

Host (useful for support):
  Version: 3.0.0
  Commit:  7d57652f33

.NET Core SDKs installed:
  2.1.801 [C:\Program Files\dotnet\sdk]
  2.1.802 [C:\Program Files\dotnet\sdk]
  2.2.401 [C:\Program Files\dotnet\sdk]
  3.0.100-rc1-014190 [C:\Program Files\dotnet\sdk]
  3.0.100 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0-rc1.19457.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0-rc1-19456-20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.0.0-rc1-19456-20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Most helpful comment

I can confirm this was caused by publishing with -p:PublishTrimmed=true and removing it solved the issue.

PublishTrimmed seems to buggy in ASP.Net Core 3.1

All 15 comments

@diego @ajcvickers for possible help on this one.

Please follow similar issue being discussed at: https://github.com/dotnet/SqlClient/issues/211
OR log a new issue at: https://github.com/dotnet/SqlClient/issues/

I don't believe the error is specific to SqlClient. There is probably an error in one of your config files (missing root element or something basic). Validate the machine.config at C:\Users\user\AppData\Local\Temp.net\app\5juw15bs.4n3\Config\machine.config (or wherever that is generated from in your error). SqlClient just happens to be the first thing looking for config information in your stack.

Hi,

Sorry didn't do enough of testing with different flags. Seems that it also needs /p:PublishTrimmed=true flag

I created minimal repro repo https://github.com/Jeremiad/Issue-3447-repro where this happens after building Worker project with command: dotnet publish -r win-x64 -c Release /p:PublishSingleFile=true /p:PublishTrimmed=true and running application.

If you add a direct dependency on System.Configuration.ConfigurationManager to the Shared project in the repro, the error goes away. System.Configuration.ConfigurationManager is already a dependency of SqlClient so this doesn't seem like a SqlClient issue to me but perhaps something in the .NET SDK.

This happens to me in a completely standalone netcoreapp3.0 project, without any external references, compiled for linux-x64. If i trim it, I get the same error as in OP:

Could not load type 'System.ComponentModel.AsyncCompletedEventArgs' from assembly 'System.Net.WebHeaderCollection, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

Ping @David-Engel . Any alternative ideas?

Figure out where the invalid app.config or machine.config is and fix it. I don't have any insight into where else those might be hiding in the above scenarios. Further error details (like the full stack trace in a previous comment) often provides a path.

If you add a direct dependency on System.Configuration.ConfigurationManager to the Shared project in the repro, the error goes away. System.Configuration.ConfigurationManager is already a dependency of SqlClient so this doesn't seem like a SqlClient issue to me but perhaps something in the .NET SDK.

Given this and the recurring theme in two of the comments above of using the experimental package trimming feature in .NET Core, it sure _sounds_ like an as "simple" case as the package trimmer not detecting this dependency.

The reason it's still (as of January 2020 and .NET Core 3.1) an _experimental feature_ is specifically that it sometimes, like when dependency injection is used, doesn't detect all dependencies even if the compiler and runtime have no problems with it and then too much is trimmed, causing all sorts of trouble.

The "fix" for now is to explicitly list the reference like above even if one shouldn't need to and it feels a bit wrong.

I can confirm this was caused by publishing with -p:PublishTrimmed=true and removing it solved the issue.

PublishTrimmed seems to buggy in ASP.Net Core 3.1

@kofifus
Same here, just happenned:

Unhandled exception. System.TypeLoadException: Could not load type 'System.ComponentModel.AsyncCompletedEventArgs' from assembly 'System.Net.WebHeaderCollection, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
   at Cero.CommandLine.CliSections..ctor(String[] args)
   at Cero.Program.Main(String[] args)
fish: “cero project qtile” terminated by signal SIGABRT (Abort)

Debian SiD 11
.Net Core 3.1.300

I can confirm the same issue on .Net Core 3.1.300 under Windows 10. Manually adding a reference to System.Configuration.ConfigurationManager did seem to resolve it.

I faced the same issue and after some trial and error attempts and reading the docs about Prevent assemblies from being trimmed, I solved my issue simply by upgrading the Microsoft.Data.SqlClient to version 2.0.0-preview4.20142.4 (at the time of writing).

Hoping this helps someone.

Edit: I'm also targeting .Net Core 3.1.300

I face the same issue when I was using WebClient in a .net core 3.1 console app like this:
WebClient webClient = new WebClient(); webClient.DownloadFile(myUri, @"myfilename");

After adding the nuget for System.Configuration.ConfigurationManager the problem resolved

I face the same issue when I was using WebClient in a .net core 3.1 console app like this:
WebClient webClient = new WebClient(); webClient.DownloadFile(myUri, @"myfilename");

After adding the nuget for System.Configuration.ConfigurationManager the problem resolved

This worked for me as well. My problem was not just the trimmed option, but being unable to use the "produce single file" option at all. In this project I was using the webclient and not the SQLClient... but I have had the problem with the trimmed option as well in a project that uses the SQL client. I will try adding this Nuget to that project as well and see if that helps.

Was this page helpful?
0 / 5 - 0 ratings