Skip to content

Commit fc25568

Browse files
committed
Update the Fornax and Mortis samples to register ApplicationDbContext in the DI container
1 parent be48497 commit fc25568

File tree

6 files changed

+43
-4
lines changed

6 files changed

+43
-4
lines changed

samples/Fornax/Fornax.Server/Global.asax.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ void Application_Start(object sender, EventArgs e)
7575
options.UseOwin();
7676
});
7777

78+
// Register the Entity Framework context needed by the OpenIddict stores.
79+
services.AddScoped(static provider => ApplicationDbContext.Create());
80+
7881
// Create a new Autofac container and import the OpenIddict services.
7982
var builder = new ContainerBuilder();
8083
builder.Populate(services);

samples/Fornax/Fornax.Server/Startup.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Fornax.Server.Models;
1+
using Autofac;
2+
using Fornax.Server.Models;
23
using Microsoft.AspNet.Identity;
34
using Microsoft.AspNet.Identity.Owin;
45
using Microsoft.Owin;
@@ -16,7 +17,16 @@ public class Startup
1617
public void Configuration(IAppBuilder app)
1718
{
1819
// Register the Entity Framework context and the user/sign-in managers used by ASP.NET Identity.
19-
app.CreatePerOwinContext(ApplicationDbContext.Create);
20+
//
21+
// Note: while the regular CreatePerOwinContext() method is used to store the Entity Framework
22+
// context in the OWIN environment (so that it can be retrieved by the ASP.NET Identity components),
23+
// the factory used here simply resolves the scoped context from the Autofac container to avoid
24+
// having multiple instances of the same context created for each request. As such, the dispose
25+
// callback action is left empty, as the context will be disposed by Autofac when the request ends.
26+
app.CreatePerOwinContext<ApplicationDbContext>(
27+
createCallback: static (options, context) => Global.Provider.ApplicationContainer.Resolve<ApplicationDbContext>(),
28+
disposeCallback: static (options, context) => { });
29+
2030
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
2131
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
2232

samples/Mortis/Mortis.Client/Models/ApplicationDbContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ public ApplicationDbContext()
99
{
1010
}
1111

12+
public static ApplicationDbContext Create()
13+
{
14+
return new ApplicationDbContext();
15+
}
16+
1217
protected override void OnModelCreating(DbModelBuilder modelBuilder)
1318
{
1419
modelBuilder.UseOpenIddict();

samples/Mortis/Mortis.Client/Startup.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public void Configuration(IAppBuilder app)
7878
});
7979
});
8080

81+
// Register the Entity Framework context needed by the OpenIddict stores.
82+
services.AddScoped(static provider => ApplicationDbContext.Create());
83+
8184
// Create a new Autofac container and import the OpenIddict services.
8285
var builder = new ContainerBuilder();
8386
builder.Populate(services);

samples/Mortis/Mortis.Server/Startup.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ public void Configuration(IAppBuilder app)
7272
options.UseOwin();
7373
});
7474

75+
// Register the Entity Framework context needed by the OpenIddict stores.
76+
services.AddScoped(static provider => ApplicationDbContext.Create());
77+
7578
// Create a new Autofac container and import the OpenIddict services.
7679
var builder = new ContainerBuilder();
7780
builder.Populate(services);
@@ -85,7 +88,16 @@ public void Configuration(IAppBuilder app)
8588
var container = builder.Build();
8689

8790
// Register the Entity Framework context and the user/sign-in managers used by ASP.NET Identity.
88-
app.CreatePerOwinContext(ApplicationDbContext.Create);
91+
//
92+
// Note: while the regular CreatePerOwinContext() method is used to store the Entity Framework
93+
// context in the OWIN environment (so that it can be retrieved by the ASP.NET Identity components),
94+
// the factory used here simply resolves the scoped context from the Autofac container to avoid
95+
// having multiple instances of the same context created for each request. As such, the dispose
96+
// callback action is left empty, as the context will be disposed by Autofac when the request ends.
97+
app.CreatePerOwinContext<ApplicationDbContext>(
98+
createCallback: (options, context) => container.Resolve<ApplicationDbContext>(),
99+
disposeCallback: static (options, context) => { });
100+
89101
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
90102
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
91103

samples/Mortis/Mortis.Server/Views/Authorization/Logout.cshtml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
@Html.AntiForgeryToken()
1010

1111
@* Flow the request parameters so they can be received by the LogoutPost action: *@
12-
foreach (var parameter in Model.Parameters)
12+
foreach (var parameter in string.Equals(Request.HttpMethod, "POST", StringComparison.OrdinalIgnoreCase) ?
13+
from name in Request.Form.AllKeys
14+
from value in Request.Form.GetValues(name)
15+
select new KeyValuePair<string, string>(name, value) :
16+
from name in Request.QueryString.AllKeys
17+
from value in Request.QueryString.GetValues(name)
18+
select new KeyValuePair<string, string>(name, value))
1319
{
1420
<input type="hidden" name="@parameter.Key" value="@parameter.Value" />
1521
}

0 commit comments

Comments
 (0)