Azure 网站异常:初始化字符串的格式不符合从索引 0 开始的规范

Azure Website Exception: Format of the initialization string does not conform to specification starting at index 0

我有一个 Azure 网站,用于托管我的 MVC + Entity Framework 项目。当我 运行 本地站点使用与我的 SQL Azure 数据库相同的连接时,一切正常,但是当我将该站点部署到我的 Azure 网站时,我收到以下错误:

Format of the initialization string does not conform to specification starting at index 0.

我按照 several 其他帖子中的步骤将连接字符串添加到抛出的错误中,这就是连接字符串的结果:

Server=tcp:abcde1fgh2.database.windows.net,1433;Database=SalesLeads;User ID=username_test@abcde1fgh2;Password=Password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;

此字符串与本地使用的连接字符串匹配。
为什么这会在本地使用相同的字符串,但不能在远程使用?

编辑

这是站点的完整 web.config:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Server=tcp:abcde1fgh2.database.windows.net,1433;Database=SalesLeads;User ID=username_test@abcde1fgh2;Password=Password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" maxRequestLength="102400" />
  </system.web>
  <system.webServer>
    <modules>
      <remove name="FormsAuthentication" />
    </modules>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.AspNet.SignalR.Core" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.1.2.0" newVersion="2.1.2.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v12.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

通过对此的一些研究,我发现错误发生在以下可能的情况下:

可能性一:你可能用错了DbConnectionclass。如果您使用的是 Access,请确保使用 OleDbConnection,而不是 SqlConnection ...,反之亦然。

可能性2:表示问题在某种程度上与数据库有关,但问题与运行网站的APP POOL标识方式有关IIS。在 Application Pool Identities 中有一些关于这意味着什么的背景知识。 我们注意到,在以更老式的方式配置的站点上,我们往往看不到该问题,即在服务器上创建一个新的 'IUSR' 用户帐户,然后将此用户添加到IIS 用户组,然后将站点设置为使用此用户而不是 APP POOL 身份。 确保 密码过期问题在您的主管理员帐户上进行排序服务器。如果这不能解决问题,请创建一个新的 'IUSR' Windows 用户帐户,并将站点设置为使用此身份而不是 APP POOL 身份。 在共享主机上,这可能是不可能的,但处理管理员密码过期是主机可能仍然能够解决的问题。

您应该 FTP 进入您的 Azure 网站并检查您 web.config 中的连接字符串。

当您发布站点时,您的 web.config 会被 Azure transformed/modified 获取,因此可能是此过程与您的连接字符串发生混乱,从而导致此错误。例如,如果您通过 Web 门户设置连接字符串,可能在某些 copy/pasting 期间编码混乱等

10/2/17 - 使用 Azure Web App + ASP.NET MVC (.NET 4.6.1) + Entity Framework(最新)。为了快速修复,我将连接字符串放入代码中并且它起作用了:

public class MyDbContext : DbContext
{
    private const string connectionString = "Server=tcp:MYDB.database.windows.net,1433;Initial Catalog=MYDBNAME;Persist Security Info=False;User ID=MYUSERID;Password=MYPW;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
    public MyDbContext() : base(connectionString)
    {

    }
} 

就我而言,我忘记将 SQL_URI 包装在

@Microsoft.KeyVault(SecretUri=SQL_URI)

这发生在我的 Web 应用 > 配置 > 连接字符串 > 值中