【问题标题】:System.TypeInitializationException' occurredSystem.TypeInitializationException'发生
【发布时间】:2015-05-20 15:28:50
【问题描述】:

我正在用 ASP.NET MVC 5 C# 制作应用程序。 尝试从 web.config 文件访问连接字符串元素时出现异常。

“System.TypeInitializationException”类型的异常发生在 WebPortalClient.dll 但未在用户代码中处理

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>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="WebPortalClient.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <connectionStrings>
    <!--<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-WebPortalClient-20140714023603;Integrated Security=True"
      providerName="System.Data.SqlClient" />-->
    <add name="WebPortalClient.Properties.Settings.ConnectionString"
      connectionString="Data Source=192.168.1.141;Initial Catalog=Dishkau;User ID=saud;Password=rakeord1"
      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="1048576" />
    <sessionState mode="InProc" timeout="99999" />
  </system.web>

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <security>
        <requestFiltering>
           <!-- Set the maximum request size to 1GB (the value is in Bytes here) -->
            <requestLimits maxAllowedContentLength="1073741824" />
        </requestFiltering>
    </security>
    <modules>
      <remove name="FormsAuthentication" />

    </modules>

  <!--<modules>
      <add name="SingleSessionEnforcement" type="SingleSessionEnforcement" />
    </modules>-->
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <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.0.0.0" newVersion="5.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="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
<system.web.extensions>
        <scripting>
            <webServices>
                <jsonSerialization maxJsonLength="1000000">
                </jsonSerialization>
            </webServices>
        </scripting>
    </system.web.extensions>
  <applicationSettings>
    <WebPortalClient.Properties.Settings>
      <setting name="UploadServiceUrl" serializeAs="String">
        <value>192.168.1.141:8085/lupload/</value>
      </setting>
      <setting name="LicenseName" serializeAs="String">
        <value>ARL</value>
      </setting>
    </WebPortalClient.Properties.Settings>
  </applicationSettings>
</configuration>

我尝试了this,但没有任何帮助。 请帮帮我。

【问题讨论】:

  • 该异常的.InnerException.Message 是什么?没有它,我们就在黑暗中拍摄。虽然公平地说,静态构造函数和/或静态字段初始化器对违规类型的可见性也是一个很好的帮助。
  • Inner Exception is {"建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。未找到或无法访问服务器。验证实例名称是否正确且SQL Server 配置为允许远程连接。(提供程序:命名管道提供程序,错误:40 - 无法打开与 SQL Server 的连接)"}
  • 我们去吧;然后...去检查SQL服务器是否可用;在线,可通过防火墙规则等...
  • SQL 服务器正在运行。我有一个从数据库获取数据的网络服务。它工作正常。

标签: c# asp.net-mvc web-config


【解决方案1】:

我找到了一个解决方案:

此异常意味着以下行失败(检查代码中的拟合行):

public static string ConString = 
    ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;

确保这里没有异常发生!

每当抛出TypeInitializationException时,在抛出异常的语句中第一次检查你所引用类型的所有初始化逻辑。

如果它不起作用,您可以使用this post

【讨论】:

  • 该语句不抛出异常。内部异常:{“建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。未找到或无法访问服务器。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接.(提供者:命名管道提供者,错误:40 - 无法打开与 SQL Server 的连接)"}
  • 您确认这是正确的连接字符串吗?
  • 我编辑了答案并附上了可以提供帮助的帖子的链接
  • 谢谢亲爱的。我刚刚重启了电脑,它自动修复了。
  • 链接TypeInitializationException 对我理解和解决问题很有帮助。
猜你喜欢
  • 2016-07-21
  • 1970-01-01
  • 2015-12-30
  • 2021-11-18
  • 1970-01-01
  • 2019-10-07
  • 2017-02-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多