接下来我们只需要修改Startup的ConfigureServices的两个地方同样达到相同的目的。如下面的代码片段所示,我们让ConfigureServices返回一个IServiceProvider对象,这个对象直接调用我们定义的扩展方法BuilderInterceptableServiceProvider来创建。
1: public class Startup 2: { 3: public IServiceProvider ConfigureServices(IServiceCollection services) 4: { 5: services 6: .AddScoped<ISystomClock, SystomClock>() 7: .AddMvc(); 8: return services.BuilderInterceptableServiceProvider(builder => builder.SetDynamicProxyFactory()); 9: } 10: 11: public void Configure(IApplicationBuilder app) 12: { 13: app.UseMvc(); 14: } 15: }
对于上述的两种编程模式,运行程序后浏览器上都会呈现出相同的时间: