Using Generated Methods Instead of Reflection


It is a common thing to say that reflection is slow.You will find tons of posts saying this, and I generally tend to agree to them, although in most cases we generally don’t need to care that much – in fact, so many libraries and frameworks that we rely on daily depend on it!

When we talk about reflection, what is it that we talk about, normally? I’d say:

  • Getting an instance’s type, or base type, or implemented interfaces
  • Getting a list of properties, or fields
  • Setting values for properties, or fields
  • Finding methods
  • Invoking methods

Some things we can do to speed up the process include caching the PropertyInfo and FieldInfo objects, so that there is no need to retrieve them again and again from the declaring Type, but when it comes to getting and setting actual values, no caching can help here.

One thing that we can do, though, is generate code for actually accessing the properties/fields for us, in a strongly typed manner, without using reflection. This incurs in a startup cost, because of the overhead associated with the code generation, but after that it becomes much faster than the reflection-based code. Without more ado, let’s jump to the code.

First, some base class with just a single method for representing a simple setter operation:

public abstract class Setter {    

public abstract void Set(object obj, string propertyName, object value);


Simple, right? We take a target object (obj), a property (propertyName) and some value, and we’re good.


The simplest, reflection-based implementation could look like this (ReflectionSetter):

public sealed class ReflectionSetter : Setter


     public override void Set(object obj, string propertyName, object value)




         var property = obj.GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.SetProperty);

         if (property != null)


             property.SetValue(obj, value, null);




             throw new InvalidOperationException(“Property not found.”);


     } }

As you can see, nothing special here, the code does what you might expect: only public, instance properties with both a setter and a getter are looked for, and if not found, an exception is thrown.

Reflection With Caching

Another, slightly more elaborared version, which caches the PropertyInfo objects, but which requires that all types be initialized prior to being used (CachedReflectionSetter):

public sealed class CachedReflectionSetter : Setter


     private readonly Dictionary<Type, Dictionary<string, PropertyInfo>> _properties = new Dictionary<Type, Dictionary<string, PropertyInfo>>();

     public void Initialize(Type type)



         this._properties[type] = new Dictionary<string, PropertyInfo>();

         foreach (var prop in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.SetProperty))


             this._properties[type][prop.Name] = prop;



     public override void Set(object obj, string propertyName, object value)




         var property = this.GetPropertyFor(obj.GetType(), propertyName);

         if (property != null)


             property.SetValue(obj, value, null);




             throw new InvalidOperationException(“Property not found.”);



     private PropertyInfo? GetPropertyFor(Type type, string propertyName)


         if (this._properties.TryGetValue(type, out var properties))


             if (properties.TryGetValue(propertyName, out var prop))


                 return prop;



         return null;

     } }

The only difference between the two is that this one does not retrieve the PropertyInfo objects on the fly from the passed instance, instead it does so from a dictionary. But one must not forget to call its Initialize method with all the Types that we want to use it with, otherwise, it will not know its properties.

Compiled Lambda

Now things get a little more complext, as we want to get away from reflection – at leat at the runtime level, when we are setting the value for the property, which is something that can potentially happen a lot of times during the lifefime of our application. So, enter the third iteration (CompiledSetter):

public sealed class CompiledSetter : Setter {

     private readonly Dictionary<Type, Dictionary<string, Delegate>> _properties = new Dictionary<Type, Dictionary<string, Delegate>>();

     public void Initialize(Type type)



         this._properties[type] = new Dictionary<string, Delegate>();

