ok

alphons <alphons@heijden.com> 20 Jan 2025, 09:44
d0c28e91922aeeb4a25adbdbe42f82a19e285dab
3 files changed
  • SessionScopedTest/Controllers/ExampleController.cs
  • SessionScopedTest/Services/ExampleService.cs
  • SessionScopedTest/wwwroot/Index.html
diff --git a/SessionScopedTest/Controllers/ExampleController.cs b/SessionScopedTest/Controllers/ExampleController.cs
index f9fc2d3..ad50c31 100644
--- a/SessionScopedTest/Controllers/ExampleController.cs
+++ b/SessionScopedTest/Controllers/ExampleController.cs
@@ -9,8 +9,15 @@ public class ExampleController(SessionScopedFactory<ExampleService> factory) : C
{
HttpContext.Session.SetString("Started", DateTime.Now.ToString());
- var result = await factory.Instance.CalcAsync(123, 456);
+ var result = await factory.Instance.SetNameAsync($"alphons {Guid.NewGuid()}");
- return Ok(result);
+ return Ok("name is set, check url /Example/WhatsYourName");
}
+ public async Task<IActionResult> WhatsYourName()
+ {
+ var name = await factory.Instance.GetNameAsync();
+
+ return Ok(name);
+ }
+
}
diff --git a/SessionScopedTest/Services/ExampleService.cs b/SessionScopedTest/Services/ExampleService.cs
index 6842f55..1edf419 100644
--- a/SessionScopedTest/Services/ExampleService.cs
+++ b/SessionScopedTest/Services/ExampleService.cs
@@ -4,13 +4,25 @@ public class ExampleService(ILoggerFactory loggerFactory) : IHostedService
{
private readonly ILogger Logger = loggerFactory.CreateLogger<ExampleService>();
- public async Task<int> CalcAsync(int a, int b)
+ private string name = "unknown";
+ public async Task<bool> SetNameAsync(string Name)
{
- Logger.LogInformation("CalcAsync {a} x {b}", a , b);
+ this.name = Name;
+
+ Logger.LogInformation("SetNameAsync {Name}", Name);
+
+ await Task.Delay(100);
+
+ return true;
+ }
+
+ public async Task<string> GetNameAsync()
+ {
+ Logger.LogInformation("GetNameAsync {name}", name);
await Task.Delay(100);
- return a * b;
+ return name;
}
public async Task StartAsync(CancellationToken cancellationToken)
diff --git a/SessionScopedTest/wwwroot/Index.html b/SessionScopedTest/wwwroot/Index.html
index 6f9d7a2..0046ad5 100644
--- a/SessionScopedTest/wwwroot/Index.html
+++ b/SessionScopedTest/wwwroot/Index.html
@@ -2,9 +2,11 @@
<html>
<head>
<meta charset="utf-8" />
- <title></title>
+ <title>Example of SessionScoped</title>
</head>
<body>
- <a href="./Example">calculate 123 x 456</a>
+ <a href="./Example">Set a random name</a>
+
+ <p>Try multiple browser types at the same time</p>
</body>
</html>
\ No newline at end of file