【问题标题】:Deploying to Azure - Cannot connect to database部署到 Azure - 无法连接到数据库
【发布时间】:2018-01-17 08:52:51
【问题描述】:

我最近订阅了 Azure 免费试用版,目前正在尝试发布我的网站。

但是,当我发布我的网站时,我遇到了以下错误:

[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

当我使用远程数据库的连接字符串在本地运行它时,它工作正常,但一旦我发布它就不起作用。

我的连接字符串如下所示:

<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" />

当我远程调试我的应用程序时,这是错误评估:

知道是什么导致了这个错误吗?

【问题讨论】:

  • 您的数据库和网站在 Azure 中托管在哪里(哪个服务)?您是否能够在 azure 中从您的实例 ping 数据库。
  • 我的网站托管为 Web 应用程序,我的数据库托管为 MySQL 数据库。我不确定从 Azure 中的实例 ping 数据库的位置 - 你能告诉我在哪里可以做到吗?
  • 您可以通过 kudu 使用命令控制台。要访问 kudu,请浏览sitename.scm.azurewebsites.net/DebugConsole。在这里,您可以使用 tcpping.exe 在特定端口上 ping 主机名。 tcpping hostname:port
  • 将站点名称替换为您的 Web 应用程序的名称。
  • 我可以成功 ping 我的数据库。

标签: c# mysql entity-framework azure azure-web-app-service


【解决方案1】:

您的问题中缺少一些信息,这将使提供简洁的答案更容易,但这是我要做的:

  1. 找出数据库的托管位置

在这里要做的第一件事是弄清楚您为 MySQL 数据库使用的服务。在 Azure 中,托管 MySQL 数据库的方法至少有四种,每种方法都可能需要不同的步骤。

  • 应用程序中的 MySQL 由于您是从桌面进行远程连接,我认为您没有使用此选项。

  • ClearDB 提供的 MySQL 如果您正在使用它,那么服务器名称将类似于:us-cdbr-azure-*.cloudapp.net

  • 微软提供的 MySQL 如果您使用它,那么服务器名称将类似于:*.mysql.database.azure.com

  • MySQL 托管在 IaaS 虚拟机中 如果您正在使用它,那么服务器名称将类似于:*.cloudapp.net

如果数据库未托管在 Azure 中,则应检查网络配置。

  1. 创建一个最简单的应用程序来测试连接性。

假设您的数据库托管在 Azure 中,下一步是减少您的依赖项,以确定问题是否出在数据库服务器、网络配置、您的代码或其他依赖项上。

 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);
    }

注意,在本例中,我们将连接字符串硬编码到代码中以绕过 web-config。

如果这正常工作,那么我们知道与数据库的连接良好,并且应用服务应用可以访问数据库。跳过第(3)步,进入第(4)步

如果它不起作用,那么我们还有更多的调试工作要做。转到步骤(3)

  1. 修复数据库服务器上的网络/防火墙

    • 如果您的数据库由 Microsoft MySQL 托管,请检查以确保防火墙允许您的应用连接到它并重复步骤 (2)

    • 如果您的数据库托管在 IaaS 虚拟机中,请确保端口和网络已打开以接受连接并重复步骤 (2)

一旦您开始执行第 (2) 步,请再次尝试使用您的代码。

  1. 调试代码

一旦您开始执行第 (2) 步,就可以排除连接问题。这意味着错误存在于您的代码、连接字符串配置或您使用实体框架的方式中。

希望对你有帮助

【讨论】:

    【解决方案2】:

    如果网站在本地运行,并且在发布到 Azure 后数据库连接失败 - Azure 网站设置中肯定缺少配置。

    查看这些SO thread 1SO thread 2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      • 2021-05-30
      • 2016-04-08
      • 2019-11-13
      • 2018-02-09
      • 1970-01-01
      相关资源
      最近更新 更多