diff --git a/aspnetcore/fundamentals/app-state.md b/aspnetcore/fundamentals/app-state.md index 89e5272969c4..cdb49218165b 100644 --- a/aspnetcore/fundamentals/app-state.md +++ b/aspnetcore/fundamentals/app-state.md @@ -163,6 +163,33 @@ Note: Since keys into `Items` are simple strings, if you are developing middlewa +## 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();`. +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)