Skip to content

Commit

Permalink
Add app state data (dotnet#2664)
Browse files Browse the repository at this point in the history
* Add app state data

* typo

* Update app-state.md

* Update app-state.md

* Update app-state.md

* Update app-state.md
  • Loading branch information
Rick-Anderson authored Feb 3, 2017
1 parent 5bf976f commit 8b2d959
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions aspnetcore/fundamentals/app-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'."
Expand All @@ -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)

0 comments on commit 8b2d959

Please sign in to comment.