.Net core base class

Wanting to use the .Net core to build a set of microservices, I was considering creating a base class for the Startup class that would be responsible for setting up common functionality like logging, authentication, endpoint health checks, so standardizing all of our Services.

I was surprised, however, that such a picture does not seem to be mentioned. Does the preferred template prefer to use its own middleware for the overall functionality? Any thoughts or experience regarding this dilemma would be appreciated.

+4


source to share


2 answers


Create extension methods for interfaces IServiceCollection

and / or IApplicationBuilder

namespace Microsoft.Extensions.DependencyInjection

:

public static IServiceCollection AddAllCoolStuff(this IServiceCollection services)
{
    if (services == null)
    {
        throw new ArgumentNullException(nameof(services));
    }

    services.AddSingleton<FooService>();
    services.AddTransient<IBooService, BooService>();
    ...

    return services;
}

public static IApplicationBuilder UseAllCoolStuff(this IApplicationBuilder app)
{
    if (app == null)
    {
        throw new ArgumentNullException(nameof(app));
    }

    app.UseMiddleware<SomeCoolMiddleware>();
    ...

    return app;
}

      



And use them in your Startup's:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAllCoolStuff();
    }

    public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
    {
        app.UseAllCoolStuff();
    }
}

      

+7


source


You can use the IStartup interface or the StartupBase class if you really want to. I did the same so that all microservices have the same configuration, which is easier to manage since we have a lot of microservices.

See: https://www.strathweb.com/2017/06/resolving-asp-net-core-startup-class-from-the-di-container/

And: https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.hosting.startupbase?view=aspnetcore-2.0

The source code for StartupBase can be seen at: https://github.com/aspnet/Hosting/blob/dev/src/Microsoft.AspNetCore.Hosting/Startup/StartupBase.cs



Below is the IStartup interface:

public interface IStartup
{
    IServiceProvider ConfigureServices(IServiceCollection services);

    void Configure(IApplicationBuilder app);
}

      

And an example of how you use it:

public class Startup : IStartup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; set; }

    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        // Add framework services
        return services.BuildServiceProvider();
    }

    public void Configure(IApplicationBuilder app)
    {
        var env = app.ApplicationServices.GetService<IHostingEnvironment>();
        var loggerFactory = app.ApplicationServices.GetService<ILoggerFactory>();

        // Set up application pipeline
    }
}

      

0


source







All Articles