The type initializer for 'Microsoft.Data.SqlClient.SNINativeMethodWrapper' threw an exception

You have just deployed a new application that you have written and it all works on your machine, but as soon as you run it you get:

The type initializer for 'Microsoft.Data.SqlClient.SNINativeMethodWrapper' threw an exception

Check if you have an x64 and x32 folder in your bin or the same folder as your executable (.exe).  These folders each has a SNI.dll and SNI.pdb and the folders and at least the SNI.dll should be included in your deployments.

In my case, I use Octopus Deploy and more specifically OctoPack to package the applications for deployment.  To make OctoPack pick this up I had to make the following change:

  • Add a nuspec file to the project, and 
  • Add a file element under the files element:
    • For a web api:
      • <file src="bin\x64\*.*" target="bin\x64\" />
      • <file src="bin\x32\*.*" target="bin\x32\" />
    • For a console app/windows service:
      • <file src="bin\x64\*.*" target="x64\" />
      • <file src="bin\x32\*.*" target="x32\" />
  • Add /p:OctopackEnforceAddingFiles=true to my msbuild task that builds the projects

Comments

Popular Posts