         foreach (var prop in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.SetProperty))


            this.GenerateSetterFor(type, prop);



     public override void Set(object obj, string propertyName, object value)




         var action = this.GetActionFor(obj.GetType(), propertyName);

         if (action is Action<object, object> act)


                act(obj, value);




             throw new InvalidOperationException(“Property not found.”);



     private void GenerateSetterFor(Type type, PropertyInfo property)


         var propertyName = property.Name;

         var propertyType = property.PropertyType;

         var parmExpression = Expression.Parameter(typeof(object), “it”);

         var castExpression = Expression.Convert(parmExpression, type);

         var propertyExpression = Expression.Property(castExpression, propertyName);

         var valueExpression = Expression.Parameter(typeof(object), propertyName);

         var operationExpression = Expression.Assign(propertyExpression, Expression.Convert(valueExpression, propertyType));

         var lambdaExpression = Expression.Lambda(typeof(Action<,>).MakeGenericType(typeof(object), typeof(object)), operationExpression, parmExpression, valueExpression);

         var action = lambdaExpression.Compile();

         this._properties[type][propertyName] = action;


     private Delegate? GetActionFor(Type type, string propertyName)


         if (this._properties.TryGetValue(type, out var properties))


             if (properties.TryGetValue(propertyName, out var action))


                 return action;



         return null;

     } }

The approach here is to generate an expression for a lambda that accesses the property that we want to access and then compile it. The problem is that it would normally produce something like this:

Action<MyEntity> (it) => it.MyProperty = MyValue;

Which is not what we want, essentially, because with this we cannot call it dynamically – we do not know, at runtime, the MyEntity type or the type of MyProperty! What we want, and always have, is object, so, instead, we want to go with something along these lines:

Action<object, object> (it, prop) => ((MyEntity) it).MyProperty = (MyPropertyType)prop;

This way, we can always call the generated lambda with the target object and we

And it works like a charm! Winking smile

Performance Tests

I did a few tests with BenchmarkDotNet:

public class Entity


public int Id { get; set; }

public string Name { get; set; }


public class Test {     static CompiledSetter compiledSetter = new CompiledSetter();     static ReflectionSetter reflectionSetter = new ReflectionSetter();     static CachedReflectionSetter cachedReflectionSetter = new CachedReflectionSetter();     static Entity[]? entities = null;     [GlobalSetup]     public static void Setup()     {         compiledSetter.Initialize(typeof(Entity));         cachedReflectionSetter.Initialize(typeof(Entity));         entities = Enumerable.Range(0, 200).Select(x => new Entity()).ToArray();     }     private static void Common(Setter setter)     {         for (var i = 0; i < entities?.Length; i++)         {             setter.Set(entities[i], nameof(Entity.Id), i);             setter.Set(entities[i], nameof(Entity.Name), i.ToString());         }     }     [Benchmark]     public static void TestCompiled() => Common(compiledSetter);     [Benchmark]     public static void TestReflection() => Common(reflectionSetter);     [Benchmark]     public static void TestCachedReflection() => Common(cachedReflectionSetter); }

Essentially, the tests generated a few (200) entities with two properties, the setters that required initialization were initialized, and then the properties were set in a loop.

The results I got were:


As you can see, the compiled version (CompiledSetter) outperformed the other two by a great margin. There’s also a small benefit in caching the PropertyInfo, as CachedReflectionSetter version arrived second on the results.


It is clear that reflection is indeed slow and a few things can definitely be done about it. I showed here setting property values, but the same logic can be done for getting them, and I’m happy to provide the code to anyone interested. If you wish to discuss any aspects of this, or have any questions, just give me a shout. As always, hope you find this useful!

Performance in .NET – Part 4


This is my fourth post on performance in the .NET world. See the first one on object instantiation here and the second on property copying here and the third here. This time I’m going to talk about collections, but focusing on the performance side.This time, I’ll be talking about value types.

Value Types versus Reference Types

Value types – structs and enums – are always allocated in the stack, as opposed to reference types – classes – , which are allocated in the heap. This means that value types are automatically released from memory when they go out of scope – end of the block/method where they are declared, or the class is garbage collected, which is more rare. Value types are thus cheaper to create and do not need to be checked by the garbage collector.

Some aspects, though, need attention.

Instance Comparison

When you compare two value types using the Equals method, if there is no override for it, the value type is compared byte by byte. This is, as you can image, pretty inefficient. Is is recommended that you implement your own Equals (and GetHashCode too) and also that you implement IEquatable<T> interface for comparing two instances of your value type without incurring into boxing and unboxing.

Usage in Lists

