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

标签 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>

异常(exception)详情
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

最佳答案

请参阅this Microsoft知识库文章:

症状:

  • You run a Microsoft .NET Framework 4-based application that is stored on a network share.
  • The application calls a static method in the System.Configuration.ConfigurationManager class. For example, the application calls the ConfigurationManager.GetSection method.

In this scenario, a System.Security.SecurityException exception is thrown and then the application crashes.



原因:

The issue occurs because the method fails to access configuration section from the application on network share.



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

关于从网络共享运行NET4.0应用程序时在app.config中指定defaultProxy时发生异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8327691/

相关文章:

c# - 是否有一个内置类可以用来解析包含 MySQL 列数据类型的字符串并返回等效的 .NET Type 对象?

.net - VB.NET 使用多个 Catch block 尝试 Catch

c# - C# 中的 "Application Configuration File"和 "Settings File"有什么区别?

c# - app.config 没有保存值

Android setBackgroundResource 导致内存不足异常

java - 在 dropwizard 运行状况检查中禁用错误​​堆栈跟踪

c# - 计划任务时间约束

visual-studio-2010 - 配置部分设计器替代方案

Qt/C++ 事件循环异常处理

spring-boot - 如何使用 kotlin.logging 记录堆栈跟踪?