As suggested above, the issue was the trust level. In an ASP.Net application (Visual Web Developer 2012), I needed to add the following to the <system.web> section of the web.config file:
<trust level="Full"/>
That fixed the error (or stopped it from happening).