Compiling a .Net DLL for a medium trust environment
Recently I wrote a little utility for a client using the excellent Html Agility Pack to read and navigate through a HTML page, selecting the data that was needed and parsing it - basically a screen scrape. I downloaded the source, compiled it, added a reference to the dll in my project and tapped away for a few minutes and et voila, within a few minutes a working screen scrape. A fantastic library.
On uploading the project to my GoDaddy web hosting however, I encountered a problem. You see, my hosting is a shared hosting environment, and like most such webhosting environments is set to a Medium Trust level for .Net applications. As MS dryly puts it:
Applications that receive less than full trust by the runtime code
access security system are not allowed to call shared managed libraries
unless the library writer specifically allows them to through the use
Therefore, application writers must be aware that some libraries will
not be available to them from a partially trusted context.
The solution, although slightly confusing from the MS documentation, is actually very simple. I opened the HtmlAgilityPack source code, and edited the AssemblyInfo.cs file. Firstly, add a reference to the AllowPartiallyTrustedCallersAttribute:
Since AllowPartiallyTrustedCallersAttribute is part of the System.Security namespace, we must add a reference at the top of the page:
I then rebuilt the project, rebuilt the web project and it now works like a charm.