ASP.NET Core

Program.cs - the entry point

Default,

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .Build();
}

CreateDefaultBuilder performs the following tasks:

  • Configures Kestrel as the web server.

  • Sets the content root to the path returned by Directory.GetCurrentDirectory.

  • Loads optional configuration from:

    appsettings.json.
    appsettings.{Environment}.json.
    User secrets when the app runs in the Development environment.
    Environment variables.
    Command-line arguments.
  • Configures logging for console and debug output. Logging includes log filtering rules specified in a Logging configuration section of an appsettings.json or appsettings.{Environment}.json file.

  • When running behind IIS, enables IIS integration. Configures the base path and port the server listens on when using the ASP.NET Core Module. The module creates a reverse proxy between IIS and Kestrel. Also configures the app to capture startup errors.

  • Sets ServiceProviderOptions.ValidateScopes to true if the app's environment is Development.

BuildWebHost without the default

public static IWebHost BuildWebHost(string[] args) =>
    new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .ConfigureAppConfiguration(config =>
            config.AddJsonFile("appSettings.json", true)
        )
        .ConfigureLogging(logging=>
            logging
            .AddConsole()
            .AddDebug()
        )
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

Startup.cs

it was first introduced in OWIN-based applications to replace
most of the tasks previously handled by the good old Global.asax file.

  • Add and configure Services and Dependency Injection, in the ConfigureServices method

  • Configure HTTP request pipeline by adding the required Middleware packages, in the Configure method

By default,

    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.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
                {
                    HotModuleReplacement = true
                });
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

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

                routes.MapSpaFallbackRoute(
                    name: "spa-fallback",
                    defaults: new { controller = "Home", action = "Index" });
            });
        }
    }

host

Kestrel

UseKestrel()
public static IWebHost BuildWebHost(string[] args) =>
new WebHostBuilder()
.UseKestrel()
//...
.UseStartup<Startup>()
.Build();

is equivlent to,

public static IWebHostBuilder UseHttpSys(this IWebHostBuilder hostBuilder)
{
return hostBuilder.ConfigureServices(services => {
services.AddSingleton<IServer, KestrelServer>();
// ...
});
}

HTTP.sys (WebListener before ASP.NET Core 2.0)

HTTP.sys is incompatible with the ASP.NET Core Module and can't be used with IIS or IIS Express.

UseHttpSys()
public static IWebHost BuildWebHost(string[] args) =>
new WebHostBuilder()
.UseHttpSys()
//...
.UseStartup<Startup>()
.Build();

is equavilent to,

public static IWebHostBuilder UseHttpSys(this IWebHostBuilder hostBuilder)
{
return hostBuilder.ConfigureServices(services => {
services.AddSingleton<IServer, MessagePump>();
// ...
});
}

reverse proxy

Use IIS as the reverse proxy.

The host will listen for requests coming
from the ASP.NET Core IIS Module (ANCM), an IIS module explicitly built for serving
requests from IIS to ASP.NET Core.

public static IWebHost BuildWebHost(string[] args) =>
new WebHostBuilder()
...
.UseIISIntegration()
...
.UseStartup<Startup>()
.Build();

Filters

Dependency Injection

In the ConfigureServices method, all the application-level dependencies are
registered inside the default IoC container by adding them to an IServiceCollection.

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<IComponent, ComponentB>();
}

To manually create the object,

var component = app.ApplicationServices.GetRequiredService<IComponent>();

Working with multiple environments

ASP.NET Core reads the environment variable ASPNETCORE_ENVIRONMENT at application startup and stores that value in IHostingEnvironment.EnvironmentName. ASPNETCORE_ENVIRONMENT can be set to any value, but three values are supported by the framework: Development, Staging, and Production. If ASPNETCORE_ENVIRONMENT isn't set, it will default to Production.

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments

JavaScriptServices

https://github.com/aspnet/JavaScriptServices

Building Single Page Applications on ASP.NET Core with JavaScriptServices

NodeServices

https://github.com/aspnet/JavaScriptServices/tree/dev/src/Microsoft.AspNetCore.NodeServices#microsoftaspnetcorenodeservices

Angular 4

results matching ""

    No results matching ""