5

起動時 ConfigurationSectionにファイルにカスタムを保存できません。web.config

一般的な実装については、このチュートリアルに従いました。単体テストに合格し、単体テストで de を保存できましたConfigurationSection!

ClaimCollectionアプリケーションの起動時に、コントローラーのクラス名と所属するメソッド名に基づいて を構築したいと思います。

Startup.cs

public void Configuration(IAppBuilder app)
{

    Config = new HttpConfiguration();

    BuildUnityContainer();

    BuildClaimCollection(); <----------------

    ConfigureOAuth(app);

    WebApiConfig.Register(Config);
    app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
    app.UseWebApi(Config);

}

public void BuildClaimCollection()
{

    //config section
    MethodClaimSection methodClaimSection = BaseConfigurationSection<MethodClaimSection>.Open();

    var methods = GetClaimAuthorizedMethods();

    foreach (var method in methods)
    {
        var claimValue = GetClaimValueFromMethodName(method);
        var claim = new MethodClaimElement()
        {
            Type = "Method",
            Value = claimValue
        };

        methodClaimSection.MethodClaims.Add(claim);
    }
    methodClaimSection.Save();
}  

BaseConfigurationSection.cs

public abstract class BaseConfigurationSection<T> : ConfigurationSection
             where T : ConfigurationSection, new()
{
    public void Save()
    {
        System.Configuration.Configuration config = BaseConfigurationSection<T>.GetConfiguration();
        T section = (T)config.Sections[typeof(T).Name];

        foreach (var property in this.GetType().GetProperties())
        {
            if (property.IsDefined(typeof(ConfigurationPropertyAttribute), false))
            {
                section.GetType().InvokeMember(property.Name, BindingFlags.SetProperty, null, section, 
                    new object[] { 
                        .GetType().InvokeMember(property.Name, BindingFlags.GetProperty, null, this, null) 
                        });
            }
        }

        config.Save(ConfigurationSaveMode.Full);
    }
}

WebApi アプリケーションを実行すると、次のエラーが発生します。

破棄されたオブジェクトにアクセスできません。オブジェクト名: 'System.Web.Http.HttpServer'。

スタックトレース:

[ObjectDisposedException: 破棄されたオブジェクトにアクセスできません。オブジェクト名: 'System.Web.Http.HttpServer'.] System.Net.Http.DelegatingHandler.CheckDisposed() +332772 System.Net.Http.DelegatingHandler.CheckDisposedOrStarted() +13 System.Net.Http.DelegatingHandler.set_InnerHandler( HttpMessageHandler 値) +23 System.Web.Http.HttpServer.Initialize() +108 System.Web.Http.HttpServer.b__b() +31 System.Threading.LazyInitializer.EnsureInitializedCore(T& target, Boolean& initialized, Object& syncLock, Func` 1 valueFactory) +115 System.Threading.LazyInitializer.EnsureInitialized(T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory) +72 System.Web.Http.HttpServer.EnsureInitialized() +119 System.Web.Http.d__0.

Configuration クラスには Dispose メソッドがありません...

この問題を解決するには?

4

0 に答える 0