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.

Shiftkey Avatar

Published by

2 reacties op “If you publish a WCF service using EF to IIS, do not forget to distribute sqlserver.dll manually”

  1. Pyracell Avatar

    Thank you sooo very much!
    I’ve been struggling with this issue for a day now. Copying the file manually did the trick.
    Bitchy error to debug.

    Cheers.

    1. Shiftkey Avatar

      You’re welcome, glad it was of help!

Plaats een reactie