Load rss (xml) with Linq to XML on medium trust hosting

For a new project I needed to implement show data from RSS. The site will be hosted on a shared hosting environment with medium trust.

I load the rss with the following code:

[code:c#]

XElement rssFeed = XElement.Load("[REPLACE WITH URL TO RSS]");
var posts = from item in rssFeed.Descendants("item")
                  select new
                  {
                       Title = item.Element("title").Value,
                       Description = item.Element("description").Value
                  };

[/code]


This normally works fine for me… But apparently not with medium trust :-(! I got the following error.

[code:c#]

Security Exception

Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

[/code]


After reading a bit and playing with the web.config setting <TRUST /> I found the solution. The solution is putting a ".*" in the originUrl parameter of the trust node.

[code:c#]

<trust level="Medium" originUrl=".*" />

[/code]


So an easy solution for a annoying problem what took me to much time. So I hope this post will help somebody out there 🙂

Linq to Sql: Change connectionstring to load from Web.config

Update 20 may 2009: New easy sollution

Set the Connection -> Application Settings property True. This will generate a connection string in your app.config.

 

Copy this connection string to your web.config and your all set!

 

================================================================= 

Old post: 

If you want to use your connectionstring from the web.config with Linq to Sql (dbml) you have to add the following partial class to your project:

[code:c#]

namespace Your.Namespace
{
  partial class yourDataContext
  {
    partial void OnCreated()
    {
       ConnectionStringSettings s = ConfigurationManager.ConnectionStrings["YourConnectionString"];
       if (s != null)
         Connection.ConnectionString = s.ConnectionString;
    }
  }
}

[/code]

Don't forget to change the Your.Namespace, yourDataContext and YourConnectionString to fit your project.

Hope this helps.