This is the reason we ran into this issue, it was simple and had nothing to do with the code, it's one of those "things to keep in mind" LOL.
We've got a web app in a virtual directory within the SharePoint site and a team member was trying to access it using the domain that was pointing to the site (http://portal.mydomain.com/myapp) and this was throwing the exception.
When the app was accessed via the server name, it worked (http://SERVERNAME/myapp).
The reason was that the SharePoint site was created as SERVERNAME and not portal.mydomain.com and the app was looking up the SharePoint site based on the current url (portal.mydomain.com) which did not exist.
// The SharePoint portal URI of the root site.
var portalUrl = new Uri(Request.Url.GetLeftPart(UriPartial.Authority));
// The SharePoint sub site.
var portalSite = Request.QueryString["site"];
.
.
.
// Instantiate the SharePoint website object located at [portalUrl]/[portalSite].
SPWeb mySite = SPWebApplication.Lookup(portalUrl).Sites[portalSite].AllWebs[portalSite];