Skip to content

Commit 7e8a35c

Browse files
Expand server-side sessions documentation to include DbContext pooling guidance, clarify migration steps, and improve formatting for consistency.
1 parent 7faca40 commit 7e8a35c

1 file changed

Lines changed: 36 additions & 7 deletions

File tree

src/content/docs/bff/fundamentals/session/server-side-sessions.mdx

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,39 @@ builder.Services.AddBff()
4545
options.UseSqlServer(cn);
4646
});
4747
```
48+
The method of `AddEntityFrameworkServerSideSessions` registers the `SessionDbContext` along with a `UserSessionStore` as transient dependencies.
4849

49-
### Entity Framework Migrations
50-
Most datastores that you might use with Entity Framework use a schema to define the structure of their data. *Duende.BFF.EntityFramework* doesn't make any assumptions about the underlying datastore, how (or indeed even if) it defines its schema, or how schema changes are managed by your organization. For these reasons, Duende does not directly support database creation, schema changes, or data migration by publishing database scripts. You are expected to manage your database in the way your organization sees fit. Using EF migrations is one possible approach to that, which Duende facilitates by publishing entity classes in each version of *Duende.BFF.EntityFramework*. An example project that uses those entities to create migrations is [here](https://github.com/DuendeSoftware/products/tree/main/bff/migrations/UserSessionDb).
50+
For developers looking to take advantage of DbContext pooling or have more fine-grained control over their DbContext creation registration and creation process, you can use the `AddEntityFrameworkServerSideSessionsServices` method instead. This method registers all the required services for server-side session except for the `SessionDbContext`, which will now be managed by the DbContext pooling mechanism.
51+
52+
```csharp
53+
var cn = _configuration.GetConnectionString("db");
54+
55+
builder.Services.AddDbContextPool<SessionDbContext>(opt =>
56+
{
57+
// configure your db context pool options here
58+
59+
options.UseSqlServer(cn);
60+
});
61+
62+
builder.Services.AddBff()
63+
.AddEntityFrameworkServerSideSessionsServices<SessionDbContext>()
64+
```
65+
66+
Note, you'll still need to let the server side session store know about the `SessionDbContext` by calling `AddEntityFrameworkServerSideSessions` with the `SessionDbContext` implementation as a generic argument.
67+
68+
### Entity Framework Migrations
69+
70+
Most data stores that you might use with Entity Framework use a schema to define the structure of their data. *Duende.BFF.EntityFramework* doesn't make any assumptions about the underlying datastore, how (or indeed even if) it defines its schema, or how schema changes are managed by your organization. For these reasons, Duende does not directly support database creation, schema changes, or data migration by publishing database scripts.
71+
72+
You are expected to manage your database in the way your organization sees fit. Using EF migrations is one possible approach to that, which Duende facilitates by publishing entity classes in each version of *Duende.BFF.EntityFramework*. An example project that uses those entities to create migrations is [here](https://github.com/DuendeSoftware/products/tree/main/bff/migrations/UserSessionDb).
73+
74+
To quickly create Entity Framework migrations, run the following command in the project directory that has access to Entity Framework Core's tools:
75+
76+
```bash
77+
dotnet ef migrations add UserSessions -o Migrations
78+
```
79+
80+
The project must also reference the `Duende.BFF.EntityFramework` NuGet package and the `Microsoft.EntityFrameworkCore.Design` NuGet package, along with a specific database provider and its corresponding configuration, including the connection string.
5181

5282
## Session Store Cleanup
5383
Added in v1.2.0.
@@ -85,16 +115,16 @@ builder.Services.AddBff()
85115
options.UseSqlServer(cn);
86116
})
87117
.AddSessionCleanupBackgroundProcess();`}/>
88-
118+
89119
:::note
90120
In V4, we changed how you enable session cleanup. We no longer automatically register the session cleanup hosted service. This has to be done manually.
91121
In a load-balanced environment, you can choose to run the cleanup job on all instances the BFF. However, you can also decide to
92-
spin up a separate host that's responsible for background jobs such as this cleanup job.
122+
spin up a separate host that's responsible for background jobs such as this cleanup job.
93123
:::
94124

95125
</TabItem>
96126
<TabItem label="V3">
97-
127+
98128
If you wish to have such sessions cleaned up periodically, then you can configure the *EnableSessionCleanup* and *SessionCleanupInterval* options:
99129

100130
<Code
@@ -126,5 +156,4 @@ builder.Services.AddBff(options =>
126156
});`}/>
127157

128158
</TabItem>
129-
</Tabs>
130-
159+
</Tabs>

0 commit comments

Comments
 (0)