sábado, 22 de dezembro de 2018

SOLVED MySQL connector with ENTITY FRAMEWORK WORKING in Medium Trust hosting environments

First I'm going to show you how I finally used the Entity Framework in a Shared Web Hosting Trust Level. And at the end, I will let off the things I worked so hard.

My environment:
Mysql version was something 5.6.35-81.0-log
.NET framework 4.0
Visual Studio Community 2017
Shared Web Hosting Trust Level = Medium

Follow the steps to you finally publish successfully your application:

  1. Install the mysql-connector-net-6.3.9 (download)
  2. Install the mysql-for-visualstudio-1.2.8 (download)
  3. Reference the 4 mysql dlls in your project
    1. mysql.data.dll (download)
    2. mysql.data.entity.dll (download)
    3. mysql.visualstudio.dll (download)
    4. mysql.web.dll (download)
  4. Go to your project Properties > AssemblyInfo.cs and add the 2 assemblies:
    1. [assembly: SecurityRules(SecurityRuleSet.Level1)]
    2. [assembly: System.Security.AllowPartiallyTrustedCallers]
  5. Make sure your webconfig file has the following lines:
    • <system.data>
    •     <DbProviderFactories>
    •       <clear />
    •       <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient"
    •       description=".Net Framework Data Provider for MySQL"
    •       type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data,
    •       Version=, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    •     </DbProviderFactories>
    •   </system.data>
  6. Celebrate and give me a hug ;)
Now, that you already solved your problem and you are still here I would like to let off somethings.
First of all, I am so relieved that I finally solved this problem. Imagine, you finish your project using Entity Framework, everything is working and when you are publishing your project you realise that nothing works. What a feeling, huh?

This image represents the number of mysql connectors that I installed to finaly make the Entity works in my web host with medium level.


I had a Brazilian web host called Locaweb where the trust level is set as Medium. The real problem is that it was Full trust level before, and then they changed due to security permissions. Guess what? We developers will swallow one's pride.

Anyway, spending 7-8 hours in a row trying to figure out how can we use the Entity framework in these shared web hosts I learned important things such as:
  1.  Careful to install some updated connectors/extensions such as Devert. It might be really simple to implement it, but the real problem will happen when you publish in a shared webhost (if you actually have one of course).
  2. MySQL has an amazing website where you can download all the previous application versions just by clicking on the Archives tab
  3. Your webconfig does not include every config of your application. You might check the machine config in the Windows -> MicrosoftNET -> Framework -> v4 -> Configs folder
  4. MySQL website provides you with the source code of each MySQL connector, I found it so amazing =D

machine config
I would like to thank Lúcio Rogério for this amazing post it really helped me. However it is important mentioning that I still needed to add the 2 assemblies in the project. 

There was another post from  which was pretty cool as well, however when I installed the MySQL connector, I was not able to edit the source project because it was requiring Visual Studio 2005 and I really did not want to install it.

Finally, I will put down here some errors I got it during this bug fix to help some people with the same problem in the future. Cya guys!

[ArgumentException: Unable to find the requested .Net Framework Data Provider. 

It may not be installed.]
   System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName) +1402071
   System.Data.EntityClient.EntityConnection.GetFactory(String providerString) +35

[SecurityException: Request for the permission of type ‘System.Security.Permissions.SecurityPermission, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089’ failed.] MySql.Data.MySqlClient.MySqlTrace..cctor() +0

System.Security.SecurityException: That assembly does not allow partially trusted callers.

Wow, and I forgot to give you the link of the webpage where I used Entity Framework. So here you are. [SOLVED MySQL connector with ENTITY FRAMEWORK WORKING in Medium Trust hosting environments]

Nenhum comentário:

Postar um comentário