Storing value types in array-based lists that permit reordering (random insertions and deletions), such as List<T> is painful, because of the items need to be copied, and copying for value types means byte by byte copying. Avoid if possible.

Usage in Arrays

Value types are great for usage in arrays, because a value type has no object header, so it’s size in memory is very small. The size of the array is therefore small when compared to the same array of reference types.


Do use value types as much as possible, but stay aware of the problems. As always, looking forward to hearing your thoughts. I’ll be back for more.

C# 8.0 and .NET Core 3.0 – Modern Cross-Platform Development – Fourth Edition Review

C# 8.0 and .NET Core 3.0 – Modern Cross-Platform Development

This is a book written by Mark J. Price for Packt Publishing. I must say that I don’t know Mark, but I was asked by Pack to review this book, which I gladly did!

The topics covered in this book are vast, so it should be no surprise the size of it: more than 800 pages. It spawns across 21 chapters, each of them ends with some exercises. It talks about pretty much everything .NET, as you can see:

Chapter 1

Introduction to Visual Studio Code and Visual Studio 2019 for Windows and Mac. Presents some useful extensions for Visual Studio Code. Talks a bit about the .NET Framework and its branches and offspring, Xamarin, Mono and .NET Core. Also explains the intermediate language that .NET compiles to and how to produce native code from .NET. Also shows how to use Visual Studio Code to get code from GitHub and build console apps. Finally, it gives some pointers on how to get support from the Internet.

Chapter 2

Presents all the C# versions from 1 until 8, the last one to date. Shows how to enable a specific version on a .NET project. Gives a an overview of the unchanging C# language features, like variables, data types, reference and value types, and presents a simple example on how to get input from the user.

Chapter 3

Expands on the C# language started on the previous chapter introducing branching, conditions, pattern matching, assignment, operators, loops and casts.

Chapter 4

Introduces functions, how to debug and unit test them.

Chapter 5

Explains Object-Oriented Programming with C#. How references to assemblies and namespaces work. Field and property modifiers. How to use the return values of functions, including tuples. Method overloading, optional parameters. Partial classes. Properties with indexers and different levels of access.

Chapter 6

Talks about interfaces and type inheritance. Explains events and delegates. Generic types. Reference and value types. The dispose pattern. Member overriding and hiding. Preventing inheritance. Polymorphism. Casting. Extension methods.

Chapter 7

Packaging .NET components in assemblies and NuGet packages. The .NET Standard and .NET Core. Publishing applications. Decompiling assemblies. Publishing to NuGet. Porting to .NET Core.

Chapter 8

Common .NET types and operations: numbers, string, regular expressions, collections, spans, indexes, ranges, network resources, types, attributes. Internationalization.

Chapter 9

Files, file streams and serialization. Working with the filesystem. Text encoding. XML and JSON serialisation and compression.

Chapter 10

Data encryption and decryption. Data hashing and signing. Random number generation. User authentication and authorisation.

Chapter 11

Database programming with Entity Framework Core.

Chapter 12

Using LINQ. Custom LINQ methods. LINQ to XML. Parallel LINQ.

Chapter 13

Performance monitoring. Tasks. Synchronizing access to shared resources. async and await.

Chapter 14

ASP.NET Core web applications. SignalR. Blazor.

Chapter 15

ASP.NET Core Razor Pages. Using EF Core with ASP.NET Core. Razor Class Libraries.

Chapter 16


Chapter 17

Using Content Management Systems (Piranha CMS).

Chapter 18

ASP.NET Core Web API. Swagger and Open API. Health checks. WCF and gRPC are mentioned briefly.

Chapter 19

Machine learning with ML.NET.

Chapter 20

Windows Forms apps. Windows Presentation Foundation apps with .NET Core and Windows Compatibility Pack. The XAML Standard. Modern Windows apps.

Chapter 21

Using Xamarin for building cross-platform mobile apps. Calling web services.


As you can see, this is a lot, and goes from the plain C# language to machine learning and Blazor. I’d say that some topics, such as Content Management Systems, could have been dropped, but other than that, pretty much everything that a developer longing to learn .NET could wish for is here. A great deal of ASP.NET Core, which is good, as it is for sure the strong part of .NET Core. So, if you’re one such developer, this is one book that you may want to get, you won’t feel disappointed! Definitely a good value for money!

