Skip to content

Commit db7adb4

Browse files
committed
Use GetREquiredService
1 parent 632ec54 commit db7adb4

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

AspNetCoreFromScratch/RoutingMiddleware.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Net;
22
using System.Text;
33
using System.Text.Json;
4+
using Microsoft.Extensions.DependencyInjection;
45

56
namespace AspNetCoreFromScratch;
67

@@ -29,7 +30,7 @@ public async Task InvokeAsync(HttpListenerContext context, Func<Task> next)
2930
var requestObj = JsonSerializer.Deserialize(requestBody, parameterType);
3031

3132
// Fetch the controller from the DI container.
32-
var controllerInstance = _serviceProvider.GetService(controllerAction.Controller);
33+
var controllerInstance = _serviceProvider.GetRequiredService(controllerAction.Controller);
3334

3435
// Invoke the controller method and get the result.
3536
var actionResult = controllerAction.Method.Invoke(controllerInstance, new[] { requestObj });

0 commit comments

Comments
 (0)