从网络共享运行NET4.0应用程序时在app.config中指定defaultProxy时出现异常

时间:2011-11-30 14:45:31

标签: exception .net-4.0 app-config network-share defaultproxy

在NET4.0下从网络共享运行以下应用程序时,我们发现了一个非常奇怪的问题。在 app.config 中指定 defaultProxy 部分时,会抛出 System.Net.WebException 。从本地驱动器运行时没有问题。

根据文档,应用程序将作为来自网络共享的完全信任程序集运行,因此我们假设这应该可以正常工作。

我们如何解决这个问题?

是否有其他人遇到此问题或有人知道为什么会发生这种情况?

示例程序

using System;
using System.Net;

namespace ProxyTest
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                String s = new WebClient().DownloadString("http://www.google.com");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
}

的app.config

  <?xml version="1.0"?>
  <configuration>
    <system.net>
      <defaultProxy useDefaultCredentials="true"/>
    </system.net>
    <startup>
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/>
    </startup>
  </configuration>

异常详情

System.Net.WebException: An exception occurred during a WebClient request. ---> System.Configuration.ConfigurationErrorsException: Insufficient permissions for setting the configuration section 'defaultProxy'. ---> System.Security.SecurityException: Request for the permission of type 'System.Net.WebPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
   at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
   at System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark)
   at System.Security.CodeAccessPermission.Demand()
   at System.Net.Configuration.DefaultProxySection.PostDeserialize()
   --- End of inner exception stack trace ---
   at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult)
   at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSection(String configKey, Boolean getLkg, Boolean checkPermission)
   at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
   at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)
   at System.Net.Configuration.DefaultProxySectionInternal.GetSection()
   at System.Net.WebRequest.get_InternalDefaultWebProxy()
   at System.Net.HttpWebRequest..ctor(Uri uri, ServicePoint servicePoint)
   at System.Net.HttpRequestCreator.Create(Uri Uri)
   at System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase)
   at System.Net.WebRequest.Create(Uri requestUri)
   at System.Net.WebClient.GetWebRequest(Uri address)
   at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
   --- End of inner exception stack trace ---
   at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
   at System.Net.WebClient.DownloadString(Uri address)
   at System.Net.WebClient.DownloadString(String address)
   at ProxyTest.Program.Main(String[] args) in Y:\Program.cs:line 12

2 个答案:

答案 0 :(得分:13)

请参阅this Microsoft知识库文章:

症状:

  
      
  • 您运行存储在网络共享上的基于Microsoft .NET Framework 4的应用程序。
  •   
  • 应用程序在 System.Configuration.ConfigurationManager 类中调用静态方法。例如,应用程序调用 ConfigurationManager.GetSection 方法。
  •   
     

在这种情况下,抛出 System.Security.SecurityException 异常,然后应用程序崩溃。

原因:

  

出现此问题是因为该方法无法从网络共享上的应用程序访问配置部分。

您可以从该网站请求修补程序。

答案 1 :(得分:5)

我们已经看到类似的症状 - 相同的错误消息

  

设置配置部分'defaultProxy'

的权限不足

运行控制台应用程序时文件被“阻止”。

通过从超链接下载zip文件来安装文件,并且在解压缩之前zip文件没有被“解锁”。

从资源管理器中取消阻止所有文件(您必须在Windows中单独执行)&gt;右键单击&gt;属性&gt;解锁解决了问题。