Now Reading: C# 8.0 and .NET Core 3.0 – Modern Cross-Platform Development – Fourth Edition

Update: see the review here.

I am now reading C# 8.0 and .NET Core 3.0 – Modern Cross-Platform Development – Fourth Edition, by Mark J. Price, from Packt Publishing. Expect a review from it very soon.

C# 8.0 and .NET Core 3.0 - Modern Cross-Platform Development - Fourth Edition

@MarkJPrice @RavitJain @PacktPub

TypeScript for C# and .NET Core Developers Review

I finished reading Hands-On TypeScript for C# and .NET Core Developers by Francesco Abbruzzese (@f_abbruzzese) for Packt Publishing. As the name states, it is about TypeScript (and JavaScript) and also very much about Angular.

The book is structured like this:

  1. First chapter explains what is TypeScript (version 2.8.3), how to install it using NPM or the SDK, how to create your first project, basic configuration options, the type system and syntax; at all times, it relates the TypeScript syntax with the recent ECMAScript versions, of which TypeScript is a superset
  2. The second one talks about type declaration, including interfaces, classes, unions, tuples, arrays and so on. It also covers operations over types, such as destructuring and spreads. Finally, it presents functions in TypeScript, how to mimic overloading and have optional arguments
  3. Chapter 3 covers DOM manipulation. This is probably something that seasoned web/JavaScript developers are quite familiar with, but, most importantly, it also introduces declaration files
  4. In chapter 4 we learn how to make effective use of classes and interfaces, declare visibility levels and modifiers, and how type compatibility works
  5. Generics is the topic of chapter 5, how to declare generic types and functions and how to enforce constraints
  6. This chapter talks about modules and namespaces, the different ways by which we can resolve and load modules, and also about TypeScript type declarations, which are used to call untyped JavaScript code
  7. Here we learn how to integrate the WebPack bundler and minifier with ASP.NET Core and how we can enable debugging of the source code in Visual Studio
  8. A very important chapter, here we get an overview on how to write reusable libraries and make them available on NPM. Testing goes together with reusability, so we learn how to use Jasmine to unit test our code
  9. In this chapter we learn about symbols, the TypeScript equivalent to decorators in Aspect-Oriented Programming, generator functions and iterators and also promises, used for asynchronous invocations. Not exactly related, but it also covers the fetch API, used for modern AJAX-style interactions
  10. Chapter 10 presents the ASP.NET Core project template for Angular using TypeScript, describes the Angular architecture and key concepts
  11. This chapter teaches us how to interact with form fields, including validations, how the Angular lifecycle works and how to achieve two-way communication, data binding and piping between components
  12. This one presents some advanced Angular features, such as custom attribute and structural directives, which are then used to create animations by doing DOM manipulation
  13. Lastly, this chapter explains what is dependency injection and its benefits and how we can leverage it with TypeScript and Angular. It also describes how we can localize messages and use HTTP-related modules for interaction with external services. Most importantly, it presents the basis of Angular routing and navigation, a must-have for any complex application, and ends with an overview of testing for

At the end of each chapter there is a summary which highlights the key aspects that were introduced in it, poses 10 questions, which are answered in the Assessments chapter.

The book covers TypeScript 2.8.3, which is relatively old by now, but given the pace that TypeScript versions come out, it can hardly surprise us. Some new stuff in TS is missing, of course, but I guess this will always happen. It is essentially a book about TypeScript and Angular, but of course also covers Node.js and, obviously, JavaScript itself. .NET Core here is discussed as leverage to deploy and compile client-side code. The book is quite comprehensive and was actually an interesting read and I definitely learnt a lot.

The source code can be found in GitHub here:

If you got interested, please go get if from the Packt Publishing site:

Now Reading: Hands-On TypeScript for C# and .NET Core Developers

