2017-02-16 1 views
0

Ich schreibe einen kleinen Aktionsfilter für ein ASP.NET Core-Web-API-Projekt. Der Filter dient zum Testen der zugehörigen Benutzeroberfläche zur Fehlerbehandlung. Es wird einen Fehler ausgeben, wenn ein bestimmtes Verb und eine Methode aufgerufen wird. Der Filter ist kein Problem. Das Problem ist die appsettings.configuration.So laden Sie ein untergeordnetes Objekt in appsetting.json (asp.net core)

Hier ist, was ich versuche zu tun: appsettings.development.json

"FaultTesting": { 
    "FaultRequests": false, 
    "SlowRequests": 0, 
    "FaultCalls": [ 
     { 
     "Path": "/api/usercontext", 
     "Verbs": "get,put,delete" 
     }, 
     { 
     "Path": "/api/cafeteriaaccounts", 
     "Verbs": "get" 
     } 
    ] 
    } 

Dies sind # -Typen meine c die Konfiguration zu halten:

public class FaultTestingOptions 
    { 
     /// <summary> 
     /// If true, checks FaultCalls for a path and verb to match. 
     /// </summary> 
     public bool FaultRequests { get; set; } 

     /// <summary> 
     /// Number of milliseconds to delay the response. 
     /// </summary> 
     public int SlowRequests { get; set; } 

     public FaultCall[] FaultCalls { get; set; } 

    } 
    public class FaultCall 
    { 
     public string Path { get; set; } 

     public string Verbs { get; set; } 
    } 

hinzufügen, was in ich tue Start:

  services.AddMvc(config => 
       { 
... 
FaultTestingFilter(Options.Create(GetFaultTestingOptions()))); 
... 
       }); 

private FaultTestingOptions GetFaultTestingOptions() 
{ 
    var options = new FaultTestingOptions 
    { 
     FaultRequests = Configuration["FaultTesting:FaultRequests"].ToBoolean(), 
     SlowRequests = Convert.ToInt32(Configuration["FaultTesting:SlowRequests"]) 
    }; 

    var calls = Configuration.GetSection("FaultTesting:FaultCalls") 
     .GetChildren() 
     .Select(x => x.Value) 
     .ToArray(); 

    var fooie = Configuration["FaultTesting:FaultCalls"]; 


    //options.FaultCalls = calls.Select(c => new FaultCall { Path = c, Verbs = c.Value }); 

    return options; 
} 

"Anrufe" ist ein Array von zwei Nullen, fooie ist null.

Was ist der richtige Ansatz hier?

Antwort

1

Bessere Option ist, TOption in ConfigServices Methode zu binden und dann es zu Ihnen filer injizieren. Es funktioniert wie die Standardmodellbinderarbeit, Sie mussten die Werte nicht manuell lesen und setzen.

ConfigureServices Methode:

public void ConfigureServices(IServiceCollection services) 
{ 
    services.Configure<FaultTestingOptions>(option => Configuration.GetSection("FaultTesting").Bind(option)); 
    // Add framework services. 

    services.AddMvc(); 
} 

in Filter Injektion:

private readonly IOptions<FaultTestingOptions> config; 

public FaultTestingFilter(IOptions<FaultTestingOptions> config) 
{ 
    this.config = config; 
} 

die Eigenschaften erreichbar.

var SlowRequests= config.Value.SlowRequests; 
var FaultCalls= config.Value.FaultCalls; 
+0

Dies funktionierte sehr gut. Danke für die Lösung. – Elton

Verwandte Themen