从网络共享运行 NET4.0 应用程序时在 app.config 中指定 defaultProxy 时出现异常 [英] Exception when specifying defaultProxy in app.config when running NET4.0 application from network share

查看:45
本文介绍了从网络共享运行 NET4.0 应用程序时在 app.config 中指定 defaultProxy 时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

有什么想法可以解决这个问题吗?

有没有其他人遇到过这个问题,或者有没有人知道为什么会发生这种情况?

示例程序

使用系统;使用 System.Net;命名空间代理测试{课程计划{static void Main(string[] args){尝试{String s = new WebClient().DownloadString("http://www.google.com");}捕获(例外 e){Console.WriteLine(e);}}}}

app.config

 

异常详情

System.Net.WebException:WebClient 请求期间发生异常.--->System.Configuration.ConfigurationErrorsException:设置配置部分defaultProxy"的权限不足.--->System.Security.SecurityException:请求类型为System.Net.WebPermission,System,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089"的权限失败.在 System.Security.CodeAccessSecurityEngine.Check(对象需求,StackCrawlMark& stackMark,Boolean isPermSet)在 System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark)在 System.Security.CodeAccessPermission.Demand()在 System.Net.Configuration.DefaultProxySection.PostDeserialize()--- 内部异常堆栈跟踪结束 ---在 System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult)在 System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject)在 System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)在 System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)在 System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)在 System.Configuration.BaseConfigurationRecord.GetSection(String configKey, Boolean getLkg, Boolean checkPermission)在 System.Configuration.BaseConfigurationRecord.GetSection(String configKey)在 System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)在 System.Configuration.ConfigurationManager.GetSection(String sectionName)在 System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)在 System.Net.Configuration.DefaultProxySectionInternal.GetSection()在 System.Net.WebRequest.get_InternalDefaultWebProxy()在 System.Net.HttpWebRequest..ctor(Uri uri, ServicePoint servicePoint)在 System.Net.HttpRequestCreator.Create(Uri Uri)在 System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase)在 System.Net.WebRequest.Create(Uri requestUri)在 System.Net.WebClient.GetWebRequest(Uri 地址)在 System.Net.WebClient.DownloadDataInternal(Uri 地址,WebRequest& 请求)--- 内部异常堆栈跟踪结束 ---在 System.Net.WebClient.DownloadDataInternal(Uri 地址,WebRequest& 请求)在 System.Net.WebClient.DownloadString(Uri 地址)在 System.Net.WebClient.DownloadString(String address)在 ProxyTest.Program.Main(String[] args) 在 Y:\Program.cs:line 12

解决方案

请参阅这篇 Microsoft 知识库文章:>

症状:

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

在这种情况下,会引发 System.Security.SecurityException 异常,然后应用程序崩溃.

原因:

<块引用>

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

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

We're seeing a very strange issue when running the following application from a network share under NET4.0. When specifying a defaultProxy section in app.config a System.Net.WebException is thrown. There is no problem when running from a local drive.

According to documentation applications will run as full-trust assemblies from a network share so we're assuming this should work just fine.

Any ideas how we can work around this problem?

Has anyone else experienced this issue or does anyone know why this might be happening?

Sample program

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 details

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

解决方案

See this Microsoft Knowledge Base article:

Symptoms:

  • 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.

Cause:

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

You can request the hotfix from that site.

这篇关于从网络共享运行 NET4.0 应用程序时在 app.config 中指定 defaultProxy 时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