代码之家  ›  专栏  ›  技术社区  ›  Cyberpks

Azure应用程序正在从计算机中提取连接字符串。配置

  •  1
  • Cyberpks  · 技术社区  · 6 年前

    我已经在MS Azure上部署了一个MVC应用程序作为应用程序服务。

    在上次更新之前,它一直工作正常,现在我遇到的错误是应用程序正在恢复 connectionString 从…起 machine.config 而不是 web.config

    1. 我知道不建议编辑 机器配置

    2. 即使我想;我无法更改这些值,因为我无权访问 机器配置 文件

    我尝试添加 <clear /> 在我的 <connectionStrings> 在里面 网状物配置 但不知何故,应用程序的一个部分/视图是有效的(我刚刚使用的 web.api 但是,当使用服务器端的控制器填充页面/视图时,会抛出错误(SQL错误: network related or instance specific error )。

    应用程序以某种方式引用了名为 LocalSqlServer 。但是,我并没有在整个应用程序中提到使用该名称的连接字符串。

    调试的问题是,应用程序可以在本地实例上完美地工作,但不能在生产环境中工作。

    以下是我的连接字符串的外观:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    <configSections>
    
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </configSections>
    <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>
    <connectionStrings>
    <clear/>
    <add name="DefaultConnection" connectionString="data source=myservername;initial catalog=mydbname;User ID=userid;Password=password;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" providerName="System.Data.SqlClient" />
    <add name="AppDBModel" connectionString="data source=myservername;initial catalog=mydbname;User ID=userid;Password=password;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" providerName="System.Data.SqlClient" />
     </connectionStrings>
    
    
    <system.web>
      <authentication mode="None" />
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
      <roleManager enabled="true"></roleManager>
      <customErrors mode="Off" defaultRedirect="/Error"></customErrors>
    
    </system.web>
    <system.webServer>
    
      <modules>
        <remove name="FormsAuthenticationModule" />
        <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.Security" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.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.6.5135.21930" newVersion="1.6.5135.21930" />
      </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.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.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.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.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>
      </assemblyBinding>
    </runtime>
    <entityFramework>
    <!--<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>-->
     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="data source=myservername;initial catalog=mydbname;User ID=userid;Password=password;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
    </entityFramework>
    <system.codedom>
      <compilers>
        <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
        <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
       </compilers>
      </system.codedom>
    </configuration>
    

    添加错误堆栈跟踪

    当我移除 <clear/> 连接字符串 在里面 网状物配置

    [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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)]
    System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling) +821
     System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +5782239
     System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions) +38
    System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) +507
     System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +154
    
    System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +21
    System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) +90
    System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +209
    System.Data.SqlClient.SqlConnection.Open() +96
    System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trusted, String connectionString) +75
    
    [HttpException (0x80004005): Unable to connect to SQL Server database.]
    System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trusted, String connectionString) +125
     System.Web.Management.SqlServices.SetupApplicationServices(String server, String user, String password, Boolean trusted, String connectionString, String database, String dbFileName, SqlFeatures features, Boolean install) +89
     System.Web.Management.SqlServices.Install(String database, String dbFileName, String connectionString) +29
     System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(String fullFileName, String dataDir, String connectionString) +386
    

    这就是我在添加 <清除/> 再次标记。

    Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. 
    
    Parser Error Message: The connection name 'LocalSqlServer' was not found in the applications configuration or the connection string is empty.
    
    Source Error: 
    An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
    
    Source File: D:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config    Line: 262 
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   David Ebbo    6 年前

    我想这与你 <roleManager enabled="true"> 在您的web中。配置,但不进行配置。这导致它使用 MySQLRoleProvider 来自机器的部分。配置,其中引用 connectionStringName="LocalMySqlServer" 。相反,您应该使用自己的DB配置角色管理器。

    e、 g.来自 doc page ,您应该有如下内容:

    <roleManager enabled ="true"   
                 defaultProvider ="SqlRoleProvider" >  
      <providers>  
        <add name ="SqlRoleProvider"   
             type="System.Web.Security.SqlRoleProvider"   
             connectionStringName="SqlConn"   
             applicationName="MembershipAndRoleProviderSample"/>  
      </providers>  
    </roleManager>  
    
        2
  •  0
  •   Cyberpks    6 年前

    该问题与 RoleManager 设置为 true 然而 defaultProvider 未提供。幸亏 @DavidEbbo 对于 pointing out

    这使得服务器从 machine.config 相反

    现在,正如我在评论中提到的 suggested solution 这确实是为了 ASP Membership 但是,当前应用程序使用 ASP Identity 所以这并没有真正起作用。

    事实上,我必须编写自己的逻辑来处理 Roles 在应用程序中,因为使用 RoleProvider 对于我需要实现的简单登录来说,更多的是冗余。因此,到目前为止,当前应用程序不再使用集成的 角色提供者 但是定制的 角色提供者