Skip to content

Commit 9df28d2

Browse files
troydaiRick-Anderson
authored andcommitted
Update session sample and documentation (dotnet#1517)
1 parent a6f25ed commit 9df28d2

File tree

7 files changed

+83
-76
lines changed

7 files changed

+83
-76
lines changed

aspnet/fundamentals/app-state.rst

+21-22
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,19 @@ For example, some simple :doc:`middleware` could add something to the ``Items``
7575
.. code-block:: c#
7676
7777
app.Use(async (context, next) =>
78-
{
78+
{
7979
// perform some verification
8080
context.Items["isVerified"] = true;
8181
await next.Invoke();
82-
});
82+
});
8383
8484
and later in the pipeline, another piece of middleware could access it:
8585

8686
.. code-block:: c#
8787
8888
app.Run(async (context) =>
8989
{
90-
await context.Response.WriteAsync("Verified request? "
91-
+ context.Items["isVerified"]);
90+
await context.Response.WriteAsync("Verified request? " + context.Items["isVerified"]);
9291
});
9392
9493
.. note:: Since keys into ``Items`` are simple strings, if you are developing middleware that needs to work across many applications, you may wish to prefix your keys with a unique identifier to avoid key collisions (e.g. "MyComponent.isVerified" instead of just "isVerified").
@@ -108,7 +107,7 @@ ASP.NET ships with several implementations of ``IDistributedCache``, including a
108107

109108
.. code-block:: c#
110109
111-
services.AddCaching();
110+
services.AddDistributedMemoryCache();
112111
services.AddSession();
113112
114113
Then, add the following to ``Configure`` and you're ready to use session in your application code:
@@ -152,13 +151,15 @@ Once session is installed and configured, you refer to it via HttpContext, which
152151
153152
public interface ISession
154153
{
155-
Task LoadAsync();
156-
Task CommitAsync();
157-
bool TryGetValue(string key, out byte[] value);
158-
void Set(string key, byte[] value);
159-
void Remove(string key);
160-
void Clear();
161-
IEnumerable<string> Keys { get; }
154+
bool IsAvailable { get; }
155+
string Id { get; }
156+
IEnumerable<string> Keys { get; }
157+
Task LoadAsync();
158+
Task CommitAsync();
159+
bool TryGetValue(string key, out byte[] value);
160+
void Set(string key, byte[] value);
161+
void Remove(string key);
162+
void Clear();
162163
}
163164
164165
Because``Session`` is built on top of ``IDistributedCache``, you must always serialize the object instances being stored. Thus, the interface works with ``byte[]`` not simply ``object``. However, there are extension methods that make working with simple types such as ``String`` and ``Int32`` easier, as well as making it easier to get a byte[] value from session.
@@ -184,7 +185,7 @@ The associated sample application demonstrates how to work with Session, includi
184185
:language: c#
185186
:lines: 15-23
186187
:dedent: 8
187-
:emphasize-lines: 3,5-8
188+
:emphasize-lines: 2,6
188189

189190
When you first navigate to the web server, it displays a screen indicating that no session has yet been established:
190191

@@ -195,7 +196,7 @@ This default behavior is produced by the following middleware in *Startup.cs*, w
195196
.. literalinclude:: app-state/sample/src/AppState/Startup.cs
196197
:linenos:
197198
:language: c#
198-
:lines: 75-103
199+
:lines: 77-107
199200
:dedent: 12
200201
:emphasize-lines: 4,6,8-11,28-29
201202

@@ -204,23 +205,21 @@ This default behavior is produced by the following middleware in *Startup.cs*, w
204205
.. literalinclude:: app-state/sample/src/AppState/Model/RequestEntry.cs
205206
:linenos:
206207
:language: c#
207-
:lines: 5-10
208+
:lines: 3-
208209
:dedent: 4
209210

210211
.. literalinclude:: app-state/sample/src/AppState/Model/RequestEntryCollection.cs
211212
:linenos:
212213
:language: c#
213-
:lines: 7-
214+
:lines: 6-
214215
:dedent: 4
215216

