Tuesday, April 11, 2017

ConfigureServices and Configure ASP.Net Core




   ConfigureServices and Configure can be specified in the startup.cs 


1)  Configure services: 

  • is configuring the dependency injection container. i.e  Configuring all the different kind of services that we would be using across the applications that we can request as necessary to be injected to different parts of the application
  • So we need to tell that grab all the associated mvc services and put them into the service container

public void Configure(IApplicationBuilder app, IHostingEnvironment
 env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
        app.UseBrowserLink();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseStaticFiles();

    app.UseIdentity();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}

 2)    Configure :  

  • The ConfigureServices method is optional; but if used, it's called before the  Configure method by the runtime   
  • Here we can turn on MVC

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

    services.AddMvc();

    // Add application services.
    services.AddTransient<IEmailSender, AuthMessageSender>();
    services.AddTransient<ISmsSender, AuthMessageSender>();
}

No comments:

Post a Comment