10

今日、.NET 4.0ランタイム用に構築されたアプリケーションをリモートでデバッグしようとしたときに、奇妙な問題が発生しました。

アプリケーションはネットワーク共有上にあり、リモートマシンによって実行されます。ただし、System.Configuration.ConfigurationManager.GetSection()メソッドのアクセス許可要求によって発生したSecurityExceptionが原因で、ロード中にアプリケーションが毎回クラッシュします。基本クラスライブラリの他のアクセス許可の要求によってセキュリティ例外が発生するかどうかは確認していませんが、すべての場合において、これは新しいCLRでは発生しないはずです。

アプリケーションは完全に信頼されて実行されているため(デバッグ中にチェックし、CLR 4.0のイントラネットアプリケーションでは常にこれが当てはまる必要があります)、この場合、アクセス許可の要求によって例外が発生する可能性があります。3.5 SP1ランタイム(デフォルトでネットワーク共有アプリに完全な信頼を最初に導入した)に対して構築された場合、すべてが期待どおりに実行されます。

以下のサンプルコードを貼り付けました。どんな助けでも大歓迎です。

using System;
using System.Configuration;

namespace ConsoleApplication1
{
public sealed class AssetsSection : ConfigurationSection
{
    private static readonly ConfigurationProperty           s_propPath;
    private static readonly ConfigurationPropertyCollection s_properties;

    static AssetsSection()
    {
        s_propPath = new ConfigurationProperty("path", typeof(String));

        s_properties = new ConfigurationPropertyCollection()
        {
            s_propPath
        };
    }

    public static AssetsSection Get()
    {
        return (AssetsSection) ConfigurationManager.GetSection("test/assets");
    }

    protected override ConfigurationPropertyCollection Properties
    {
        get
        {
            return s_properties;
        }
    }

    public String Path
    {
        get
        {
            return (String) base[s_propPath];
        }
        set
        {
            base[s_propPath] = value;
        }
    }
}

class Program
{
    static void Main(String[] args)
    {
        Console.WriteLine(AssetsSection.Get().Path);

        Console.ReadLine();
    }
}
}

そしてApp.configファイル。

<?xml version="1.0"?>
<configuration>
<configSections>
    <sectionGroup name="test">
        <section name="assets" type="ConsoleApplication1.AssetsSection, ConsoleApplication1"/>
    </sectionGroup>
</configSections>

<startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/>
</startup>

<test>
    <assets path="..\Assets"/>
</test>
</configuration>
4

4 に答える 4

17

最初に構成をロードして、そのセクションを開いてみてください。

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AssetsSection configSection = (AssetsSection)config.GetSection("test/assets");

.NET 4でも同じ問題が発生しましたが、これでうまくいきます。

于 2010-05-10T07:23:45.547 に答える
4

これは、ネットワーク共有からアプリケーションを実行する際の.NET4.0の既知のバグが原因です。

次のコードはSecurityExceptionで失敗します。この例のようにセクションにカスタムタイプを定義した場合にのみ失敗することに注意してくださいAssetsSection

ConfigurationManager.GetSection("test/assets");

1つの修正は、別のAPIを使用するためのTimoによるソリューションの提案です。別の解決策は、Microsoftが提供するパッチを適用することです。

バグと関連する修正プログラムはKB2580188の下に提出されています。

于 2012-11-27T09:52:14.793 に答える
1

次のようにセクションをマップするために独自のクラスを追加する場合:

[XmlRoot("Interface")]
public class MySectionClass
{
    [XmlAttribute()]
    public string MyAttr1
    {
        get;
        set;
    }

    public string MyAttr2
    {
        get;
        set;
    }
}

次のコードを使用できます。

ConfigurationSection configSection = 
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).
GetSection("MySection");

XmlSerializer xs = new XmlSerializer(typeof(MySectionClass));

XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(configSection.SectionInformation.GetRawXml());

XmlNodeReader xnr = new XmlNodeReader(xdoc.DocumentElement);

MySectionClass section = (MySectionClass)xs.Deserialize(xnr);
于 2012-01-03T15:39:51.390 に答える
-1

ここで推測していますが、信頼されていないのは構成ファイルだと思います。

あなたの場合、構成ファイルはConsoleApplication1.AssetsSection、証拠として使用できる厳密な名前を持たない型を参照しています。

詳細と正確なエラーメッセージを教えてください。

于 2010-05-05T17:02:17.140 に答える