These days I’m working on a small personal project with ASP.Net Core and this allowed me play around with the ConfigureServices method in Startup.cs and discover some things I wasn’t aware of. So I though on sharing my experiments here to see what others have to say about them.
Even if the application itself is fairly simple I decided to create several different projects in a solution to keep things open for extensions, right? So what I have is an ASP.Net Core API project and a bunch of different other projects like Sample.Core, Sample.Infrastructure, Sample.Dal. For the data access layer I wanted to play around with the repository pattern and created a very simple and insecure repository to handle operations on the Azure Table Storage service. My idea was to keep thinks as simple and possible and use each project for classes, interfaces and so on that are really specific to the scope of the project. So in the DAL project I handled everything related to the communication with Azure Table Storage and registered my “Azure context” and repository in my API project using the ConfigureServices() method in Startup.cs.
Nothing really special to this point. Then in the infrastructure project I wanted to create business logic services, like creating unique keys and so on. So playing around with this, at a certain point I thought that I need the repository also in one of my services that were part of the infrastructure layer to dump some analytics information to Azure Table Storage. So how to do that? Well, theoretically I could create a new instance of the repository and use it. But I felt like this is really out of what ASP.Net Core is all about: making services available through dependency injection. A crazy idea came to my mins: what if services can be simply injected in another service?
In the infrastructure project I created a new class to be used as TestService:
[code language=”csharp”]
public class TestService
{
public async Task<int> Add(int number, int number2)
{
return number + number2;
}
}
[/code]
And I added a constructor and a private field to inject my repository:
[code language=”csharp”]
public class TestService
{
private ITableRepository _repo;
public TestService(ITableRepository repo)
{
_repo = repo;
}
public async Task<int> Add(int number, int number2)
{
var something = await _repo.GetByPartitionKeyAndRowkey(“partKey”, “rowKey”);
return number + number2;
}
}
[/code]
Then I hopped over to my API project and added a new service in ConfigureServices() within Startup.cs:
[code language=”csharp”]
services.AddScoped<TestService, TestService>();
[/code]
Finally, I went to a test controller where I injected my TestService and gave to it the terrible task of adding 1 and 2:
[code language=”csharp”]
_ts.Add(1, 2);
[/code]
And when setting a break point, it turns out I have my repository in the TestService:
So I found out that we can basically inject registered services in other services, which I assume is a basic ASP.Net Core feature but I was not aware of it. Further, services that are registered in the ConfigureServices() method can be injected in custom middleware classes in the Invoke() method. This is however a topic for another blog post on how to write custom middleware, which is on my to do list. I hope this information might be helpful to one or another. If you have any feedback, don’t be shy and drop a comment 🙂 And if you thing this information is useful, share it. Cheers!
How useful was this post?
Click on a star to rate it!
Average rating / 5. Vote count:
Dan Patrascu-Baba
Latest posts by Dan Patrascu-Baba (see all)
- Configuration and environments in ASP.NET Core - 25/11/2019
- GraphQL in the .NET ecosystem - 19/11/2019
- A common use case of delegating handlers in ASP.NET API - 12/11/2019