I started reading a book by my MVP colleague Francesco Abbruzzese (@f_abbruzzese) on C# and TypeScript: Hands-On TypeScript for C# and .NET Core Developers. So far, it seems an interesting reading! Will write a review about it here, once I finish reading it. In case you want to know more, go get if from the Packt Publishing site:

Succinctly Series Readers Awards


My e-book Entity Framework Core Succinctly was silver winner on the Succinctly Series Readers Awards!

Many thanks to all who voted for it! And congratulations to Joseph D. Booth for winning the gold award for his Natural Language Processing Succinctly and to Alessando Del Sole (@progalex) for his bronze award for Xamarin.Forms Succinctly!

Performance in .NET – Part 3


This post is part of a series on performance in .NET. See the first one on object instantiation here and the second on property copying here. This time I’m going to talk about collections, but focusing on the performance side.


Back in 2009 (!) I wrote a blog post, which I updated a couple of times, about .NET collection types. Essentially, my point was – is – that you should pick the right collection for the job that you have at hands. This post is still up to date.

How many of us don’t by default just choose List<T> when there is need for a general-purpose collection? I certainly do, at times… Well, it turns out that this may or may not be what we need. Let me give a few examples.

List<T> is an array-based collection, which means that it is probably the best if you are going to iterate through items one at a time, sequentially, but it is not so good if you want to remove items from a random position other than the list’s end, because this causes a whole new array to be instantiated in memory, and all items (except the one that you wish to remove, of course) from the original list to be copied into the new one. It’s the same problem with random inserts at any given position, other than the end, and only if the initial capacity isn’t exceeded.

For operations where random inserts and deletes are required, LinkedList<T> is a much better choice as it does not require the instantiation of new arrays and the memory copy. It does that, however, at the expense of a slightly poorer performance in list traversal.

What about duplicates? With the previous list implementations, if we don’t want to allow duplicates, we need to check one by one, which is a PITA. Luckily, the .NET BCL has an implementation of a mathematical set which automatically excludes duplicates. One implementation is HashSet<T>, which is an indexed collection that uses each object’s GetHashCode method to figure out if the object already exists in the collection; needless to say, this method must be properly implemented.

Talking about indexed collections, if we want to be able to rapidly get to one element – or a number of elements – by some key, .NET has that as well: Dictionary<TKey, TValue> for storing a single value per unique key. The key can be of whatever type we want. This one also offers good performance when it comes to retrieving, adding, removing or modifying a single item.

Then we have LIFO and FIFO implementations in the form of the Stack<T> and Queue<T> types, which are optimized for adding items at the top or the bottom of the list, respectively, and don’t even allow other kinds of operations other than traversal. Internally they also use a linked-list approach.

Bits have their own specialized collections, BitArray and BitVector32. The latter only works with up to 32 bits and it probably provides the fastest performance of the two, according to Microsoft:

BitVector32 is more efficient than BitArray for Boolean values and small integers that are used internally. A BitArray can grow indefinitely as needed, but it has the memory and performance overhead that a class instance requires. In contrast, a BitVector32 uses only 32 bits.

Finally, Microsoft makes available thread-safe collections that are thread-safe in nature and therefore do not need any thread synchronization mechanisms, which makes them faster than if we had to roll out our own thread synchronization. They include thread-safe dictionaries (ConcurrentDictionary<TKey, TValue>), queues (ConcurrentQueue<T>), stacks (ConcurrentStack<T>) and general-purpose lists (BlockingCollection<T>).


I didn’t go through all the collection classes available, for that you can refer to my previous post.

The point I want to make with this post is:

  • Pick the right collection for the job; use the 80-20 rule and try to understand what the most common usage for your collection will be; do not just go blindly with List<T> or similar
  • Always expose collections publicly though interfaces or base classes that only show the bare minimum required, so that you can swap out the implementation should you need to
  • And, of course, measure your usage so that you can make opinionated decisions.

If performance is not an issue, by all means, forget about this and keep on doing what you are already doing and works for you.

Java vs C# – Part 3


This is the third in a series of posts about the similarities and differences between C# (and .NET, to some extent) and Java. You can find the first one here and the second one here.

