![](/img/trans.png)
[英]How do you invalidate a cached AWS API Gateway endpoint in ASPNETCORE?
[英]How can I provide overridable configuration per endpoint in an AspNetCore web api?
我有一個 Asp.Net Core api 中間件,我想通過 appsettings.json 公開一些配置選項:
public class MyMiddleware: IMiddleware
{
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
// something like this, except get data from appsettings.json instead of attributes
var attribute = context.GetEndpoint()?.Metadata.GetMetadata<MyAttribute>();
if (attribute is not null) {
// do the things
}
await next(context);
}
}
我擁有的屬性模式具有我想保留的優點,我可以將屬性放在操作方法上,包含它的 class 或基數 class 和中間件使用最具體的一個。 不幸的是,這些屬性只能通過重建應用程序和部署新版本來修改。
是否有內置方式或其他合理方式將 map 選項數據存儲到端點?
我在想 appsettings.json 中的一些東西,比如路徑遍歷相似性的 globbing 規則:
{
"ConfigurationRules": [
{
"Match": "/foo/**/bar",
"Option1": "..."
},
{
"Match": "/foo",
"Option1": "..."
},
{
"Match": "/**",
"Option1": "..."
},
]
}
本文介紹了如何將自定義配置部分 map 轉換為可在代碼中使用的 object。 您的中間件必須接受IConfiguration
object,以便調用者可以將其傳遞到您在ConfigureServices
中的設置方法。 像這樣:
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
//...
public void ConfigureServices(IServiceCollection services)
{
services.AddMyMiddleware(Configuration);
}
然后在您的AddMyMiddleware
擴展方法中,您可以獲得這樣的配置部分:
var rules = Configuration.GetSection("ConfigurationRules");
services.Configure<MyConfigurationRulesClass>(rules);
現在MyConfigurationRulesClass
的這個實例將通過依賴注入在您需要的任何地方可用 - 例如通過將IOptions<MyConfigurationRulesClass> myConfiguration
添加到構造函數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.