216-
.. note:: The types that are to be stored in session must be marked with ``[Serializable]``.
217-
218217
Fetching the current instance of ``RequestEntryCollection`` is done via the ``GetOrCreateEntries`` helper method:
219218

220219
.. literalinclude:: app-state/sample/src/AppState/Startup.cs
221220
:linenos:
222221
:language: c#
223-
:lines: 107-122
222+
:lines: 109-124
224223
:dedent: 8
225224
:emphasize-lines: 4,8-9
226225

@@ -239,7 +238,7 @@ Establishing the session is done in the middleware that handles requests to "/se
239238
.. literalinclude:: app-state/sample/src/AppState/Startup.cs
240239
:linenos:
241240
:language: none
242-
:lines: 54-73
241+
:lines: 56-75
243242
:dedent: 12
244243
:emphasize-lines: 2,8-14
245244

@@ -248,7 +247,7 @@ Requests to this path will get or create a ``RequestEntryCollection``, will add
248247
.. literalinclude:: app-state/sample/src/AppState/Startup.cs
249248
:linenos:
250249
:language: c#
251-
:lines: 124-130
250+
:lines: 126-132
252251
:dedent: 8
253252
:emphasize-lines: 6
254253

@@ -259,7 +258,7 @@ The sample includes one more piece of middleware worth mentioning, which is mapp
259258
.. literalinclude:: app-state/sample/src/AppState/Startup.cs
260259
:linenos:
261260
:language: c#
262-
:lines: 40-53
261+
:lines: 42-54
263262
:dedent: 12
264263
:emphasize-lines: 2,13
265264

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
{
2-
"projects": [ "src", "test" ],
3-
"sdk": {
4-
"version": "1.0.0-rc1-final"
5-
}
2+
"projects": [ "src", "test" ]
63
}