Again, with this I do not intent to demonstrate that one is superior to the other – totally – as I really like both platforms, even though I work mostly with C# and .NET. This is merely an exercise to show these differences and maybe it can be useful to someone who is learning one or the other. If you find something that you think is wrong, please let me know!

Keyword Usage

In Java, it is not possible to use reserved words, or keywords, such as class, public, etc, as variables, parameters or field names. In C# you can if you prefix it with @:. For example:

var @class = “My Class”;

Object Class

The Object class is the root of both type hierarchies in both Java and .NET. The two are pretty similar, with some remarkable exceptions. All of the following have exactly identical behavior:

Java .NET
clone MemberwiseClone
getClass GetType
equals Equals
hashCode GetHashCode
finalize Finalize
toString ToString

.NET does not offer methods corresponding to notify, notifyAll or wait. On the other hand, Java does not offer a method like ReferenceEquals.


Recent versions of C# lets us return tuples, which are sets of values combined together ad hoc, but not inside a type definition. For example, should you wish to return a coordinate from a method, you could do this:

(double x, double y) GetCoordinates()


double x = …;

double y = …;

return (x, y);


It is also possible to “deconstruct” a class into a tuple, by providing a proper Deconstruct method:

public class Coordinate


public void Deconstruct(out double x, out double y)


x = …;

y = …;



Coordinate coord = …;

(double x, double y) = coord;

You can have as many Deconstruct methods you like, provided their signatures are different. For now, at least, Java is still lacking this functionality.


C# lets us use the nameof keyword to obtain a strongly-typed name of a class, method, property, field or parameter. It is very useful because it is refactor-friendly: should you change the name of the target, you also change the value that is being assigned. An example:

var className = nameof(MyClass); //”MyClass”

var fieldName = nameof(MyClass.MyField); //”MyField”

Mind you, only the “final” piece is returned, for example, if you use nameof with a fully qualified type name, you’ll only get the type name.

Friend Assemblies

As you know, internal classes and their methods are not available outside the current assembly/namespace. In C#/.NET, however, we can make these internals available to other assemblies by applying the InternalsVisibleToAttribute attribute to the assembly that we want to make available. These are called friend assemblies. For example:



C# 5 introduced a new asynchronous programming model around the async and await keywords. I won’t go into the details of it but essentially it simplifies asynchronous programming a lot, preventing the usage of a lot of boilerplate code. It goes like this:

async Task<int> Compute(int a, int b) { … }

var result = await Compute(1, 2);

Import Methods

In C# we can import public static methods from public classes, which means, we can use them without prefacing them with the name of the class. The syntax is like this:

using static System.Environment;

and the usage:

var path = GetEnvironmentVariable(“PATH”);

Local Functions

.NET allows us to define local functions, that is, functions that exist only in the scope of methods. They are similar to lambda functions, with some remarkable differences, among which:

  • They can have attributes applied to its parameters
  • They can have ref, out and other kind of parameters
  • They can be asynchronous

An example:

void SomeMethod()


int sum(int a, int b) => a + b;

var x = sum(1, 2);


The closest that Java offers is anonymous classes, which are actually pretty cool, IMO.


Java offers the assert keyword as part of the language, this has no equivalent in .NET. An assert evaluates a Boolean condition which, if not found to be true, throws an error:

assert speed < SPEED_OF_LIGHT;

Forgot to say, assertions can be disabled, which means, they are turned into no-ops.

Next Steps

I still have a couple of things to talk about, so be prepared for a future post!

As always, do let me know if you think I got anything wrong or you wish me to clarify something.

.NET Core Service Provider Gotchas and Less-Known Features


In this post I’m going to talk about a few gotchas with the .NET Core’s built-in inversion of control (IoC) / service provider (SP)/dependency injection (DI) library. It is made available as the Microsoft.Extensions.DependencyInjection NuGet package.

I wrote another post some time ago, but this one supersedes it, in many ways.

Extension Methods

