15

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?

2
  • I'd say this is not good practice. Technically it's not going to be a problem because you're just adding services, but it does mean the extra MVC bits you are configuring are hidden away from the user and may cause confusion. It looks like you are just adding a JSON contract resolver and a custom model binder, why not just add those two things? Commented May 5, 2019 at 8:52
  • @DavidG Yeah even I'm thinking of better approach!!. Please could you assist me on how this can be made better? I'm also adding services.TryAddSingleton<IHttpContextAccessor,HttpContextAccessor>(); inside my AddMyNugetPackageName(). So how can this be implemented in a better way? This is what I thought instead of asking my end user to add these three items in ConfigureServices() I thought it would be better if I give a fluent approach Commented May 5, 2019 at 9:07

1 Answer 1

19

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();
Sign up to request clarification or add additional context in comments.

5 Comments

Hi @shoe. Thanks for teaching me a new technique. How to add AddJsonOptions() in MyConfigureOptions? Do I need to inherit another interface like IConfigureOptions<MvcOptions>, IConfigureOptions<MvcJsonOptions> and have two Configure(MvcOptions options) and Configure(MvcJsonOptions options)?
Yes it's a different type you want to configure so create a new configOptions class and another call to ConfigureOptions
Yeah that's how I ended up doing. Thanks for your help and support.
Deos this "add" to existing MvcOptions or "replace" them?
Worth adding that you can use services.Configure<MvcOptions>(o => { ..... }); if you don't want to create a new class to configure the options

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.