aspnet/fundamentals/app-state/sample/src/AppState/Model/RequestEntry.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
using System;
2-
3-
namespace AppState.Model
1+
namespace AppState.Model
42
{
5-
63
public class RequestEntry
74
{
85
public string Path { get; set; }

aspnet/fundamentals/app-state/sample/src/AppState/Model/RequestEntryCollection.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Linq;
43

54
namespace AppState.Model
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.IO;
2+
using Microsoft.AspNetCore.Hosting;
3+
4+
namespace AppState
5+
{
6+
public class Program
7+
{
8+
public static void Main(string[] args)
9+
{
10+
var host = new WebHostBuilder().UseKestrel()
11+
.UseContentRoot(Directory.GetCurrentDirectory())
12+
.UseStartup<Startup>()
13+
.Build();
14+
15+
host.Run();
16+
}
17+
}
18+
}

aspnet/fundamentals/app-state/sample/src/AppState/Startup.cs

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
11
using System;
2-
using Microsoft.AspNet.Builder;
3-
using Microsoft.AspNet.Http;
2+
using AppState.Model;
3+
using Microsoft.AspNetCore.Builder;
4+
using Microsoft.AspNetCore.Hosting;
5+
using Microsoft.AspNetCore.Http;
46
using Microsoft.Extensions.DependencyInjection;
57
using Microsoft.Extensions.Logging;
6-
using AppState.Model;
7-
using Microsoft.AspNet.Hosting;
88
using Newtonsoft.Json;
99

1010
namespace AppState
1111
{
1212
public class Startup
1313
{
14-
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
1514
public void ConfigureServices(IServiceCollection services)
1615
{
17-
services.AddCaching();
16+
services.AddDistributedMemoryCache();
1817

1918
services.AddSession(options =>
2019
{
2120
options.IdleTimeout = TimeSpan.FromSeconds(10);
2221
});
2322
}
2423

25-
public void Configure(IApplicationBuilder app,
26-
IHostingEnvironment env,
27-
ILoggerFactory loggerFactory)
24+
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
2825
{
29-
loggerFactory.MinimumLevel = LogLevel.Debug;
3026
loggerFactory.AddConsole(LogLevel.Debug);
31-
app.UseIISPlatformHandler();
27+
3228
if (env.IsDevelopment())
3329
{
3430
app.UseDeveloperExceptionPage();
3531
}
3632

33+
app.Use(async (context, next) =>
34+
{
35+
context.Items["entry_time"] = DateTime.Now;
36+
await next.Invoke();
37+
});
38+
3739
// don't count favicon requests
3840
app.Map("/favicon.ico", ignore => { });
3941

@@ -43,7 +45,7 @@ public void Configure(IApplicationBuilder app,
4345
subApp.Run(async context =>
4446
{
4547
await context.Response.WriteAsync("<html><body>");
46-
await context.Response.WriteAsync("Requested at: " + DateTime.Now.ToString() + "<br>");
48+
await context.Response.WriteAsync("Requested at: " + DateTime.Now.ToString("hh:mm:ss.ffff") + "<br>");
4749
await context.Response.WriteAsync("This part of the application isn't referencing Session...<br><a href=\"/\">Return</a>");
4850
await context.Response.WriteAsync("</body></html>");
4951
});
@@ -65,10 +67,10 @@ public void Configure(IApplicationBuilder app,
6567
{
6668
context.Session.SetString("StartTime", DateTime.Now.ToString());
6769
}
70+
6871
await context.Response.WriteAsync("<html><body>");
6972
await context.Response.WriteAsync($"Counting: You have made {collection.TotalCount()} requests to this application.<br><a href=\"/\">Return</a>");
7073
await context.Response.WriteAsync("</body></html>");
71-
7274
});
7375
});
7476

@@ -128,8 +130,5 @@ private void SaveEntries(HttpContext context, RequestEntryCollection collection)
128130

129131
context.Session.Set("RequestEntries", serializedResult);
130132
}
131-
132-
// Entry point for the application.
133-
public static void Main(string[] args) => Microsoft.AspNet.Hosting.WebApplication.Run<Startup>(args);
134133
}
135134
}
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
{
2-
"webroot": "wwwroot",
3-
"version": "1.0.0-*",
4-
5-
"dependencies": {
6-
"Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final",
7-
"Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
8-
"Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
9-
"Microsoft.AspNet.Session": "1.0.0-rc1-final",
10-
"Microsoft.Extensions.Caching.Memory": "1.0.0-rc1-final",
11-
"Microsoft.Extensions.Logging": "1.0.0-rc1-final",
12-
"Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
13-
"Newtonsoft.Json": "7.0.1"
2+
"buildOptions": {
3+
"emitEntryPoint": true
144
},
15-
16-
"commands": {
17-
"web": "Microsoft.AspNet.Server.Kestrel"
5+
"dependencies": {
6+
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
7+
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
8+
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
9+
"Microsoft.AspNetCore.Session": "1.0.0",
10+
"Microsoft.Extensions.Caching.Memory": "1.0.0",
11+
"Microsoft.Extensions.Logging": "1.0.0",
12+
"Microsoft.Extensions.Logging.Console": "1.0.0",
13+
"Newtonsoft.Json": "9.0.1"
1814
},
19-
2015
"frameworks": {
21-
"dnx451": { },
22-
"dnxcore50": { }
16+
"net451": {},
17+
"netcoreapp1.0": {
18+
"dependencies": {
19+
"Microsoft.NETCore.App": {
20+
"version": "1.0.0",
21+
"type": "platform"
22+
}
23+
}
24+
}
2325
},
24-
25-
"exclude": [
26-
"wwwroot",
27-
"node_modules"
28-
],
29-
"publishExclude": [
30-
"**.user",
31-
"**.vspscc"
32-
]
33-
}
26+
"publishOptions": {
27+
"include": [
28+
"wwwroot"
29+
]
30+
}
31+
}

0 commit comments

Comments
 (0)