Thursday, July 25, 2013

MySQL + WCF Data Service Error "Failed to find or load the registered .Net Framework Data Provider"

This little thing took me about an hour of angry googling (that's my daily job lately) to fix. So, you want to use WCF Data Services to expose data in your MySQL database. You created a project, added ADO.NET Entity thingy, added WCF Data Service, and this last guy does not work throwing Failed to find or load the registered .Net Framework Data Provider at you.

The only thing you are missing is a reference to MySql.Data.Entity (if only it could add itself automatically like tens of others). Go and add it: References -> Add Reference -> Extensions -> Check MySql.Data.Entity. There are a lot of versions available. My installation required version 6.6.5.0. If you add wrong version, the your server will throw an error that would tell you which version it is looking for.

Enable Verbosely Errors in WCF Data Service

In case you want to see some additional info about the occurred exception (+ its stack trace). There are 3 ways to do this:
  1. You can change your service configuration in InitializeService() method
        
    
        public class MrSophisticatedServer: DataService<TestEntities>
        {
            public static void InitializeService(DataServiceConfiguration config)
            {
                // Make service to report errors in a nice way - doesn't really help....
                config.UseVerboseErrors = true;
    
               ....
          
            }
        }
    
    
    NOTE: There is one problem with this method. It does not work if your service (for some ridiculous reason) is not initialized. Basically, something goes wrong before InitializeService() is called. In this case use method 2 or 3 below.

  2. Add this attribute before your data service class
    
        [System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)] 
    
    

  3. Add the following to your Web.config
     
    
    
        
          
          
        
        
          
            
              
            
          
        
        
         
    
    
    
    Basically, this also sets IncludeExceptionDetailInFaults to TRUE. You define service behavior and ask your smart-ass computer to use it for your service.