2

app.config ファイルのエラーのレポートを改善する必要があります。

タイプ「int」の設定を含む小さなテストアプリがあります。app.config の値を有効な整数ではない値に変更すると、キャッチして報告できる例外が発生することが予想されます。残念ながら、何かが例外を食べています。その動作を防ぎ、例外を伝播させる簡単な方法はありますか?

My Settings.Designer.cs ファイル (Visual Studio で生成):

[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute(
    "Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {

    private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

    public static Settings Default { get { return defaultInstance; } }

    [global::System.Configuration.UserScopedSettingAttribute()]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Configuration.DefaultSettingValueAttribute("17")]
    public int SecondSetting
    {
        get { return ((int)(this["SecondSetting"])); }
        set { this["SecondSetting"] = value; }
    }
}

私のC#テストアプリ:

static void Main (string[] args)
{
    try
    {
        Console.WriteLine(AppConfigTests.Properties.Settings.Default.SecondSetting);
    }
    catch (Exception x)
    {
        Console.WriteLine(x.ToString());
    }
}

私の App.config ファイルの関連部分 (値は有効な整数ではないことに注意してください):

<userSettings>
    <AppConfigTests.Properties.Settings>
        <setting name="SecondSetting" serializeAs="String">
            <value>1foo7</value>
        </setting>
    </AppConfigTests.Properties.Settings>
</userSettings>

System.Number.StringToNumber によって System.Format 例外がスローされていますが、何かがそれをキャッチして破棄しているため、catch ブロックに入ることがありません。Visual Studio デバッガーの出力で、「mscorlib.dll で 'System.FormatException' 型の最初の例外が発生しました」というメッセージが表示されましたが、例外がスローされたことをさらに確認する以外には役に立ちません。

Settings.Designer.cs の設定プロパティに IntegerValidatorAttribute を追加しようとしましたが、役に立ちませんでした (.cs が再生成されないことを確認しました)。

main() メソッドの先頭に次のコードを追加しようとしましたが、それも役に立ちませんでした。

foreach (SettingsProperty sp in AppConfigTests.Properties.Settings.Default.Properties)
    sp.ThrowOnErrorDeserializing = true;

独自の ConfigurationSection を実装することを考えましたが、もっと簡単な解決策があることを願っています。

繰り返しますが、app.config ファイルの設定値のエラーを報告する方法が必要です。

(たとえば、山かっこを削除して App.config の XML 構文を壊すと、catch ブロックは要求できるすべての詳細を含む適切な例外を取得します。)

4

1 に答える 1

1

これは次のように行うことができます (app.config ですべての設定値がオーバーライドされていると仮定します)。

  foreach (SettingsPropertyValue propertyValue in AppConfigTests.Properties.Settings.Default.PropertyValues) {
    if (propertyValue.UsingDefaultValue) {
      throw new Exception(propertyValue.Name + " is not deserialized properly.");
    }
  }

Web アプリケーションを作成している場合、逆シリアル化が失敗すると、このイベントが発生します。

try {
  if (this.IsHostedInAspnet()) {
    object[] args = new object[] { this.Property, this, ex };
    Type type = Type.GetType("System.Web.Management.WebBaseEvent, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", true);
    type.InvokeMember("RaisePropertyDeserializationWebErrorEvent", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, args, CultureInfo.InvariantCulture);
  }
}
catch {
}

そうしないと、デフォルト値に戻るだけなので、できることはすべての値を繰り返し処理し、デフォルトになっていないかどうかを確認することだけです。

于 2013-06-13T09:46:28.843 に答える