Skip to content

Commit 06856c5

Browse files
committed
add request services
1 parent 06b9c1f commit 06856c5

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

aspnetcore/migration/fx-to-core/areas/hosting.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ This extension method is an internal method that will be source generated depend
7070

7171
- WebForms and handlers using the <xref:System.Web.HttpRuntime.WebObjectActivator>
7272
- ASP.NET MVC using <xref:System.Web.Mvc.DependencyResolver>
73-
- ASP.NET WebApi using [DependencyResolver](https://learn.microsoft.com/en-us/previous-versions/aspnet/hh969140(v=vs.108))
73+
- ASP.NET WebApi using [DependencyResolver](previous-versions/aspnet/hh969140)
7474

7575
### Registering services
7676

@@ -116,6 +116,15 @@ public class HomeController : Controller
116116
}
117117
```
118118

119+
Within a request, a scoped set of services may be retrieved by a special extension method:
120+
121+
```csharp
122+
var myService = HttpContext.Current.GetRequestServices().GetService(typeof(ISomeType));
123+
```
124+
125+
> [!WARNING]
126+
> <xref:System.Web.HttpContext> implements <xref:System.IServiceProvider> so you may see extension methods to get services from it. However, it is not implemented in a way to retrieve any useful services from a registered container. You will see an error if you attempt to use it and you should instead use the `GetRequestServices()` extension method first.
127+
119128
## Logging
120129

121130
The generic host integrates `Microsoft.Extensions.Logging`, providing a modern logging infrastructure for your ASP.NET Framework application.
@@ -191,6 +200,8 @@ var configValue = AppConfiguration.GetSetting("SomeSettings");
191200
var connStr = AppConfiguration.GetConnectionString("connection-string-name");
192201
```
193202

203+
This will work on both ASP.NET Core and ASP.NET Framework applications that are referencing `Microsoft.AspNetCore.SystemWebAdapters` and will provide access to the current `IConfiguration`.
204+
194205
### Using options pattern
195206

196207
The options pattern provides strongly-typed access to configuration sections:

0 commit comments

Comments
 (0)