ASP.NET (Part 4)

With ASP.NET Core, we have rewritten the web framework and .NET framework to better enable a modern web development experience and provide a baseline to build the next 15 years of server development.

This controller is capable of servicing a Web API GET request, responding on the api/Product route as well as handle a request for the Index page and return with the content of a Razor-based view.

Yeoman generators

Secrets Manager

env.IsDevelopment() This is a check that the current environment is named “Development”. By default, when you debug an application from Visual Studio 2015, an environment variable called “ASPNETCORE_ENVIRONMENT” is set to the value “Development” and ASP.NET Core can interpret that value and make features available to the Development environment only. If that environment variable is NOT set, ASP.NET Core assumes that you are running in Production.

We recommend that you look at the switch of an application to ASP.NET Core as a rewrite instead of a migration

A repository is an object that encapsulates the data layer, and contains logic for retrieving data and mapping it to an entity model.

CreateAtRoute also adds a Location header to the response. The Location header specifies the URI of the newly created to-do item.

ASP.NET Core can figure out to call the controller’s method based on the name of the class by the Controller postfix convention. Unless you need specific features of the Controller base class you don’t have to inherit from it in ASP.NET Core.

ASP.NET Core’s MVC version supports both API and MVC endpoints in the same controller, so I don’t need to create a separate controller class (although generally it’s a good idea to separate API and UI endpoints)

If you run on Windows you will likely want to run Kestrel behind IIS to gain infrastructure features like port 80/443 forwarding via Host Headers,
process lifetime management and certificate management to name a few.

Once you’ve installed the hosting bundle (or you install the .NET Core SDK on your Dev machine) the AspNetCoreModule is available in the IIS native module list:

While the IIS Site/Virtual still needs an IIS Application Pool to run in, the Application Pool should be set to use No Managed Code. Since the App Pool acts merely as a proxy to forward requests, there’s no need to have it instantiate a .NET runtime.

It’s also a good idea to run secure SSL requests through IIS proper by setting up certificates through the IIS certificate store and letting IIS handle the SSL authentication. IIS can also provide static file serving, gzip compression of static content, static file caching, Url Rewriting and a host of other features that IIS provides natively. IIS is really good and efficient at processing non-application requests, so it’s worthwhile to take advantage of that.

In order to run an application with IIS you have to first publish it. There are two ways to that you can do this today:
● Use dotnet publish
● Use the Visual Studio Publishing Features

Dependency Injection on MVC 6

Or as below

Will be automatically resolved by .net framework

 

Webpack and aspnetcore environment

Asp.net core configuration

CreatedAtAction helper method

Nuget packaging

 

Test runner

Facts are tests which are always true. They test invariant conditions.
Theories are tests which are only true for a particular set of data.

Tag Helpers

Overriding existing tags

If you need HttpContext information, copy the values you care about to a state object or inside a closure and pass it in to the background worker.  Don’t pass the HttpContext instance itself, as it’s not a thread-safe object and even simple property getters (like HttpContext.Request.Url) might throw.

The .NET runtime uses an AppDomain as a container for code and data, just like the operating system uses a process as a container for code and data. As the operating system uses a process to isolate misbehaving code, the .NET runtime uses an AppDomain to isolate code inside of a secure boundary.

An AppDomain belongs to only a single process, but single process can hold multiple AppDomains. An AppDomain is relatively cheap to create (compared to a process), and has relatively less overhead to maintain than a process.

Each ASP.NET application will have it’s own set of global variables: Cache, Application, and Session objects are not shared.

Even though the code for both of the applications resides inside the same process, the unit of isolation is the .NET AppDomain. If there are classes with shared or static members, and those classes exist in both applications, each AppDomain will have it’s own copy of the static fields – the data is not shared. The code and data for each application is safely isolated and inside of a boundary provided by the AppDomain.

Since ASP.NET cannot swap the dll into the existing AppDomain , it starts a new AppDomain. The old application domain is “drain stopped”, that is, existing requests are allowed to finish executing, and once they are all finished the AppDomain can unload. The new AppDomain starts with the new code and begins taking all new requests.

Shadow Copy allows us to overwrite any dll in the bin directory during an update without taking the web application offline.

Using the CurrentDomain property of the AppDomain class you can inspect properties about the AppDomain your code is executing in, including the Shadow Copy settings.

Difference between Task.Run and QueueBackgroundWorkItem in Asp.Net

ConfigureAwait(false);

It is more performant since it doesn’t have to switch thread contexts back to the original thread context.

This is true with UI applications, where there is only one UI thread that you have to “sync” back to.

So ConfigureAwait(false) does not save you a thread jump in ASP.NET; it does save you the reentering of the request context, but this is normally very fast. ConfigureAwait(false) could be useful if you’re trying to do a small amount of parallel processing of a request, but really TPL is a better fit for most of those scenarios.

What if I really do need “sync over async”?

In the case of async/await, this typically means making sure that any awaits inside of the asynchronous implementation you’re calling are using ConfigureAwait(false) on all await points; this will prevent the await from trying to marshal back to the current SynchronizationContext.

As a library implementer, it’s a best practice to always use ConfigureAwait(false) on all of your awaits, unless you have a specific reason not to; this is good not only to help avoid these kinds of deadlock problems, but also for performance, as it avoids unnecessary marshaling costs.

Await a minute – that isn’t async!

When you call. Result on an incomplete Task, the thread executing the method has to sit and wait for the task to complete, which blocks the thread from doing any other useful work in the meantime. This negates the benefit of the asynchronous nature of the task.

Asp.net pipeline

UseStatusCodePagesWithRedirects

And here is automatic redirection

This method will not redirect to status page, it will just print it:

Another usage

Logging

 

Leave a Reply

Your email address will not be published. Required fields are marked *