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

时间:2015-02-14 20:38:03

标签: entity-framework connection-string azure-web-sites

我有一个Azure网站,用于托管我的MVC +实体框架项目。当我使用与SQL Azure数据库相同的连接在本地运行站点时,一切都运行良好,但当我将站点部署到我的Azure网站时,我收到以下错误:

  

初始化字符串的格式不符合从索引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>

4 个答案:

答案 0 :(得分:3)

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

当您发布网站时,您的web.config会被Azure转换/修改,因此这个过程可能会使您的连接字符串混乱,导致此错误。例如,如果您通过Web门户设置连接字符串,可能在某些复制/粘贴过程中编码混乱,等等。

答案 1 :(得分:1)

10/2/17 - 使用Azure Web App + ASP.NET MVC(.NET 4.6.1)+实体框架(最新版)。为了快速解决问题,我将连接字符串放在代码中并且工作正常:

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)
    {

    }
} 

答案 2 :(得分:0)

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

可能性1 :您可能使用了错误的DbConnection课程。如果您使用的是Access,请确保使用OleDbConnection,而不是SqlConnection ...反之亦然。

可能性2 :表明该问题在某种程度上与数据库有关,但该问题与在IIS上运行网站的APP POOL身份识别方式有关。关于这在Application Pool Identities中的含义有一些背景知识。 我们注意到,我们倾向于不会在以更老式的方式配置的网站上看到问题,即创建一个新的“IUSR&#39;服务器上的用户帐户,然后将此用户添加到IIS用户组,然后将该站点设置为使用此用户而不是APP POOL标识。 确保密码到期问题已排序在服务器上的主管理员帐户上。如果这不能解决问题,那就创建一个新的IUSR&#39; Windows用户帐户,并将站点设置为使用此而不是APP POOL标识。 在共享主机上,这可能无法实现,但处理管理员密码到期是主机可能仍然可以解决的问题。

答案 3 :(得分:0)

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

@Microsoft.KeyVault(SecretUri=SQL_URI)

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