MySql with entity framework under partial trust hosting

I have a few websites like this blog hosted at a site where my web application must be able to run under partial trust and other limitations as I share the server with many other websites. It would be way to expensive to use dedicated hosting as my sites are non-commercial.

I am now studying to take a couple of exams and decided to come up with a few ideas and try to realize them. One idea was a link shortener and when I deployed an alpha version to my hosting provider I started to run into all sorts of issues. I had it already running well in Cassini, the development webserver for Visual Studio, using MySQL backend and entity framework under .NET 3.5.

First of all it was the issue that later versions of the .NET Connector for MySQL requires full trust and that is not about to happen. All used assemblies must define that it allows to be called in a partial trust scenario. The specific assembly that was the culprit was MySql.Data.Entity.dll. I downloaded the source for the connector and added it to my solution. Then I was able to add the line to allow partial trust callers. In AssemblyInfo.cs I added the following:

using System.Security;
[assembly: AllowPartiallyTrustedCallers()]

Now I ran into another issue, the DbProviderFactory no longer had a public key and I had to adjust it in the web.config file. Just replace the key with null.

<add name=”MySQL Data Provider” invariant=”MySql.Data.MySqlClient” description=”.Net Framework Data Provider for MySQL” type=”MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.3.6.0, Culture=neutral, PublicKeyToken=null” />

I yet again deployed it and tried to run into another security exception. This time in the MySqlTrace class. It is a set of static methods to help trace runtime information. I don’t need it and noticed that by commenting out all code in the constructor it would be disabled.

static MySqlTrace()
{
    //foreach (TraceListener listener in source.Listeners)
    //{
    //    if (listener.GetType().ToString().Contains(“MySql.EMTrace.EMTraceListener”))
    //    {
    //        qaEnabled = true;
    //        break;
    //    }
    //}
}

This time everything worked as it should and I am now able to use MySql and entity framework in a .NET 3.5 web application that is hosted with only partial trust allowed. A few more tweaks and the link shortener should be ready!