Skip to content

Commit 03c5d90

Browse files
committed
add service using AsyncLocal for instance context across request
1 parent 57e5495 commit 03c5d90

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

src/Altinn.App.Api/Controllers/ResourceController.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ namespace Altinn.App.Api.Controllers;
1111
public class ResourceController : ControllerBase
1212
{
1313
private readonly IAppResources _appResourceService;
14+
private readonly IInstanceContext _instanceContext;
1415

1516
/// <summary>
1617
/// Initializes a new instance of the <see cref="ResourceController"/> class
1718
/// </summary>
1819
/// <param name="appResourcesService">The execution service</param>
19-
public ResourceController(IAppResources appResourcesService)
20+
public ResourceController(IAppResources appResourcesService, IInstanceContext instanceContext)
2021
{
2122
_appResourceService = appResourcesService;
23+
_instanceContext = instanceContext;
2224
}
2325

2426
/// <summary>
@@ -71,6 +73,32 @@ public ActionResult GetLayouts(string org, string app, string id)
7173
return Ok(layouts);
7274
}
7375

76+
/// <summary>
77+
/// Endpoint for layouts with instance context
78+
/// </summary>
79+
/// <param name="org"></param>
80+
/// <param name="app"></param>
81+
/// <param name="instanceOwnerPartyId"></param>
82+
/// <param name="instanceId"></param>
83+
/// <param name="layoutSetId"></param>
84+
/// <returns></returns>
85+
[ProducesResponseType(typeof(string), StatusCodes.Status200OK, "text/plain")]
86+
[HttpGet]
87+
[Route("{org}/{app}/instance/{instanceOwnerPartyId:int}/{instanceId}/layouts/{layoutSetId}")]
88+
public ActionResult GetInstanceLayouts(
89+
string org,
90+
string app,
91+
int instanceOwnerPartyId,
92+
string instanceId,
93+
string layoutSetId
94+
)
95+
{
96+
_instanceContext.InstanceId = instanceId;
97+
_instanceContext.InstanceOwnerPartyId = instanceOwnerPartyId;
98+
string layouts = _appResourceService.GetLayoutsForSet(layoutSetId);
99+
return Ok(layouts);
100+
}
101+
74102
/// <summary>
75103
/// Get the layout settings.
76104
/// </summary>

src/Altinn.App.Core/Extensions/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ IWebHostEnvironment env
168168
services.TryAddTransient<IPDP, PDPAppSI>();
169169
services.TryAddTransient<IPrefill, PrefillSI>();
170170
services.TryAddTransient<ISigningCredentialsResolver, SigningCredentialsResolver>();
171+
services.TryAddSingleton<IInstanceContext, InstanceContext>();
171172
services.TryAddSingleton<IAppResources, AppResourcesSI>();
172173
services.TryAddSingleton<IAppMetadata, AppMetadata>();
173174
services.TryAddSingleton<IFrontendFeatures, FrontendFeatures>();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Altinn.App.Core.Internal.App;
2+
3+
public class InstanceContext : IInstanceContext
4+
{
5+
private static readonly AsyncLocal<int?> _instanceOwnerPartyId = new();
6+
private static readonly AsyncLocal<string?> _instanceId = new();
7+
8+
public string? InstanceId
9+
{
10+
get => _instanceId.Value;
11+
set => _instanceId.Value = value;
12+
}
13+
14+
public int? InstanceOwnerPartyId
15+
{
16+
get => _instanceOwnerPartyId.Value;
17+
set => _instanceOwnerPartyId.Value = value;
18+
}
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Altinn.App.Core.Internal.App;
2+
3+
/// <summary>
4+
/// Interface for accessing instance context information
5+
/// </summary>
6+
public interface IInstanceContext
7+
{
8+
/// <summary>
9+
/// Instance Id
10+
/// </summary>
11+
string? InstanceId { get; set; }
12+
13+
/// <summary>
14+
/// Party Id
15+
/// </summary>
16+
int? InstanceOwnerPartyId { get; set; }
17+
}

0 commit comments

Comments
 (0)