System.TypeInitializationException“发生(System.TypeInitializationException' occurred)

编程入门 行业动态 更新时间:2024-10-26 02:31:41
System.TypeInitializationException“发生(System.TypeInitializationException' occurred)

我正在ASP.NET MVC 5 C#中进行应用程序。 我在尝试从web.config文件访问连接字符串元素时遇到异常。

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

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>

我试过这个,但没有任何帮助。 请帮助我。

I am making application in ASP.NET MVC 5 C#. I am getting exception while trying to access connection string element from web.config file.

An exception of type 'System.TypeInitializationException' occurred in WebPortalClient.dll but was not handled in user code

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>

I tried this but it was of no help. Please help me out.

最满意答案

我找到了一个解决方案:

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

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

确保这里没有异常发生!

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

如果它不工作,你可以使用这个帖子

A solution i've found about it:

This exception means that the following line fails(check for the fitting line in your code):

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

Make sure that no exception happens here!

Whenever TypeInitializationException is thrown, check all initialization logic of the type you are referring to for the first time in the statement where the exception is thrown.

If it is not working you can use this post

更多推荐

本文发布于:2023-08-06 06:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446125.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发生   System   TypeInitializationException   occurred

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!