Overview Flashcards
Fundamentals 1
What is dependency injection?
Instead of manually creating an object via constructor / factory or any other means, you pass a dependency to be instanciated for you “by someone else”. There are many different ways of handling this f.e: constructor or property injeection etc. They all share that the given dependency is resolved by someone else, f.e: a framework (.NET build in DI via IServiceProvider).
What is Inversion of Control?
Bonus: What is the difference between a framework and a library?
Inversion of control means that the flow of control in / of - a program is reversed. See Martin Fowlers post regarind DI / IoC.
Bonus:
This is also one of the main differences between a library and a framework. A library is usually a collection of classes / functions, executed and return something desired. A Framework can reverse the flow of a program by taking control away from the client and managing state / life-cycles or instantiation of objects for the client “out of his hands”. This could be DI or managing activity states in an android app (or even a garbagecollector!).
What is the purpose and functionality of the Program.Main method in ASP.NET Core?
The Main method is the programs (!!) managed entry point (!!!) (only correct answer is exactly this!).
Explain the implementation and functionality of the Program.Main method in an ASP.NET Core WebApplication, including all parameters in detail. Use this code snippet:
public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); }
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup(); }
Explain what public static void main does
Explain what CreateWebHostBuilder does
Explain what the Webhost is and does
How can I specify a custom Webserver, where would i do that?
How can I specify a custom Startup class?
Name two methods available for IWebHostBuilder
What is Build() and Run()?
1) public static void Main: Describes the managed entry point of the application.
2) CreateWebHostBuilder(args).Build().Run();:
- Loads the .NET Core Runtime
- Uses the first command line argument as the path to the managed binary that contains the entry point and begins program execution.
3) Invoces Webhost.CreateWebHostBuilder which followes the builder pattern to create a webhost.
The Webhost hast methods that define a websever (f.e. UseKestrel) and a StartUp class (UseStartup). If no Webserver is specified, ASP.NET Core 2.0 will try to use IIS per default (if available).
IWebhostbuilder provides many optional methods such as UseHttpSys for hosting the app on Http.sys as well as UseContentRoot for specifying a custom content root directory.
The Build() and Run() Methods build the IWebHost object with hosts the app and listens for http requests.
What is the Startup Class and what does it do?
Bonus: Which access modifiers can be used on the Startup class?
The startup class is the class in which the services required by the app are configered and the request pipeline is defined.
The Startup class must be public and usually contains the following methods:
public void ConfigureServices(IServiceCollection services) { }
public void Configure(IApplicationBuilder app) { }
ConfigureServices is optional!
What is the purpose of Configure (IApplicationBuilder app) and ConfigureServices (IServiceCollection services) ?
ConfigureServices configures the services used in your app such as:
services. AddMvC();
services. AddDbContext();
services. AddSingleton();
Configure configures the middleware in the request pipeline such as:
app. UseHttpsRedirection();
app. UseStaticFiles();
app. UseCookiePolicy();
app. UseMvc();
What are Content Root and webroot?
Bonus: How would you access the webroot from a Razor View?
The content root is the base path to any content used by the app such as MVC, RazorPages and static assets. The default content root is the same location as the base path for the executable hosting the app.
The webroot is the directory in the project containing static, public files sch as CSS, JS and images. By default wwwroot is the webroot.
For razor, ~/ (tilde slash) points to the webroot. Paths beginning with tilde slash (~/) are called virtual paths.
What is a service? How would you add a service to an ASP.NET Core WebApplication?
A service is a component intended for common consumption in an app. Services should be made available trough DI which ASP.NET Core 2.0 supports natively (constructor injection). ASP.NET Core includes a native IoC container.
What is Middleware in ASP.NET?
Bonus: How do you add Middleware and are there naming conventions?
Bonus2: What is OWIN?
In ASP.NET Core you compose your request pipeline using middleware. Midlewre performs asynchronous operations on an HttpContext and either invokes the next Middleware in the pipeline or terminates the request.
ASP.NET Core 2.0 supports OWIN (Open web interface for .NET) which decouples your app from a webserver or can self-host an asp.net core 2.0 application.
What is the IHttpClientFactory?
It allows you to initialize httprequests (GET/POST/PUT/DELETE using an httpclient instance.
Which environmetns are available by default and how can you set them?
ASP.NET Core supports environments like “Development” and “Production” by default. You can set them using an environment variable, command line arguments or a settings file.
How does hosting work?
Core apps initialize and launch a “host” (WebHost) which is responsible for startup and lifecycle management.
How do servers work in ASP.NET Core 2.0? Which servers are available from the get go? (Most famous)
ASP.NET Core’s hosting model does not directly listen to HTTP Requests, instead a webserver directs httprequests to the app.
Windows:
Kestrel Serer is a platform independant server wroking on windows, macOS and Linux. It is often run in a reverse proxy configuration using IIS.
IISHttpServer is an in-process server for IIS:.
Http.sys server is a webserver for windows.
MacOS:
Kestrel
Linux:
Kestrel / Apache / Nginx
How does Configuration work in ASP.NET Core 2.0?
ASP.NET Core uses a build in configuration model based on name-value pairs. Configuration does not use System.Configuration or a web.config file, instead you can even write your own configuration provider and the base implementation supports a variety of configuration files such as JSON/XML/INI and command-line arguments etc.
Logging in ASp.NET Core 2.0?
Supports a build in logging api which can log to a variety of destinations, use 3rd party logging providers like serilog etc.