Overview Flashcards

Fundamentals 1

1
Q

What is dependency injection?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is Inversion of Control?

Bonus: What is the difference between a framework and a library?

A

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!).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the purpose and functionality of the Program.Main method in ASP.NET Core?

A

The Main method is the programs (!!) managed entry point (!!!) (only correct answer is exactly this!).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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()?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the Startup Class and what does it do?

Bonus: Which access modifiers can be used on the Startup class?

A

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!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the purpose of Configure (IApplicationBuilder app) and ConfigureServices (IServiceCollection services) ?

A

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();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are Content Root and webroot?

Bonus: How would you access the webroot from a Razor View?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a service? How would you add a service to an ASP.NET Core WebApplication?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is Middleware in ASP.NET?

Bonus: How do you add Middleware and are there naming conventions?

Bonus2: What is OWIN?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the IHttpClientFactory?

A

It allows you to initialize httprequests (GET/POST/PUT/DELETE using an httpclient instance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which environmetns are available by default and how can you set them?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does hosting work?

A

Core apps initialize and launch a “host” (WebHost) which is responsible for startup and lifecycle management.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do servers work in ASP.NET Core 2.0? Which servers are available from the get go? (Most famous)

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How does Configuration work in ASP.NET Core 2.0?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Logging in ASp.NET Core 2.0?

A

Supports a build in logging api which can log to a variety of destinations, use 3rd party logging providers like serilog etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Background Tasks in ASP.NET Core 2.0?

Bonus: Which promises must service implement for the required interface and when are those methods called

A

Background tasks can be implemented as hosted services using the IHostedService interface.

StartAsync(CancellationToken) contains the logic to start the background task. When using the WEB HOST, this method is called AFTER the server has started and IApplicationLifetime.ApplicationStarted has been triggered. When using the GENERIC host it is called before applicationstarted.

StopAsync(CancellationToken) is called when the application is performing a gracefull shutdown. This method contains the logic to stop the background task. Implement IDosposable to dispose of any unmanaged resources.

17
Q

how do you access the HttpContext in an ASP.NET Core app?

A

HttpContext is readily available when proccessing requests in MVC and Razor - Pages. In situations where the context is not readily available, you can access the context by using the IHttpContextAccessor interface and its default implementation HttpContextAccessor.

18
Q

Startup Class in ASP.NET Core => Which methods does the startup class contain?

A

The startup class is the default startup class used by an asp.net core application. The Startup class contains the method Configure(IApplicationBuilder app), and an optional method ConfigureServices(IServiceProvider serviceProvider) which configures the apps request processing pipeline. Both methods are called by the runtime when the app starts.

The Startup class is specified to the app when the apps host is build. The apps host is build when the Build() method is called on the host builder in the apps Program class (IWebHostBuilder).

You can specify the class by adding UseStartup where T is the startup class (a method of WebHostBuilderExtensions.UseStartup)

The host provides services that are available to the StartUp classes constructor, as well as to the Configure method.

19
Q

Which dependencies are most commonly injected into the Startup classes constructor?

A

IHostingEnvironment, ILoggerfactory and IConfiguration

20
Q

What is the purpose of the configure method?

A

The Configure method specifies how the app responds to HttpRequests. The request pipeline is specified by adding Middleware components to an IApplicationBuilder instance. IApplicationBuilder is available to the Configure method, but is not registered in the DI container, but directly passed to the startup class.

21
Q

Why are code dependencies problematic and why should they be avoided? Take this code snippet as an example:

public class IndexModel : PageModel
{
    MyDependency _dependency = new     MyDependency();
    public async Task OnGetAsync()
    {
        await _dependency.SomeMethod();
    }
}
A

To replace _dependency the class must be modified.

This implementation is hard to unit test. The app should use a Mock or a stub to test which is impossible with this approach.

22
Q

What is an object graph?

A

By using interfaces as an abstraction over a concrete implementation, the framework can take the responsibility of creating a required instance of a class using depedency - injection, and disposing of said instance.

If a dependency has one or many dependencies itself, the framework can resolve those dependencies.

The collective set of dependencies that must be resolved is called the dependency tree, dependency graph or object graph.

23
Q

Should you resolve a scoped service from within a singleton?

A

No, you should never do that. Resolving a scoped service from within a singleton may cause this service to have an incorrect state when processing subsequent requests.

24
Q

How should DbContexts be registered?

A

DbContexts should either be registered via a call to UseDbContext where T is of type ApplicationDbContext OR registered in the container manually.

When registering a Db context manually it is HIGHLY recommended to give it a Lifetime of ServiceLifeTime.Scoped.

Scoped lifetimes are lifetimes scoped to a single request, being disposed directly after the request.