{{'2018-01-19T10:21:29.0588206Z' | utcToLocalDate }}
Richie Lee

Weird “type initializer” Exception When Loading Dacpac


A team at ne of our clients recently got in touch with me with regards to an exception being thrown when they were deploying a database. For the sake of clarity I have replicated the script below -

And this is the output with errors-

Following the output we can see that the dac dll, dacpac and publish.xml files are all present as the script tries to load them. And whilst the dac dll is loaded successfully the dacpac and publish xml fail as exceptions are thrown at lines 26 and 31 or the script. Ignore the fact that the output states that dacpac and publish profile are loaded (lines 12 and 20), this is just because the script doesn’t attempt to load the files in a try/catch and just continues.

So, what is going on here? The exception is not too helpful -  “The type initializer for 'Microsoft.SqlServer.Dac.DacPackage' threw an exception”. But after much investigation we came to the conclusion that we did not have .NET 4.6 installed on the box, which was the target version for the dacpac. So upgrading to .NET 4.6 resolved the issue.

Because this is part of a deployment pipeline, perhaps one method to prevent issues like this happening is to check what versions of required software, such as .NET is installed. Below is a script that does such a thing -

But this kind of thing works where there are very few dependencies – checking software versions is harder where there is a large number of them. Another solution is for people to use something like DSC/Chef to manage required software, or use NuGet to install required packages. Again, this works in theory but requires users to continue the practice.

comments powered by Disqus