コード例 #1
0
        public IActionResult SynthesizeUser(
            [FromHeader(Name = "Authorization")] string token)
        {
            var tokenUserId = User.Claims.GetUserId();

            if (!appSettings.AdminUserIds.Contains(tokenUserId))
            {
                return(Forbid());
            }

            string randomEmail;
            User   existing;

            do
            {
                randomEmail = $"{nameGenerator.FirstName()}.{nameGenerator.Surname()}@testuser.com";
                existing    = context.Users.SingleOrDefault(u => u.Email == randomEmail);
            } while (existing != null);

            queue.QueueBackgroundWorkItem(async cancelToken =>
            {
                // https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.2#call-services-from-main
                using (var serviceScope = serviceScopeFactory.CreateScope())
                {
                    var services = serviceScope.ServiceProvider;
                    var m        = services.GetRequiredService <IMediator>();

                    Log.Information($"Starting SynthesizeUser for");
                    await m.Send(new SynthesizeUser.Command(randomEmail)).ConfigureAwait(false);
                }

                Log.Information("SynthesizeUser has completed");
            });

            return(Accepted(new { Email = randomEmail, Password = "******" }));
        }