Add app state data (#2664)
* Add app state data * typo * Update app-state.md * Update app-state.md * Update app-state.md * Update app-state.mdpull/2669/head
parent
5bf976f7fe
commit
8b2d959352
|
@ -163,6 +163,33 @@ Note: Since keys into `Items` are simple strings, if you are developing middlewa
|
|||
|
||||
<a name=appstate-errors></a>
|
||||
|
||||
## Application state data
|
||||
|
||||
Use [Dependency Injection](xref:fundamentals/dependency-injection) to make data available to all users.
|
||||
|
||||
1. Define a service containing the data (for example, a class named `MyAppData`).
|
||||
|
||||
```csharp
|
||||
public class MyAppData
|
||||
{
|
||||
// Declare properties/methods/etc.
|
||||
}
|
||||
```
|
||||
2. Add the service class to `ConfigureServices` (for example `services.AddSingleton<MyAppData>();`.
|
||||
3. Consume the data service class in each controller:
|
||||
|
||||
```csharp
|
||||
public class MyController : Controller
|
||||
{
|
||||
public MyController(MyAppData myService)
|
||||
{
|
||||
// Do something with the service (read some data from it,
|
||||
// store it in a private field/property, etc.
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Common errors when working with session
|
||||
|
||||
* "Unable to resolve service for type 'Microsoft.Extensions.Caching.Distributed.IDistributedCache' while attempting to activate 'Microsoft.AspNetCore.Session.DistributedSessionStore'."
|
||||
|
@ -172,4 +199,5 @@ In memory caching](xref:performance/caching/memory) for more information
|
|||
|
||||
### Additional Resources
|
||||
|
||||
|
||||
* [Sample code used in this document](https://github.com/aspnet/Docs/tree/master/aspnetcore/fundamentals/app-state/sample/src/WebAppSession)
|
||||
|
|
Loading…
Reference in New Issue