fin*_*s10 12 c# service configure nuget asp.net-core-mvc
我正在为 Asp.Net Core 创建一个 Nuget 包。我想让配置变得简单。所以我决定采用一种流畅的方式将我的 Nuget 添加到ConfigureServices()Asp.Net Core 中的服务集合中。
这是我打算做的:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options => options....);
services.AddMyNugetPackageName();
}
Run Code Online (Sandbox Code Playgroud)
在我的AddMyNugetPackageName()方法里面,
public static IServiceCollection AddMyNugetPackageName(this IServiceCollection services)
{
services
.AddMvc(options => options.ModelBinderProviders.Insert(0, new MyModelBinderProvider()))
.AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
return services;
}
Run Code Online (Sandbox Code Playgroud)
所以现在如果人们开始使用我的 Nuget 包,AddMvc()我的内部会AddMyNugetPackageName()替换AddMvc()他们的ConfigureServices()? 这会给最终用户带来什么麻烦吗?
如果这将取代AddMvc()他们的用户,ConfigureServices()那么最好的方法是什么或如何处理?
Sho*_*hoe 14
您可以使用该IConfigureOptions模式MvcOptions从您的 nuget 包中注入配置
public class MyConfigureOptions : IConfigureOptions<MvcOptions>
{
public void Configure(MvcOptions options)
{
options.ModelBinderProviders.Insert(0,new MyModelBinderProvider());
}
}
Run Code Online (Sandbox Code Playgroud)
使用ConfigureOptions注册你的选择
public static IServiceCollection AddMyNugetPackageName(this IServiceCollection services)
{
services.ConfigureOptions<MyConfigureOptions>();
}
Run Code Online (Sandbox Code Playgroud)
将您的 nuget 服务添加到服务集合
services.AddMvc();
services.AddMyNugetPackageName();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1918 次 |
| 最近记录: |