If you publish a WCF service using EF to IIS, do not forget to distribute sqlserver.dll manually

I’ve spent several hours this week debugging a WCF application using Entity Framework, that was published to an IIS server, only to have every call to the service fill with the error message “The type initializer for [My]DbContext has caused an exception”. Of course, on the dev machine everything was working fine. Short answer: as the blog title implies, the application was missing EntityFramework.SqlServer.dll in its bin folder, because Visual Studio failed to recognize the fact it is referenced by the application.

Sketching the project: this was about a simple WCF class library (one service, three methods), that uses Entity Framework for its data layer to connect to a Microsoft SQL Server instance. This class library is then wrapped in an as-thin-as-possible IIS module, which is published to IIS using the standard Visual Studio “Publish” wizard.

The class library references EntityFramework.dll and EntityFramework.SqlServer.dll. The IIS module in turn only references the class library. Both the class library and the IIS module will build and run fine in both debug mode and release mode. Both implicitly required files (and a couple more) are copied to the bin folder correctly as needed.

However publishing is different.

Somewhere along the lines, something goes wrong with VS determining the dependencies for the IIS module to publish. When you use the Publish function on the IIS module to package and publish it (it does not matter which distribution method you choose), the publish wizard does recognize the implicit dependency on EntityFramework.dll and correctly packages it with your app. It does not recognize that EntityFramework.SqlServer.dll is also needed, which is consequently absent from the bin folder on the webserver.

There may be some EF internals at work here that I do not know about, because I am a bit mystified by this behavior. I’d expect VS to either publish correctly, or miss both files. But not publish one of them and not the other.

I ended up copying the file to the web server manually, which then worked immediately.


Over Shiftkey
I am Maarten, owner of and chief software developer for Shiftkey software development from The Netherlands. I will be writing mainly about things I run into when programming in C# or Delphi.

Geef een reactie

Vul je gegevens in of klik op een icoon om in te loggen.

WordPress.com logo

Je reageert onder je WordPress.com account. Log uit / Bijwerken )


Je reageert onder je Twitter account. Log uit / Bijwerken )

Facebook foto

Je reageert onder je Facebook account. Log uit / Bijwerken )

Google+ photo

Je reageert onder je Google+ account. Log uit / Bijwerken )

Verbinden met %s

%d bloggers liken dit: