Tag Archives: .net standard 2.0

.net 4.7.2 Forms Application referencing a .net Standard 2.0 Lib causes System.Data.SqlClient Reference TypeInitializationException

Exception

System.TypeInitializationException
The type initializer for ‘{AStandardNetLibInYourProject}’ threw an exception.

For example, you have an explicit reference to System.Data.SqlClient > 4.5.0.1, either through a Nuget package reference or an Assembly Reference.

The consumed .net Standard 2.0 Library already contains (internally/transitively) a reference to System.Data.SqlClient 4.5.0.1

That binding causes the explicit reference to conflict.

Solution

  1. Remove all explicit references to System.Data.SqlClient 4.6.0 and 4.6.1 in any consuming project as well as the .net std lib itself.
  2. Add the following <RestoreProjectStyle…/> section to the top of your consuming/main Forms Application Project .csproj file using an Editor:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
  </PropertyGroup>
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>

See also: https://stackoverflow.com/questions/49266384/there-is-possible-to-use-packagereference-in-xamarin-android-or-xamarin-ios-pr/49266856
and: https://github.com/xamarin/xamarin-macios/issues/3949

Possible error details in german

=== Zustandsinformationen vor Bindung ===
LOG: DisplayName = System.Data.SqlClient, Version=4.5.0.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)
LOG: Appbase = file:///C:/Workspaces/DWH/DwhApps/Dwh.Cmd.Etl.ImportFile/bin/Debug/
LOG: Ursprünglicher PrivatePath = NULL
Aufruf von Assembly : Dwh.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: Diese Bindung startet im default-Load-Kontext.
LOG: Die Anwendungskonfigurationsdatei wird verwendet: C:\Workspaces\DWH\DwhApps\Dwh.Cmd.Etl.ImportFile\bin\Debug\Dwh.Cmd.Etl.ImportFile.exe.Config
LOG: Die Hostkonfigurationsdatei wird verwendet: 
LOG: Die Computerkonfigurationsdatei von C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config wird verwendet.
LOG: In der Anwendungskonfigurationsdatei wurde eine Umleitung gefunden. 4.5.0.1 wird nach 4.6.1.0 umgeleitet.
LOG: Verweis nach der Richtlinie: System.Data.SqlClient, Version=4.6.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: Download von neuem URL file:///C:/Workspaces/DWH/DwhApps/Dwh.Cmd.Etl.ImportFile/bin/Debug/System.Data.SqlClient.DLL.
WRN: Der Vergleich des Assemblynamens führte zum Konflikt: Nebenversion.
ERR: Das Setup der Assembly konnte nicht abgeschlossen werden (hr = 0x80131040). Die Suche wurde beendet.