Skip to content

Commit 5018afb

Browse files
authored
Merge pull request #988 from DuendeSoftware/mb/bff
Improve upgrade docs from V3 -> V4
2 parents 8d7d53f + c3e2c97 commit 5018afb

1 file changed

Lines changed: 86 additions & 21 deletions

File tree

src/content/docs/bff/upgrading/bff-v3-to-v4.md

Lines changed: 86 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ The syntax for configuring remote APIs has changed slightly:
2929
- .RequireAccessToken(TokenType.Client);
3030

3131
+ app.MapRemoteBffApiEndpoint("/api/client-token", new Uri("https://localhost:5010"))
32-
+ .WithAccessToken(RequiredTokenType.Client);
32+
+ .WithAccessToken(RequiredTokenType.Client);
3333

3434
// Use the client token only if the user is logged in
3535
- app.MapRemoteBffApiEndpoint("/api/optional-user-token", "https://localhost:5010")
3636
- .WithOptionalUserAccessToken();
3737

3838
+ app.MapRemoteBffApiEndpoint("/api/optional-user-token", new Uri("https://localhost:5010"))
39-
+ .WithAccessToken(RequiredTokenType.UserOrNone);
39+
+ .WithAccessToken(RequiredTokenType.UserOrNone);
4040
```
4141

4242
* The enum `TokenType` has been renamed to `RequiredTokenType`, and moved from the `Duende.Bff` to `Duende.Bff.AccessTokenManagement` namespace.
@@ -49,6 +49,74 @@ The required token type configuration in YARP has also changed slightly. It uses
4949

5050
### Extending The BFF
5151

52+
#### Service To Endpoint Updates
53+
54+
Service interfaces and their default implementations have been renamed and have changed, resulting in an updated extensibility model:
55+
56+
* Generally, the interfaces have been renamed, e.g. from `IUserService` to `IUserEndpoint`.
57+
* Default implementation is now internal, but can be used when overriding the endpoint:
58+
59+
```diff lang="csharp"
60+
- public class MyUserService : DefaultUserService
61+
- {
62+
- public override Task ProcessRequestAsync(HttpContext context, CancellationToken ct)
63+
- {
64+
- // Custom logic here
65+
-
66+
- return base.ProcessRequestAsync(context);
67+
- }
68+
- }
69+
70+
+ var bffOptions = app.Services.GetRequiredService<IOptions<BffOptions>>().Value;
71+
+
72+
+ app.MapGet(bffOptions.UserPath, async (HttpContext context, CancellationToken ct) =>
73+
+ {
74+
+ // ... custom logic before calling the endpoint implementation ...
75+
+
76+
+ var endpointProcessor = context.RequestServices.GetRequiredService<IUserEndpoint>();
77+
+ await endpointProcessor.ProcessRequestAsync(context, ct);
78+
+
79+
+ // ... custom logic after calling the endpoint implementation ...
80+
+ });
81+
```
82+
83+
For more information, see the [endpoints documentation](/bff/extensibility/management/index.mdx).
84+
85+
#### Custom Session Store
86+
87+
If you have a custom implementation of `IUserSessionStore`, the interface has changed to support multiple frontends.
88+
89+
In all methods, the `string key` has been replaced with a strongly typed `UserSessionKey` struct, which contains the `PartitionKey` and `SessionId`:
90+
91+
* `PartitionKey` - Corresponds to the frontend name (or `ApplicationName` in V3).
92+
* `SessionId`: The user's session identifier.
93+
94+
```diff lang="csharp"
95+
public class MySessionStore : IUserSessionStore
96+
{
97+
- public Task<UserSession> GetUserSessionAsync(string key, CancellationToken cancellationToken)
98+
+ public Task<UserSession> GetUserSessionAsync(UserSessionKey key, CancellationToken cancellationToken)
99+
{
100+
// ...
101+
}
102+
103+
// ...
104+
}
105+
```
106+
107+
Also see [related database changes and migrations](#server-side-sessions-database-migrations).
108+
109+
#### Access Token Retrieval
110+
111+
The `HttpContext.GetUserAccessTokenAsync` extension method has been removed from the `Duende.Bff` namespace. You should now use the extension method from the `Duende.AccessTokenManagement.OpenIdConnect` namespace.
112+
113+
```csharp
114+
using Duende.AccessTokenManagement.OpenIdConnect;
115+
116+
// ...
117+
var token = await HttpContext.GetUserAccessTokenAsync();
118+
```
119+
52120
#### Simplified Wireup Without Explicit Authentication Setup
53121

54122
The V3 style of wireup still works, but BFF V4 comes with a newer style of wireup:
@@ -60,11 +128,12 @@ services.AddBff()
60128
options.Authority = "your authority";
61129
options.ClientId = "your client id";
62130
options.ClientSecret = "secret";
63-
// ... other OpenID Connect options.
131+
132+
// ... other OpenID Connect options.
64133
}
65134
.ConfigureCookies(options => {
66135
// The cookie options are automatically configured with recommended practices.
67-
// However, you can change the config here.
136+
// However, you can change the config here.
68137
});
69138
```
70139

