【问题标题】:Asp Web Api deployed db connectionAsp Web Api 部署的数据库连接
【发布时间】:2015-12-10 14:08:56
【问题描述】:

我有一个简单的 ASP Web API 2 应用程序,它部署到 Azure Web 应用程序。它使用同样托管在 Azure 上的 SQL 数据库。 DbContext 是使用 Publish 对话框设置的,如下图所示,因此我信任 VS 工具,并且我假设连接字符串设置正确:(可以找到以下示例 here

连接字符串在 Web.config 中设置

<connectionStrings>
    <add name="DefaultConnection" 
         connectionString="..." 
         providerName="System.Data.SqlClient"/>
</connectionStrings>

并用于ctorIdentityDbContext

public ApplicationDbContext() : base("DefaultConnection")

当 API 项目在本地调试时,它可以毫无问题地访问生产数据库,但是一旦部署它就不能再访问它了。以下是例外情况(发回邮递员)。 SO上有一些关于类似问题的其他答案,但到目前为止我没有成功地关注它们。

[Win32Exception (0x80004005): No such host is known]

[SqlException (0x80131904): A network-related or instance-specific 
error occurred while establishing a connection to SQL Server. The 
server was not found or was not accessible. Verify that the instance
name is correct and that SQL Server is configured to allow remote
connections. (provider: TCP Provider, error: 0 - No such host is known.)]
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +92
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +285
System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover, SqlAuthenticationMethod authType) +372
System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) +172

[ProviderIncompatibleException: The provider did not return a ProviderManifestToken string.]
System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) +271
System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection) +33

[ProviderIncompatibleException: An error occurred accessing the database. This usually means that the connection to the database failed. Check that the connection string is correct and that the appropriate DbContext constructor is being used to specify it or find it in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=386386 for information on DbContext and connections. See the inner exception for details of the failure.]
System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection) +246
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

【问题讨论】:

  • 您是否根据构建配置转换配置文件?
  • 不,我禁用它们是为了调试这个问题。
  • 如果您可以在生产环境中验证连接字符串是否正确,那么您的问题可能是您尝试访问的网络位置无法使用生产数据库?您确定 DNS,您的 hosts 文件中没有指向数据库的条目吗?
  • WebApp 和 Sql 数据库都是从 Azure 门户托管和管理的。是否应该授予某种权限?

标签: c# sql-server azure asp.net-web-api asp.net-web-api2


【解决方案1】:

验证您的 LIVE 连接字符串是否正确

转到http://myWebApp.scm.azurewebsites.net 并选择调试控制台 > PowerShell。然后运行它来查看你的 LIVE 连接字符串。

cd site/wwwroot
$xml = [xml](Get-Content Web.config)
$connStrings = $xml.SelectSingleNode("configuration/connectionStrings") 
$connStrings.InnerXml

验证您的防火墙规则是否正确

转到http://portal.azure.com,打开你的数据库服务器(BROWSE ALL > Sql servers > myServer),然后选择Settings 。在防火墙下,确保允许访问 Azure 服务开启

您也可以像这样从 Azure PowerShell 执行此操作:

Get-AzureAccount                                # sign-in to your account
Get-AzureResource -ResourceGroupName mvp2015 `
    -ResourceName mvp2015 `
    -ResourceType Microsoft.Sql/servers/firewallrules `
    -OutputObjectFormat New

如果您看到名为 AllowAllWindowsAzureIps 的规则,则说明您已正确配置。

检查您的 Web 应用配置

您可能会覆盖 Web 应用配置中的 Web.config。

您可以在 portal.azure.com > Browse All > MyAppName > Settings > Application settings > Connection strings 进行检查。如果您有一个名为 DefaultConnection 的名称,它将覆盖 Web.config 连接字符串。

您还可以在 myappname.scm.azurewebsites.net > 环境 > 连接字符串中进行检查。确保那里列出的DefaultConnection 是您想要的。

【讨论】:

  • 感谢您的提示。不幸的是,字符串连接是正确的,并且设置了权限。
  • 确实,myappname.scm.azurewebsites.net 连接字符串中有一个旧的 DefaultConnection。哪个 Web 应用配置会设置此连接字符串?
  • 通常您可以在 portal.azure.com > Browse All > MyAppName > Settings > Application settings > Connection strings 中找到连接字符串覆盖。
猜你喜欢
  • 2018-04-11
  • 1970-01-01
  • 2018-11-21
  • 2019-01-08
  • 1970-01-01
  • 1970-01-01
  • 2010-09-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多