Retarder- reorganizing the project to easy use- part 2
Now it is the moment to start reorganizing the project to be easy to use by other programmers. I want, instead of registering the services manually, to can use .AddRetarder and .UseRetarder. So I create a new project, NetCoreRetarderCore.csproj , and move there the RetarderMiddleware . The only new thing is the extension class RetarderExtensions
using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Text; namespace NetCoreRetarderCore { public static class RetarderExtensions { public static void AddRetarder(this IServiceCollection serviceCollection) { serviceCollection.AddTransient<RetarderMiddleware>(); } public static void UseRetarder(this IApplicationBuilder app) { app.UseMiddleware<RetarderMiddleware>(); } } }
This let’s me reorganize the Startup as
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using NetCoreRetarderCore; namespace NetCoreRetarder { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddRetarder(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseAuthorization(); app.UseRetarder(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } }
You can find code at https://github.com/ignatandrei/NetCoreRetarder/commits/version2_reorg
Leave a Reply