The single method exposed by the IServiceProvider interface, GetService, is not strongly typed. If you add a using statement for Microsoft.Extensions.DependencyInjection, you’ll get a few ones that are:

  • GetRequiredService<T>: tries to retrieve a service that is registered under the type of the generic template parameter and throws an exception if one cannot be found; if it is, it is cast to the template parameter;
  • GetService<T>: retrieves a service and casts it to the template parameter; if no service is found, null is returned;
  • GetServices<T>: returns all services registered as the template parameter type, cast appropriately.

Using a Different Service Provider

You are not forced to use the built-in service provider; you can use anyone you like, as long as it exposes an IServiceProvider implementation. You just need to return this implementation from the ConfigureServices method, which normally does not return anything:

public IServiceProvider ConfigureServices(IServiceCollection services)
//return an implementation of IServiceProvider

Why would you want to do that, you may ask? Well, there are service providers out there that offer much more interesting features than Microsoft’s (for example, more lifetimes), and this has a reason: Microsoft kept his simple on purpose.

Multiple Registrations

You may not have realized that you can register any number of implementations for a given service, even with different lifetimes:

services.AddTransient<IService, ServiceA>();
services.AddScoped<IService, ServiceB>();

So, what happens when you ask for an implementation for IService? Well, you get the last one registered, in this case, ServiceB. However, you can ask for all the implementations, if you call GetServices<T>.

Registration Factories

You can specify how a service implementation is constructed when you register a service, and it can depend upon other services that are also registered in the service provider:

services.AddTransient<IService>(sp => new ServiceImpl(sp.GetRequiredService<IOtherService>));

Don’t worry about registration order: IOtherService will only be required once IService is retrieved.

Lifetime Dependencies

You cannot have a Singleton registration depend upon a Scoped service. This makes sense, if you think about it, as a singleton has a much longer lifetime than a scoped service.

Nested Scopes

You can create nested scopes at any time and retrieve services from them. If you are using the extension methods in the Microsoft.Extensions.DependencyInjection namespace, it’s as easy as this:

using (var scope = serviceProvider.CreateScope())
    var svc = scope.ServiceProvider.GetRequiredService<IService>();

The CreateScope method comes from the IServiceScopeFactory implementation that is registered automatically by the dependency injection implementation. See next for implications of this.

Why is this needed? Because of lifetime dependencies: using this approach you can instantiate a service marked as a singleton that takes as a parameter a scoped one, inside a scope.

Dispose Pattern

All services instantiated using the Scoped or Transient lifetimes that implement the IDisposable interface will have their Dispose methods called at the end of the request – or the nested scope (when it is disposed). The root service provider is only disposed with the app itself.

Scope Validation

The built-in service provider validates the registrations so that a singleton does not depend on a scoped registration. This has the effect of preventing retrieving services in the Configure method, through IApplicationBuilder.ApplicationServices, that are not transient or singletons.

If, however, you think you know what you’re doing, you can bypass this validation:

public IServiceProvider ConfigureServices(IServiceCollection services)
//add services
return services.BuildServiceProvider(validateScopes: false);

As I said before, the other alternative is creating a scope and instantiating your singleton service inside the scope. This will always work.

Injecting Services

ASP.NET Core only supports constructor:

public HomeController(IService svc)

and parameter:

public IActionResult Index([FromServices] IService svc)

inheritance, but not property, in controllers and Razor Pages. You can achieve that through actions or conventions. Another option is to use the Service Locator pattern.

Service Locator

You can retrieve any registered services from HttpContext.RequestServices, so whenever you have a reference to an HttpContext, you’re good. From the Configure method, you can also retrieve services from IApplicationBuilder.ApplicationServices, but not scoped ones (read the previous topics). However, it is generally accepted that you should prefer constructor or parameter injection over the Service Locator approach.


Although the service provider that comes with .NET Core is OK for most scenarios, it is clearly insufficient for a number of others. These include:

  • Other lifetimes, such as, per resolve-context, per thread, etc;
  • Property injection;
  • Lazy<T> support;
  • Named registrations;
  • Automatic discovery and configuration of services;
  • Child containers.

You should consider a more featured DI library, and there are many out there, if you need any of these.