Skip to content

Tag: .Net Core

Build and Deploy an ASP .Net Core Web Application as Docker Container using Microsoft Azure – Part 1

This article is the first in the series where we are going to build a simple ASP .Net Core web application, containerize it with Docker and run it on local host. And we will push everything to GitHub for later use.In later posts, we will setup Microsoft Azure DevOps Build pipeline to automate the process of building and pushing the Docker image to Docker Hub. Next, we will use Azure DevOps Release pipeline to deploy our application on Azure Web App Service as a container.

A better approach to use HttpContext outside a Controller in .Net Core 2.1

Introduction In my previous post we discussed an approach to access the HttpContext.Session in RequestHandler. However, there was a problem. For every single component where we need to access the session, we have to inject a dependency of IHttpContextAccessor. While for one or two components it’s not a problem, it can be very daunting if we have to do the same over and over again. In this article, we will use a different…

Using HttpContext outside a Controller in .Net Core 2.1

  Introduction Last Friday, while working on a web application based on ASP.Net Core 2.1, I came across a scenario where I had to put some data into memory. While that’s not a problem, the catch is that the data has to be unique for each Http Session. Consider it as keeping a key that is to be used across different views to display some…

Composed Relationship Types Func<Owned<T>>

We often end up in situations where injecting a service using simple relationship types like Func<T>, Lazy<T>, Owned<T> and others, doesn’t serve the purpose. For instance, there can be a scenario where we want to control the lifetime of a component and yet the initialization of the component takes place at run time. Now, for such a scenario, injecting the service as either Func<T> or…

Func <T> – Dynamic Instantiation

Think of a scenario where we want more than one instance of a given service, or the decision, whether to instantiate a component is made at run time. For such scenarios injecting a service as a Direct or Lazy dependency will not be enough. However, injecting the service dependency as a Func do.   Using Func <T> Consider the following example, where we have a PaymentProcessor…

Owned<T> – Controlled Lifetime with Autofac

In general, Autofac controls the lifetime of components. It automatically disposes off all the disposable components at the end of lifetime scope. However, that may mean a component is held onto for too long. Generally speaking, an owned dependency corresponds to some unit of work performed by the dependent component.   Releasing an Owned<T> We use an owned dependency when we want to control the disposal…

Using Lazy<T, TMetadata> in Dependency Injection

We know how to use a Lazy<T> and IEnumerable<T>. Recently, I came across a piece of code that uses these two in combination. However, it doesn’t make sense if we simply inject a dependency of IEnumerable<Lazy<T>>. Therefore, we need some data to decide which one of the registered implementations, returned by the container, shall be instantiated. This data is generally known as the Metadata. In…

Direct Dependencies & Enumeration<T>

In this post, we will be talking about the two types of relationships, namely, direct dependency relationship and enumeration. In terms of a component A and service B, these relationships can be stated as the following: Direct Dependency – A needs B Enumeration – A needs all the kinds of B   Direct Dependency (B) It’s quite common to have a direct dependency relationship –  component A needs service B.…

Being lazy with Lazy<T>

Being lazy doesn’t refer to being lazy in life, but being lazy about the creation or instantiation of objects in our code. Sometimes, creation of an object is very costly. Now, when we say “costly”, we are not talking about money. Rather, we are talking about the memory consumed in the process and by the object itself. Consider a component A that uses a service…