2014-05-04 21 views
0

几个星期以来,我一直在试图解决从无处出现的问题。问题是,每当我尝试创建WebRequest时,它都会以NotInitialized异常失败。 这不仅仅是新的代码,而且旧的应用程序以前运行良好,现在正在崩溃。我尝试卸载.NET 2.0 SDK(我认为可能是原因),但没有结果。.NET 2.0和3.0 WebRequest不能正常工作

即使WebClient不能下载任何东西,只是抛出未知的异常。 这出现在.NET 2.0和3.0,.NET 4.0和4.5似乎工作正常。

有谁知道为什么会发生这种情况?

编辑:它有两个可能的例外。一个是ConfigurationErrorsException(配置系统无法初始化),当我删除app.config时,这会消失,而我不需要。

第二个是的NullReferenceException(不设置到对象的实例对象引用。)

堆栈跟踪:

at System.Net.WebRequest.get_PrefixList() 
    at System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase) 
    at System.Net.WebRequest.Create(String requestUriString) 
    at EAC.Form1.Request(String url, String postData, Boolean useAlpha, Boolean useBeta) in D:\Projects\someproject\someproject\Form1.cs:line 34 
+0

幸运的是,在.NET中,“未知异常”不存在(除了互操作异常,当它们被错误处理时可能未知)。在这里粘贴确切的例外名称,消息和堆栈跟踪。否则,我们将无法帮助您。 –

+0

几乎所有的'NullReferenceException'都是一样的。请参阅“[什么是.NET一个NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)”获得一些提示。 –

+0

谢谢,但正如我写的 - 这不仅仅是新的代码,而且旧的应用程序以前运行良好,现在正在崩溃。 这个错误也不会直接出现在我的代码中,而是在System.Net.WebClient类中,我根本没有修改它,并且用于非常好地工作。 – Eldan

回答

0

检查“webRequestModules”在machine.config中部分登记,并且如果添加它不存在。

<sectionGroup name="system.net" type="System.Net.Configuration.NetSectionGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
     <section name="authenticationModules" type="System.Net.Configuration.AuthenticationModulesSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> 
     <section name="connectionManagement" type="System.Net.Configuration.ConnectionManagementSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> 
     <section name="defaultProxy" type="System.Net.Configuration.DefaultProxySection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> 
     <sectionGroup name="mailSettings" type="System.Net.Configuration.MailSettingsSectionGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
      <section name="smtp" type="System.Net.Configuration.SmtpSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> 
     </sectionGroup> 
     <section name="requestCaching" type="System.Net.Configuration.RequestCachingSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> 
     <section name="settings" type="System.Net.Configuration.SettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> 
     <section name="webRequestModules" type="System.Net.Configuration.WebRequestModulesSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> 
</sectionGroup>