2

I have recently subscribed for Azure free trial, and I'm currently trying to publish my website.

However, as I got my website published I encountered the following error:

[NullReferenceException: Object reference not set to an instance of an object.]
MySql.Data.MySqlClient.MySqlProviderServices.GetDbProviderManifestToken(DbConnection connection) +56
   System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) +276
   System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection) +27
   System.Data.Entity.Infrastructure.<>c__DisplayClass1.<ResolveManifestToken>b__0(Tuple`3 k) +32
   System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) +72
   System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.ResolveManifestToken(DbConnection connection) +251
   System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest) +56
   System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) +43
   System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) +62
   System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) +123
   System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +610
   System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +18
   System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +53
   System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +15
   System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() +38

When I run this locally with the connection-string to my remote database it works fine, but as soon as I publish it it doesn't work.

My connection strings look like this:

<add name="DefaultConnection" connectionString="user id=****;password=****;persistsecurityinfo=False;server=**** ;database=iprospect_tools" providerName="MySql.Data.MySqlClient" />
<add name="ToolsEntities" connectionString="metadata=res://*/ToolsModel.csdl|res://*/ToolsModel.ssdl|res://*/ToolsModel.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;user id=****;password=****;persistsecurityinfo=False;server=****;database=tools&quot;" providerName="System.Data.EntityClient" />

When I remote debug my application, this is the error evaluation:

enter image description here

Any idea what might be causing this error?

Byron Tardif
  • 1,172
  • 6
  • 16
Frederik Hansen
  • 506
  • 4
  • 21

2 Answers2

6

There is some informatin missing form your question that would make providing a consice answer easier, but here is what I would do:

  1. Figure out where the db is being hosted

The first thing to do here is to figure out what service you are using for your MySQL database. In Azure there are at least four ways to host a MySQL database and each one might require different steps.

  • MySQL in App Since you are doing a remote connection from your desktop I don't think you are using this option.

  • MySQL provided by ClearDB If you are using this, then server name would be something like: us-cdbr-azure-*.cloudapp.net

  • MySQL provided by Microsoft If you are using this, then server name would be something like: *.mysql.database.azure.com

  • MySQL hosted in an IaaS VM If you are using this, then server name would be something like: *.cloudapp.net

If the database is not hosted in Azure, then you should check network configuration.

  1. Create a simplest app possible to test connectivity.

Assuming your DB is hosted in Azure the next step is to reduce your dependencies in an effort to identify if the issue lies with the Db server, network config, your code, or another dependency.

 string myConnectionString = "your-connectionstring-goes-here";
    try{
        using (var con = new MySqlConnection { ConnectionString = myConnectionString }){
            con.Open();

            if (!con.Ping()){
                System.Diagnostics.Trace.TraceError("MySQL Ping Failed:");
            }
            else{
                System.Diagnostics.Trace.TraceInformation("MySQL Successfull");
            }
        }
    }
    catch (Exception exep){
        System.Diagnostics.Trace.TraceError("Could not connect to MySQL: " + exep.Message);
    }

Note that in this example we are hard-coding the connection string into the code to bypass even web-config.

If this is working then we know connectivity to the DB is good and the app service app can reach the DB. Skip step (3) and go to step (4)

If it's not working then the we still have a bit more debugging to do. Go to step (3)

  1. Fix network/firewall on DB server

    • If your DB is being hosted by Microsoft MySQL check to make sure the Firewall is allowing your app to connect to it and repeat step (2)

    • If your DB is being hosted in an IaaS VM make sure the ports and network are open to accepct connections and repeat step (2)

Once you get step (2) to work then try with your code again.

  1. Debug your code

Once you get step (2) working and then that rules out connectivity issues. That means that the bug is in your code, connection string config or the way you are using Entity Framework.

Hope this helps

Byron Tardif
  • 1,172
  • 6
  • 16
1

If the website is working locally, and database connection failing after publishing to Azure - definitely config is missing in Azure website Settings.

enter image description here

Check out these SO thread 1 and SO thread 2