@@ -116,68 +185,64 @@ See the type `BffConfiguration` to see what settings can be configured.
116185

117186
## Handling SPA Static Assets
118187

119-
The BFF can be configured to handle the static file assets that are typically used when developing SPA based apps.
188+
The BFF can be configured to handle the static file assets that are typically used when developing SPA based apps.
120189

121190
### Proxying Only `index.html`
122191

123-
When deploying a multi-frontend BFF, it makes most sense to have the frontends configured with an `index.html` file that is retrieved from a Content Delivery Network (CDN).
192+
When deploying a multi-frontend BFF, it makes most sense to have the frontends configured with an `index.html` file that is retrieved from a Content Delivery Network (CDN).
124193

125-
This can be done in various ways. For example, if you use Vite, you can publish static assets with a base URL configured. This will make sure that any static asset, (such as images, scripts, etc.) are retrieved directly from the CDN for best performance.
194+
This can be done in various ways. For example, if you use Vite, you can publish static assets with a base URL configured. This will make sure that any static asset, (such as images, scripts, etc.) are retrieved directly from the CDN for best performance.
126195

127196
```csharp
128197
var frontend = new BffFrontend(BffFrontendName.Parse("frontend1"))
129198
.WithCdnIndexHtml(new Uri("https://my_cdn/some_app/index.html"))
130199
```
131200

132-
The BFF automatically wires up a catch-all route that serves`index.html` for that specific frontend.
201+
The BFF automatically wires up a catch-all route that serves`index.html` for that specific frontend.
133202

134-
See [Serve the index page from the BFF host](/bff/architecture/ui-hosting.md#serve-the-index-page-from-the-bff-host) for more information.
203+
See [Serve the index page from the BFF host](/bff/architecture/ui-hosting.md#serve-the-index-page-from-the-bff-host) for more information.
135204

136-
### Proxying All Static Assets
205+
### Proxying All Static Assets
137206

138207
When developing a Single-Page Application (SPA), it's very common to use a development webserver such as Vite. While Vite can publish static assets with a base URL, this doesn't work well during development.
139208

140209
The best development experience can be achieved by configuring the BFF to proxy all static assets from the development server:
141210

142-
143211
```csharp
144212
var frontend = new BffFrontend(BffFrontendName.Parse("frontend1"))
145213
.WithProxiedStaticAssets(new Uri("https://localhost:3000")); // https://localhost:3000 would be the URL of your development web server.
146214
```
147215

148-
While this can also be done in production, it will proxy all static assets through the BFF. This will increase the bandwidth consumed by the BFF and reduce the overall performance of your application.
216+
While this can also be done in production, it will proxy all static assets through the BFF. This will increase the bandwidth consumed by the BFF and reduce the overall performance of your application.
149217

150218
### Proxying Assets Based On Environment
151219

152220
If you're using a local development server during development and a CDN in production, you can configure this as follows:
153221

154-
``` csharp
155-
222+
```csharp
156223
// In this example, the environment name from the application builder is used to determine
157-
// if we're running in production or not.
224+
// if we're running in production or not.
158225
var runningInProduction = () => builder.Environment.EnvironmentName == Environments.Production;
159226

160-
// Then, when configuring the frontend, you can switch when the static assets will be proxied.
227+
// Then, when configuring the frontend, you can switch when the static assets will be proxied.
161228
new BffFrontend(BffFrontendName.Parse("default-frontend"))
162229
.WithBffStaticAssets(new Uri("https://localhost:5010/static"), useCdnWhen: runningInProduction);
163-
164230
```
165231

166232
:::note
167-
This function is evaluated immediately when calling the`.WithBffStaticAssets()` extension method. When you call this method during startup, the condition is only evaluated at startup time. It's not evaluated at runtime for every request.
233+
This function is evaluated immediately when calling the`.WithBffStaticAssets()` extension method. When you call this method during startup, the condition is only evaluated at startup time. It's not evaluated at runtime for every request.
168234
:::
169235

170-
171-
172236
### Server Side Sessions Database Migrations
173237

174-
When using the server side sessions feature backed by the `Duende.BFF.EntityFramework` package, you will need to script [Entity Framework database migrations](/bff/fundamentals/session/server-side-sessions.mdx#entity-framework-migrations) and apply these changes to your database.
238+
When using the server side sessions feature backed by the `Duende.BFF.EntityFramework` package, you will need to script [Entity Framework database migrations](/bff/fundamentals/session/server-side-sessions.mdx#entity-framework-migrations) and apply these changes to your database.
175239

176240
```shell
177241
dotnet ef migrations add BFFUserSessionsV4 -o Migrations -c SessionDbContext
178242
```
179243

180244
In the `UserSessions` table, a number of changes were introduced:
245+
181246
* The `ApplicationName` column was renamed to `PartitionKey`. This column will contain the BFF frontend name.
182247
* Related indexes were updated.
183248

0 commit comments

Comments
 (0)