I'm creating a Nuget Package for Asp.Net Core. I want to make it simple to configure. So I decided to give a fluent way approach to add my Nuget to the service collection in ConfigureServices()
in Asp.Net Core.
This is what I'm planning to do:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options => options....);
services.AddMyNugetPackageName();
}
Inside my AddMyNugetPackageName()
method,
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;
}
So now if people start using my Nuget package, will the AddMvc()
inside my AddMyNugetPackageName()
replace the AddMvc()
in their ConfigureServices()
? Will this cause any trouble to the end users?
If this will replace the user's AddMvc()
in their ConfigureServices()
then what is the best approach or how to handle this?
You can use the IConfigureOptions
pattern to inject a MvcOptions
configuration from your nuget package
public class MyConfigureOptions : IConfigureOptions<MvcOptions>
{
public void Configure(MvcOptions options)
{
options.ModelBinderProviders.Insert(0,new MyModelBinderProvider());
}
}
Use ConfigureOptions
to register your options
public static IServiceCollection AddMyNugetPackageName(this IServiceCollection services)
{
services.ConfigureOptions<MyConfigureOptions>();
}
Add your nuget services to the service collection
services.AddMvc();
services.AddMyNugetPackageName();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With