From 93b9a58029a6480c3c1358f31ca83c5c2a3580f9 Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 22 Jan 2020 14:17:41 +0800 Subject: [PATCH 1/8] Move generic host usage for console apps Resolve #1891 --- .../AbpConsoleDemo/AbpConsoleDemo.csproj | 1 + .../AbpConsoleDemo/AppHostedService.cs | 32 +++++++++++ .../AbpConsoleDemo/Program.cs | 29 ++++------ .../DbMigratorHostedService.cs | 34 ++++++++++++ .../src/Acme.BookStore.DbMigrator/Program.cs | 41 +++++--------- .../ConsoleTestAppHostedService.cs | 26 +++++++++ .../Program.cs | 25 ++++----- .../DbMigratorHostedService.cs | 34 ++++++++++++ .../src/Acme.BookStore.DbMigrator/Program.cs | 41 +++++--------- .../ConsoleTestAppHostedService.cs | 26 +++++++++ .../Program.cs | 25 ++++----- .../ConsoleTestAppHostedService.cs | 26 +++++++++ .../Program.cs | 25 ++++----- .../DbMigratorHostedService.cs | 34 ++++++++++++ .../src/Acme.BookStore.DbMigrator/Program.cs | 41 +++++--------- .../ConsoleTestAppHostedService.cs | 26 +++++++++ .../Program.cs | 25 ++++----- .../DbMigratorHostedService.cs | 34 ++++++++++++ .../src/DashboardDemo.DbMigrator/Program.cs | 41 +++++--------- .../ConsoleTestAppHostedService.cs | 26 +++++++++ .../Program.cs | 25 ++++----- .../ConsoleClientDemoHostedService.cs | 33 +++++++++++ .../applications/ConsoleClientDemo/Program.cs | 55 ++++++------------- .../DbMigratorHostedService.cs | 34 ++++++++++++ .../Program.cs | 41 +++++--------- .../ConsoleTestAppHostedService.cs | 26 +++++++++ .../Program.cs | 25 ++++----- .../ConsoleTestAppHostedService.cs | 26 +++++++++ .../Program.cs | 27 ++++----- 29 files changed, 588 insertions(+), 296 deletions(-) create mode 100644 samples/BasicConsoleApplication/AbpConsoleDemo/AppHostedService.cs create mode 100644 samples/BookStore-Angular-MongoDb/aspnet-core/src/Acme.BookStore.DbMigrator/DbMigratorHostedService.cs create mode 100644 samples/BookStore-Angular-MongoDb/aspnet-core/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs create mode 100644 samples/BookStore-Modular/application/src/Acme.BookStore.DbMigrator/DbMigratorHostedService.cs create mode 100644 samples/BookStore-Modular/application/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs create mode 100644 samples/BookStore-Modular/modules/book-management/test/Acme.BookStore.BookManagement.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs create mode 100644 samples/BookStore/src/Acme.BookStore.DbMigrator/DbMigratorHostedService.cs create mode 100644 samples/BookStore/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs create mode 100644 samples/DashboardDemo/src/DashboardDemo.DbMigrator/DbMigratorHostedService.cs create mode 100644 samples/DashboardDemo/test/DashboardDemo.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs create mode 100644 samples/MicroserviceDemo/applications/ConsoleClientDemo/ConsoleClientDemoHostedService.cs create mode 100644 templates/app/aspnet-core/src/MyCompanyName.MyProjectName.DbMigrator/DbMigratorHostedService.cs create mode 100644 templates/app/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs create mode 100644 templates/module/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs diff --git a/samples/BasicConsoleApplication/AbpConsoleDemo/AbpConsoleDemo.csproj b/samples/BasicConsoleApplication/AbpConsoleDemo/AbpConsoleDemo.csproj index c3c4e39fd8..6bdf644289 100644 --- a/samples/BasicConsoleApplication/AbpConsoleDemo/AbpConsoleDemo.csproj +++ b/samples/BasicConsoleApplication/AbpConsoleDemo/AbpConsoleDemo.csproj @@ -7,6 +7,7 @@ + diff --git a/samples/BasicConsoleApplication/AbpConsoleDemo/AppHostedService.cs b/samples/BasicConsoleApplication/AbpConsoleDemo/AppHostedService.cs new file mode 100644 index 0000000000..578c437177 --- /dev/null +++ b/samples/BasicConsoleApplication/AbpConsoleDemo/AppHostedService.cs @@ -0,0 +1,32 @@ +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Volo.Abp; + +namespace AbpConsoleDemo +{ + public class AppHostedService : IHostedService + { + public Task StartAsync(CancellationToken cancellationToken) + { + using (var application = AbpApplicationFactory.Create(options => + { + options.UseAutofac(); //Autofac integration + })) + { + application.Initialize(); + + //Resolve a service and use it + var helloWorldService = application.ServiceProvider.GetService(); + helloWorldService.SayHello(); + + application.Shutdown(); + } + + return Task.CompletedTask; + } + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + } +} diff --git a/samples/BasicConsoleApplication/AbpConsoleDemo/Program.cs b/samples/BasicConsoleApplication/AbpConsoleDemo/Program.cs index 651fda3c4f..2c8791f656 100644 --- a/samples/BasicConsoleApplication/AbpConsoleDemo/Program.cs +++ b/samples/BasicConsoleApplication/AbpConsoleDemo/Program.cs @@ -1,28 +1,21 @@ -using System; +using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; -using Volo.Abp; +using Microsoft.Extensions.Hosting; namespace AbpConsoleDemo { class Program { - static void Main(string[] args) + static async Task Main(string[] args) { - using (var application = AbpApplicationFactory.Create(options => - { - options.UseAutofac(); //Autofac integration - })) - { - application.Initialize(); - - //Resolve a service and use it - var helloWorldService = - application.ServiceProvider.GetService(); - helloWorldService.SayHello(); - - Console.WriteLine("Press ENTER to stop application..."); - Console.ReadLine(); - } + await CreateHostBuilder(args).RunConsoleAsync(); } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureServices((hostContext, services) => + { + services.AddHostedService(); + }); } } diff --git a/samples/BookStore-Angular-MongoDb/aspnet-core/src/Acme.BookStore.DbMigrator/DbMigratorHostedService.cs b/samples/BookStore-Angular-MongoDb/aspnet-core/src/Acme.BookStore.DbMigrator/DbMigratorHostedService.cs new file mode 100644 index 0000000000..aef3d88bb5 --- /dev/null +++ b/samples/BookStore-Angular-MongoDb/aspnet-core/src/Acme.BookStore.DbMigrator/DbMigratorHostedService.cs @@ -0,0 +1,34 @@ +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Acme.BookStore.Data; +using Serilog; +using Volo.Abp; + +namespace Acme.BookStore.DbMigrator +{ + public class DbMigratorHostedService : IHostedService + { + public async Task StartAsync(CancellationToken cancellationToken) + { + using (var application = AbpApplicationFactory.Create(options => + { + options.UseAutofac(); + options.Services.AddLogging(c => c.AddSerilog()); + })) + { + application.Initialize(); + + await application + .ServiceProvider + .GetRequiredService() + .MigrateAsync(); + + application.Shutdown(); + } + } + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + } +} diff --git a/samples/BookStore-Angular-MongoDb/aspnet-core/src/Acme.BookStore.DbMigrator/Program.cs b/samples/BookStore-Angular-MongoDb/aspnet-core/src/Acme.BookStore.DbMigrator/Program.cs index d3c4034c8a..179c18f4ca 100644 --- a/samples/BookStore-Angular-MongoDb/aspnet-core/src/Acme.BookStore.DbMigrator/Program.cs +++ b/samples/BookStore-Angular-MongoDb/aspnet-core/src/Acme.BookStore.DbMigrator/Program.cs @@ -1,39 +1,15 @@ -using System.IO; +using System.IO; +using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; -using Acme.BookStore.Data; +using Microsoft.Extensions.Hosting; using Serilog; using Serilog.Events; -using Volo.Abp; -using Volo.Abp.Threading; namespace Acme.BookStore.DbMigrator { class Program { - static void Main(string[] args) - { - ConfigureLogging(); - - using (var application = AbpApplicationFactory.Create(options => - { - options.UseAutofac(); - options.Services.AddLogging(c => c.AddSerilog()); - })) - { - application.Initialize(); - - AsyncHelper.RunSync( - () => application - .ServiceProvider - .GetRequiredService() - .MigrateAsync() - ); - - application.Shutdown(); - } - } - - private static void ConfigureLogging() + static async Task Main(string[] args) { Log.Logger = new LoggerConfiguration() .MinimumLevel.Information() @@ -48,6 +24,15 @@ namespace Acme.BookStore.DbMigrator .WriteTo.File(Path.Combine(Directory.GetCurrentDirectory(), "Logs/logs.txt")) .WriteTo.Console() .CreateLogger(); + + await CreateHostBuilder(args).RunConsoleAsync(); } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureServices((hostContext, services) => + { + services.AddHostedService(); + }); } } diff --git a/samples/BookStore-Angular-MongoDb/aspnet-core/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs b/samples/BookStore-Angular-MongoDb/aspnet-core/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs new file mode 100644 index 0000000000..e41ee7ed31 --- /dev/null +++ b/samples/BookStore-Angular-MongoDb/aspnet-core/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs @@ -0,0 +1,26 @@ +using Microsoft.Extensions.Hosting; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp; + +namespace Acme.BookStore.HttpApi.Client.ConsoleTestApp +{ + public class ConsoleTestAppHostedService : IHostedService + { + public async Task StartAsync(CancellationToken cancellationToken) + { + using (var application = AbpApplicationFactory.Create()) + { + application.Initialize(); + + var demo = application.ServiceProvider.GetRequiredService(); + await demo.RunAsync(); + + application.Shutdown(); + } + } + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + } +} diff --git a/samples/BookStore-Angular-MongoDb/aspnet-core/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/Program.cs b/samples/BookStore-Angular-MongoDb/aspnet-core/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/Program.cs index ecef29d963..7d8265158b 100644 --- a/samples/BookStore-Angular-MongoDb/aspnet-core/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/Program.cs +++ b/samples/BookStore-Angular-MongoDb/aspnet-core/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/Program.cs @@ -1,24 +1,21 @@ -using System; +using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; -using Volo.Abp; -using Volo.Abp.Threading; +using Microsoft.Extensions.Hosting; namespace Acme.BookStore.HttpApi.Client.ConsoleTestApp { class Program { - static void Main(string[] args) + static async Task Main(string[] args) { - using (var application = AbpApplicationFactory.Create()) - { - application.Initialize(); - - var demo = application.ServiceProvider.GetRequiredService(); - AsyncHelper.RunSync(() => demo.RunAsync()); - - Console.WriteLine("Press ENTER to stop application..."); - Console.ReadLine(); - } + await CreateHostBuilder(args).RunConsoleAsync(); } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureServices((hostContext, services) => + { + services.AddHostedService(); + }); } } diff --git a/samples/BookStore-Modular/application/src/Acme.BookStore.DbMigrator/DbMigratorHostedService.cs b/samples/BookStore-Modular/application/src/Acme.BookStore.DbMigrator/DbMigratorHostedService.cs new file mode 100644 index 0000000000..aef3d88bb5 --- /dev/null +++ b/samples/BookStore-Modular/application/src/Acme.BookStore.DbMigrator/DbMigratorHostedService.cs @@ -0,0 +1,34 @@ +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Acme.BookStore.Data; +using Serilog; +using Volo.Abp; + +namespace Acme.BookStore.DbMigrator +{ + public class DbMigratorHostedService : IHostedService + { + public async Task StartAsync(CancellationToken cancellationToken) + { + using (var application = AbpApplicationFactory.Create(options => + { + options.UseAutofac(); + options.Services.AddLogging(c => c.AddSerilog()); + })) + { + application.Initialize(); + + await application + .ServiceProvider + .GetRequiredService() + .MigrateAsync(); + + application.Shutdown(); + } + } + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + } +} diff --git a/samples/BookStore-Modular/application/src/Acme.BookStore.DbMigrator/Program.cs b/samples/BookStore-Modular/application/src/Acme.BookStore.DbMigrator/Program.cs index d3c4034c8a..179c18f4ca 100644 --- a/samples/BookStore-Modular/application/src/Acme.BookStore.DbMigrator/Program.cs +++ b/samples/BookStore-Modular/application/src/Acme.BookStore.DbMigrator/Program.cs @@ -1,39 +1,15 @@ -using System.IO; +using System.IO; +using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; -using Acme.BookStore.Data; +using Microsoft.Extensions.Hosting; using Serilog; using Serilog.Events; -using Volo.Abp; -using Volo.Abp.Threading; namespace Acme.BookStore.DbMigrator { class Program { - static void Main(string[] args) - { - ConfigureLogging(); - - using (var application = AbpApplicationFactory.Create(options => - { - options.UseAutofac(); - options.Services.AddLogging(c => c.AddSerilog()); - })) - { - application.Initialize(); - - AsyncHelper.RunSync( - () => application - .ServiceProvider - .GetRequiredService() - .MigrateAsync() - ); - - application.Shutdown(); - } - } - - private static void ConfigureLogging() + static async Task Main(string[] args) { Log.Logger = new LoggerConfiguration() .MinimumLevel.Information() @@ -48,6 +24,15 @@ namespace Acme.BookStore.DbMigrator .WriteTo.File(Path.Combine(Directory.GetCurrentDirectory(), "Logs/logs.txt")) .WriteTo.Console() .CreateLogger(); + + await CreateHostBuilder(args).RunConsoleAsync(); } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureServices((hostContext, services) => + { + services.AddHostedService(); + }); } } diff --git a/samples/BookStore-Modular/application/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs b/samples/BookStore-Modular/application/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs new file mode 100644 index 0000000000..e41ee7ed31 --- /dev/null +++ b/samples/BookStore-Modular/application/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs @@ -0,0 +1,26 @@ +using Microsoft.Extensions.Hosting; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp; + +namespace Acme.BookStore.HttpApi.Client.ConsoleTestApp +{ + public class ConsoleTestAppHostedService : IHostedService + { + public async Task StartAsync(CancellationToken cancellationToken) + { + using (var application = AbpApplicationFactory.Create()) + { + application.Initialize(); + + var demo = application.ServiceProvider.GetRequiredService(); + await demo.RunAsync(); + + application.Shutdown(); + } + } + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + } +} diff --git a/samples/BookStore-Modular/application/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/Program.cs b/samples/BookStore-Modular/application/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/Program.cs index ecef29d963..7d8265158b 100644 --- a/samples/BookStore-Modular/application/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/Program.cs +++ b/samples/BookStore-Modular/application/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/Program.cs @@ -1,24 +1,21 @@ -using System; +using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; -using Volo.Abp; -using Volo.Abp.Threading; +using Microsoft.Extensions.Hosting; namespace Acme.BookStore.HttpApi.Client.ConsoleTestApp { class Program { - static void Main(string[] args) + static async Task Main(string[] args) { - using (var application = AbpApplicationFactory.Create()) - { - application.Initialize(); - - var demo = application.ServiceProvider.GetRequiredService(); - AsyncHelper.RunSync(() => demo.RunAsync()); - - Console.WriteLine("Press ENTER to stop application..."); - Console.ReadLine(); - } + await CreateHostBuilder(args).RunConsoleAsync(); } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureServices((hostContext, services) => + { + services.AddHostedService(); + }); } } diff --git a/samples/BookStore-Modular/modules/book-management/test/Acme.BookStore.BookManagement.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs b/samples/BookStore-Modular/modules/book-management/test/Acme.BookStore.BookManagement.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs new file mode 100644 index 0000000000..aec5d0252d --- /dev/null +++ b/samples/BookStore-Modular/modules/book-management/test/Acme.BookStore.BookManagement.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs @@ -0,0 +1,26 @@ +using Microsoft.Extensions.Hosting; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp; + +namespace Acme.BookStore.BookManagement +{ + public class ConsoleTestAppHostedService : IHostedService + { + public async Task StartAsync(CancellationToken cancellationToken) + { + using (var application = AbpApplicationFactory.Create()) + { + application.Initialize(); + + var demo = application.ServiceProvider.GetRequiredService(); + await demo.RunAsync(); + + application.Shutdown(); + } + } + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + } +} diff --git a/samples/BookStore-Modular/modules/book-management/test/Acme.BookStore.BookManagement.HttpApi.Client.ConsoleTestApp/Program.cs b/samples/BookStore-Modular/modules/book-management/test/Acme.BookStore.BookManagement.HttpApi.Client.ConsoleTestApp/Program.cs index 9b6e256930..f69d97f590 100644 --- a/samples/BookStore-Modular/modules/book-management/test/Acme.BookStore.BookManagement.HttpApi.Client.ConsoleTestApp/Program.cs +++ b/samples/BookStore-Modular/modules/book-management/test/Acme.BookStore.BookManagement.HttpApi.Client.ConsoleTestApp/Program.cs @@ -1,24 +1,21 @@ -using System; +using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; -using Volo.Abp; -using Volo.Abp.Threading; +using Microsoft.Extensions.Hosting; namespace Acme.BookStore.BookManagement { class Program { - static void Main(string[] args) + static async Task Main(string[] args) { - using (var application = AbpApplicationFactory.Create()) - { - application.Initialize(); - - var demo = application.ServiceProvider.GetRequiredService(); - AsyncHelper.RunSync(() => demo.RunAsync()); - - Console.WriteLine("Press ENTER to stop application..."); - Console.ReadLine(); - } + await CreateHostBuilder(args).RunConsoleAsync(); } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureServices((hostContext, services) => + { + services.AddHostedService(); + }); } } diff --git a/samples/BookStore/src/Acme.BookStore.DbMigrator/DbMigratorHostedService.cs b/samples/BookStore/src/Acme.BookStore.DbMigrator/DbMigratorHostedService.cs new file mode 100644 index 0000000000..aef3d88bb5 --- /dev/null +++ b/samples/BookStore/src/Acme.BookStore.DbMigrator/DbMigratorHostedService.cs @@ -0,0 +1,34 @@ +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Acme.BookStore.Data; +using Serilog; +using Volo.Abp; + +namespace Acme.BookStore.DbMigrator +{ + public class DbMigratorHostedService : IHostedService + { + public async Task StartAsync(CancellationToken cancellationToken) + { + using (var application = AbpApplicationFactory.Create(options => + { + options.UseAutofac(); + options.Services.AddLogging(c => c.AddSerilog()); + })) + { + application.Initialize(); + + await application + .ServiceProvider + .GetRequiredService() + .MigrateAsync(); + + application.Shutdown(); + } + } + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + } +} diff --git a/samples/BookStore/src/Acme.BookStore.DbMigrator/Program.cs b/samples/BookStore/src/Acme.BookStore.DbMigrator/Program.cs index d3c4034c8a..179c18f4ca 100644 --- a/samples/BookStore/src/Acme.BookStore.DbMigrator/Program.cs +++ b/samples/BookStore/src/Acme.BookStore.DbMigrator/Program.cs @@ -1,39 +1,15 @@ -using System.IO; +using System.IO; +using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; -using Acme.BookStore.Data; +using Microsoft.Extensions.Hosting; using Serilog; using Serilog.Events; -using Volo.Abp; -using Volo.Abp.Threading; namespace Acme.BookStore.DbMigrator { class Program { - static void Main(string[] args) - { - ConfigureLogging(); - - using (var application = AbpApplicationFactory.Create(options => - { - options.UseAutofac(); - options.Services.AddLogging(c => c.AddSerilog()); - })) - { - application.Initialize(); - - AsyncHelper.RunSync( - () => application - .ServiceProvider - .GetRequiredService() - .MigrateAsync() - ); - - application.Shutdown(); - } - } - - private static void ConfigureLogging() + static async Task Main(string[] args) { Log.Logger = new LoggerConfiguration() .MinimumLevel.Information() @@ -48,6 +24,15 @@ namespace Acme.BookStore.DbMigrator .WriteTo.File(Path.Combine(Directory.GetCurrentDirectory(), "Logs/logs.txt")) .WriteTo.Console() .CreateLogger(); + + await CreateHostBuilder(args).RunConsoleAsync(); } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureServices((hostContext, services) => + { + services.AddHostedService(); + }); } } diff --git a/samples/BookStore/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs b/samples/BookStore/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs new file mode 100644 index 0000000000..e41ee7ed31 --- /dev/null +++ b/samples/BookStore/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs @@ -0,0 +1,26 @@ +using Microsoft.Extensions.Hosting; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp; + +namespace Acme.BookStore.HttpApi.Client.ConsoleTestApp +{ + public class ConsoleTestAppHostedService : IHostedService + { + public async Task StartAsync(CancellationToken cancellationToken) + { + using (var application = AbpApplicationFactory.Create()) + { + application.Initialize(); + + var demo = application.ServiceProvider.GetRequiredService(); + await demo.RunAsync(); + + application.Shutdown(); + } + } + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + } +} diff --git a/samples/BookStore/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/Program.cs b/samples/BookStore/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/Program.cs index ecef29d963..7d8265158b 100644 --- a/samples/BookStore/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/Program.cs +++ b/samples/BookStore/test/Acme.BookStore.HttpApi.Client.ConsoleTestApp/Program.cs @@ -1,24 +1,21 @@ -using System; +using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; -using Volo.Abp; -using Volo.Abp.Threading; +using Microsoft.Extensions.Hosting; namespace Acme.BookStore.HttpApi.Client.ConsoleTestApp { class Program { - static void Main(string[] args) + static async Task Main(string[] args) { - using (var application = AbpApplicationFactory.Create()) - { - application.Initialize(); - - var demo = application.ServiceProvider.GetRequiredService(); - AsyncHelper.RunSync(() => demo.RunAsync()); - - Console.WriteLine("Press ENTER to stop application..."); - Console.ReadLine(); - } + await CreateHostBuilder(args).RunConsoleAsync(); } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureServices((hostContext, services) => + { + services.AddHostedService(); + }); } } diff --git a/samples/DashboardDemo/src/DashboardDemo.DbMigrator/DbMigratorHostedService.cs b/samples/DashboardDemo/src/DashboardDemo.DbMigrator/DbMigratorHostedService.cs new file mode 100644 index 0000000000..dcfd612adc --- /dev/null +++ b/samples/DashboardDemo/src/DashboardDemo.DbMigrator/DbMigratorHostedService.cs @@ -0,0 +1,34 @@ +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using DashboardDemo.Data; +using Serilog; +using Volo.Abp; + +namespace DashboardDemo.DbMigrator +{ + public class DbMigratorHostedService : IHostedService + { + public async Task StartAsync(CancellationToken cancellationToken) + { + using (var application = AbpApplicationFactory.Create(options => + { + options.UseAutofac(); + options.Services.AddLogging(c => c.AddSerilog()); + })) + { + application.Initialize(); + + await application + .ServiceProvider + .GetRequiredService() + .MigrateAsync(); + + application.Shutdown(); + } + } + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + } +} diff --git a/samples/DashboardDemo/src/DashboardDemo.DbMigrator/Program.cs b/samples/DashboardDemo/src/DashboardDemo.DbMigrator/Program.cs index 3cfe1b680a..ccfa70ea32 100644 --- a/samples/DashboardDemo/src/DashboardDemo.DbMigrator/Program.cs +++ b/samples/DashboardDemo/src/DashboardDemo.DbMigrator/Program.cs @@ -1,39 +1,15 @@ -using System.IO; +using System.IO; +using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; -using DashboardDemo.Data; +using Microsoft.Extensions.Hosting; using Serilog; using Serilog.Events; -using Volo.Abp; -using Volo.Abp.Threading; namespace DashboardDemo.DbMigrator { class Program { - static void Main(string[] args) - { - ConfigureLogging(); - - using (var application = AbpApplicationFactory.Create(options => - { - options.UseAutofac(); - options.Services.AddLogging(c => c.AddSerilog()); - })) - { - application.Initialize(); - - AsyncHelper.RunSync( - () => application - .ServiceProvider - .GetRequiredService() - .MigrateAsync() - ); - - application.Shutdown(); - } - } - - private static void ConfigureLogging() + static async Task Main(string[] args) { Log.Logger = new LoggerConfiguration() .MinimumLevel.Information() @@ -48,6 +24,15 @@ namespace DashboardDemo.DbMigrator .WriteTo.File(Path.Combine(Directory.GetCurrentDirectory(), "Logs/logs.txt")) .WriteTo.Console() .CreateLogger(); + + await CreateHostBuilder(args).RunConsoleAsync(); } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureServices((hostContext, services) => + { + services.AddHostedService(); + }); } } diff --git a/samples/DashboardDemo/test/DashboardDemo.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs b/samples/DashboardDemo/test/DashboardDemo.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs new file mode 100644 index 0000000000..b10baf1daa --- /dev/null +++ b/samples/DashboardDemo/test/DashboardDemo.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs @@ -0,0 +1,26 @@ +using Microsoft.Extensions.Hosting; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp; + +namespace DashboardDemo.HttpApi.Client.ConsoleTestApp +{ + public class ConsoleTestAppHostedService : IHostedService + { + public async Task StartAsync(CancellationToken cancellationToken) + { + using (var application = AbpApplicationFactory.Create()) + { + application.Initialize(); + + var demo = application.ServiceProvider.GetRequiredService(); + await demo.RunAsync(); + + application.Shutdown(); + } + } + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + } +} diff --git a/samples/DashboardDemo/test/DashboardDemo.HttpApi.Client.ConsoleTestApp/Program.cs b/samples/DashboardDemo/test/DashboardDemo.HttpApi.Client.ConsoleTestApp/Program.cs index c48298d7d4..7ba0e8151e 100644 --- a/samples/DashboardDemo/test/DashboardDemo.HttpApi.Client.ConsoleTestApp/Program.cs +++ b/samples/DashboardDemo/test/DashboardDemo.HttpApi.Client.ConsoleTestApp/Program.cs @@ -1,24 +1,21 @@ -using System; +using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; -using Volo.Abp; -using Volo.Abp.Threading; +using Microsoft.Extensions.Hosting; namespace DashboardDemo.HttpApi.Client.ConsoleTestApp { class Program { - static void Main(string[] args) + static async Task Main(string[] args) { - using (var application = AbpApplicationFactory.Create()) - { - application.Initialize(); - - var demo = application.ServiceProvider.GetRequiredService(); - AsyncHelper.RunSync(() => demo.RunAsync()); - - Console.WriteLine("Press ENTER to stop application..."); - Console.ReadLine(); - } + await CreateHostBuilder(args).RunConsoleAsync(); } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureServices((hostContext, services) => + { + services.AddHostedService(); + }); } } diff --git a/samples/MicroserviceDemo/applications/ConsoleClientDemo/ConsoleClientDemoHostedService.cs b/samples/MicroserviceDemo/applications/ConsoleClientDemo/ConsoleClientDemoHostedService.cs new file mode 100644 index 0000000000..e07c8564eb --- /dev/null +++ b/samples/MicroserviceDemo/applications/ConsoleClientDemo/ConsoleClientDemoHostedService.cs @@ -0,0 +1,33 @@ +using Microsoft.Extensions.Hosting; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp; +using Serilog; + +namespace ConsoleClientDemo +{ + public class ConsoleClientDemoHostedService : IHostedService + { + public async Task StartAsync(CancellationToken cancellationToken) + { + using (var application = AbpApplicationFactory.Create(options => + { + options.Services.AddLogging(loggingBuilder => + { + loggingBuilder.AddSerilog(dispose: true); + }); + })) + { + application.Initialize(); + + var demo = application.ServiceProvider.GetRequiredService(); + await demo.RunAsync(); + + application.Shutdown(); + } + } + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + } +} diff --git a/samples/MicroserviceDemo/applications/ConsoleClientDemo/Program.cs b/samples/MicroserviceDemo/applications/ConsoleClientDemo/Program.cs index 9626e874a1..40082b1d2a 100644 --- a/samples/MicroserviceDemo/applications/ConsoleClientDemo/Program.cs +++ b/samples/MicroserviceDemo/applications/ConsoleClientDemo/Program.cs @@ -1,48 +1,14 @@ -using Serilog; -using Serilog.Events; -using System; +using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; -using Volo.Abp; -using Volo.Abp.Threading; +using Microsoft.Extensions.Hosting; +using Serilog; +using Serilog.Events; namespace ConsoleClientDemo { internal class Program { - private static void Main(string[] args) - { - InitializeSerilog(); - - Log.Information("Starting ConsoleClientDemo..."); - - try - { - using (var application = AbpApplicationFactory.Create(options => - { - options.Services.AddLogging(loggingBuilder => - { - loggingBuilder.AddSerilog(dispose: true); - }); - })) - { - application.Initialize(); - - var demo = application.ServiceProvider.GetRequiredService(); - AsyncHelper.RunSync(() => demo.RunAsync()); - - Console.WriteLine("Press ENTER to stop application..."); - Console.ReadLine(); - } - } - catch (Exception ex) - { - Log.Error(ex.Message); - Log.Error(ex.StackTrace); - throw; - } - } - - private static void InitializeSerilog() + static async Task Main(string[] args) { Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() @@ -50,6 +16,17 @@ namespace ConsoleClientDemo .Enrich.FromLogContext() .WriteTo.File("Logs/logs.txt") .CreateLogger(); + + Log.Information("Starting ConsoleClientDemo..."); + + await CreateHostBuilder(args).RunConsoleAsync(); } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureServices((hostContext, services) => + { + services.AddHostedService(); + }); } } diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.DbMigrator/DbMigratorHostedService.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.DbMigrator/DbMigratorHostedService.cs new file mode 100644 index 0000000000..730a7f7369 --- /dev/null +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.DbMigrator/DbMigratorHostedService.cs @@ -0,0 +1,34 @@ +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using MyCompanyName.MyProjectName.Data; +using Serilog; +using Volo.Abp; + +namespace MyCompanyName.MyProjectName.DbMigrator +{ + public class DbMigratorHostedService : IHostedService + { + public async Task StartAsync(CancellationToken cancellationToken) + { + using (var application = AbpApplicationFactory.Create(options => + { + options.UseAutofac(); + options.Services.AddLogging(c => c.AddSerilog()); + })) + { + application.Initialize(); + + await application + .ServiceProvider + .GetRequiredService() + .MigrateAsync(); + + application.Shutdown(); + } + } + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + } +} diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.DbMigrator/Program.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.DbMigrator/Program.cs index 1a9175bbee..d1556ea371 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.DbMigrator/Program.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.DbMigrator/Program.cs @@ -1,39 +1,15 @@ -using System.IO; +using System.IO; +using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; -using MyCompanyName.MyProjectName.Data; +using Microsoft.Extensions.Hosting; using Serilog; using Serilog.Events; -using Volo.Abp; -using Volo.Abp.Threading; namespace MyCompanyName.MyProjectName.DbMigrator { class Program { - static void Main(string[] args) - { - ConfigureLogging(); - - using (var application = AbpApplicationFactory.Create(options => - { - options.UseAutofac(); - options.Services.AddLogging(c => c.AddSerilog()); - })) - { - application.Initialize(); - - AsyncHelper.RunSync( - () => application - .ServiceProvider - .GetRequiredService() - .MigrateAsync() - ); - - application.Shutdown(); - } - } - - private static void ConfigureLogging() + static async Task Main(string[] args) { Log.Logger = new LoggerConfiguration() .MinimumLevel.Information() @@ -48,6 +24,15 @@ namespace MyCompanyName.MyProjectName.DbMigrator .WriteTo.File(Path.Combine(Directory.GetCurrentDirectory(), "Logs/logs.txt")) .WriteTo.Console() .CreateLogger(); + + await CreateHostBuilder(args).RunConsoleAsync(); } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureServices((hostContext, services) => + { + services.AddHostedService(); + }); } } diff --git a/templates/app/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs b/templates/app/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs new file mode 100644 index 0000000000..ae929ced30 --- /dev/null +++ b/templates/app/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs @@ -0,0 +1,26 @@ +using Microsoft.Extensions.Hosting; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp; + +namespace MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp +{ + public class ConsoleTestAppHostedService : IHostedService + { + public async Task StartAsync(CancellationToken cancellationToken) + { + using (var application = AbpApplicationFactory.Create()) + { + application.Initialize(); + + var demo = application.ServiceProvider.GetRequiredService(); + await demo.RunAsync(); + + application.Shutdown(); + } + } + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + } +} diff --git a/templates/app/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/Program.cs b/templates/app/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/Program.cs index ada0212483..1e24d8a349 100644 --- a/templates/app/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/Program.cs +++ b/templates/app/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/Program.cs @@ -1,24 +1,21 @@ -using System; +using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; -using Volo.Abp; -using Volo.Abp.Threading; +using Microsoft.Extensions.Hosting; namespace MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp { class Program { - static void Main(string[] args) + static async Task Main(string[] args) { - using (var application = AbpApplicationFactory.Create()) - { - application.Initialize(); - - var demo = application.ServiceProvider.GetRequiredService(); - AsyncHelper.RunSync(() => demo.RunAsync()); - - Console.WriteLine("Press ENTER to stop application..."); - Console.ReadLine(); - } + await CreateHostBuilder(args).RunConsoleAsync(); } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureServices((hostContext, services) => + { + services.AddHostedService(); + }); } } diff --git a/templates/module/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs b/templates/module/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs new file mode 100644 index 0000000000..1ea99ce263 --- /dev/null +++ b/templates/module/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs @@ -0,0 +1,26 @@ +using Microsoft.Extensions.Hosting; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp; + +namespace MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp +{ + public class ConsoleTestAppHostedService : IHostedService + { + public async Task StartAsync(CancellationToken cancellationToken) + { + using (var application = AbpApplicationFactory.Create()) + { + application.Initialize(); + + var demo = application.ServiceProvider.GetRequiredService(); + await demo.RunAsync(); + + application.Shutdown(); + } + } + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + } +} diff --git a/templates/module/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/Program.cs b/templates/module/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/Program.cs index 23192c4e92..1e24d8a349 100644 --- a/templates/module/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/Program.cs +++ b/templates/module/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/Program.cs @@ -1,24 +1,21 @@ -using System; +using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; -using Volo.Abp; -using Volo.Abp.Threading; +using Microsoft.Extensions.Hosting; -namespace MyCompanyName.MyProjectName +namespace MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp { class Program { - static void Main(string[] args) + static async Task Main(string[] args) { - using (var application = AbpApplicationFactory.Create()) - { - application.Initialize(); - - var demo = application.ServiceProvider.GetRequiredService(); - AsyncHelper.RunSync(() => demo.RunAsync()); - - Console.WriteLine("Press ENTER to stop application..."); - Console.ReadLine(); - } + await CreateHostBuilder(args).RunConsoleAsync(); } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureServices((hostContext, services) => + { + services.AddHostedService(); + }); } } From 6b9598530e1a65f11f19050193d32dcf979c26c7 Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 4 Feb 2020 16:37:51 +0800 Subject: [PATCH 2/8] Make proxy script support [FromForm] model binding. --- .../AspNetCoreApiDescriptionModelProvider.cs | 5 ++- .../Modeling/ParameterApiDescriptionModel.cs | 9 +++-- .../JQuery/JQueryProxyScriptGenerator.cs | 6 +++- .../Generators/ProxyScriptingHelper.cs | 2 +- .../Generators/ProxyScriptingJsFuncHelper.cs | 34 +++++++++++++++++-- 5 files changed, 47 insertions(+), 9 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProvider.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProvider.cs index d9cb936326..1c3806f272 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProvider.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProvider.cs @@ -175,7 +175,10 @@ namespace Volo.Abp.AspNetCore.Mvc parameterDescription.RouteInfo?.IsOptional ?? false, parameterDescription.RouteInfo?.DefaultValue, parameterDescription.RouteInfo?.Constraints?.Select(c => c.GetType().Name).ToArray(), - parameterDescription.Source.Id + parameterDescription.Source.Id, + parameterDescription.ModelMetadata.ContainerType != null + ? parameterDescription.ParameterDescriptor.Name + : string.Empty ) ); } diff --git a/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ParameterApiDescriptionModel.cs b/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ParameterApiDescriptionModel.cs index d1de33258f..257f06d3db 100644 --- a/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ParameterApiDescriptionModel.cs +++ b/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ParameterApiDescriptionModel.cs @@ -1,4 +1,4 @@ -using System; +using System; namespace Volo.Abp.Http.Modeling { @@ -19,12 +19,14 @@ namespace Volo.Abp.Http.Modeling public string BindingSourceId { get; set; } + public string DescriptorName { get; set; } + private ParameterApiDescriptionModel() { } - public static ParameterApiDescriptionModel Create(string name, string nameOnMethod, Type type, bool isOptional = false, object defaultValue = null, string[] constraintTypes = null, string bindingSourceId = null) + public static ParameterApiDescriptionModel Create(string name, string nameOnMethod, Type type, bool isOptional = false, object defaultValue = null, string[] constraintTypes = null, string bindingSourceId = null, string descriptorName = null) { return new ParameterApiDescriptionModel { @@ -34,7 +36,8 @@ namespace Volo.Abp.Http.Modeling IsOptional = isOptional, DefaultValue = defaultValue, ConstraintTypes = constraintTypes, - BindingSourceId = bindingSourceId + BindingSourceId = bindingSourceId, + DescriptorName = descriptorName }; } } diff --git a/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/JQuery/JQueryProxyScriptGenerator.cs b/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/JQuery/JQueryProxyScriptGenerator.cs index 169e9b8764..b267c26296 100644 --- a/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/JQuery/JQueryProxyScriptGenerator.cs +++ b/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/JQuery/JQueryProxyScriptGenerator.cs @@ -96,7 +96,11 @@ namespace Volo.Abp.Http.ProxyScripting.Generators.JQuery AddAjaxCallParameters(script, action); - script.AppendLine(" }, ajaxParams));;"); + var ajaxParamsIsFromForm = action.Parameters.Any(x => x.BindingSourceId == ParameterBindingSources.Form); + script.AppendLine(ajaxParamsIsFromForm + ? " }, $.extend(true, {}, ajaxParams, { contentType: 'application/x-www-form-urlencoded; charset=UTF-8' })));" + : " }, ajaxParams));"); + script.AppendLine(" };"); } diff --git a/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/ProxyScriptingHelper.cs b/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/ProxyScriptingHelper.cs index 64d1515574..700dc4e290 100644 --- a/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/ProxyScriptingHelper.cs +++ b/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/ProxyScriptingHelper.cs @@ -66,7 +66,7 @@ namespace Volo.Abp.Http.ProxyScripting.Generators return null; } - return ProxyScriptingJsFuncHelper.CreateJsObjectLiteral(parameters, indent); + return ProxyScriptingJsFuncHelper.CreateJsFormPostData(parameters, indent); } private static string ReplacePathVariables(string url, IList actionParameters) diff --git a/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/ProxyScriptingJsFuncHelper.cs b/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/ProxyScriptingJsFuncHelper.cs index baa52f3155..0fd80203df 100644 --- a/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/ProxyScriptingJsFuncHelper.cs +++ b/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/ProxyScriptingJsFuncHelper.cs @@ -111,8 +111,8 @@ namespace Volo.Abp.Http.ProxyScripting.Generators public static string GetParamNameInJsFunc(ParameterApiDescriptionModel parameterInfo) { return parameterInfo.Name == parameterInfo.NameOnMethod - ? NormalizeJsVariableName(parameterInfo.Name.ToCamelCase(), ".") - : NormalizeJsVariableName(parameterInfo.NameOnMethod.ToCamelCase()) + "." + NormalizeJsVariableName(parameterInfo.Name.ToCamelCase(), "."); + ? NormalizeJsVariableName(parameterInfo.Name.ToCamelCase(), ".") + : NormalizeJsVariableName(parameterInfo.NameOnMethod.ToCamelCase()) + "." + NormalizeJsVariableName(parameterInfo.Name.ToCamelCase(), "."); } public static string CreateJsObjectLiteral(ParameterApiDescriptionModel[] parameters, int indent = 0) @@ -131,9 +131,37 @@ namespace Volo.Abp.Http.ProxyScripting.Generators return sb.ToString(); } + public static string GetFormPostParamNameInJsFunc(ParameterApiDescriptionModel parameterInfo) + { + return parameterInfo.Name == parameterInfo.NameOnMethod + ? NormalizeJsVariableName((parameterInfo.DescriptorName + parameterInfo.Name).ToCamelCase(), ".") + : NormalizeJsVariableName(parameterInfo.NameOnMethod.ToCamelCase()) + "." + NormalizeJsVariableName((parameterInfo.DescriptorName + parameterInfo.Name).ToCamelCase(), "."); + } + + public static string CreateJsFormPostData(ParameterApiDescriptionModel[] parameters, int indent) + { + var sb = new StringBuilder(); + + for (var i = 0; i < parameters.Length; i++) + { + var and = i < parameters.Length - 1 ? " + '&' + " : string.Empty; + + var parameterName = parameters[i].DescriptorName.IsNullOrWhiteSpace() + ? parameters[i].Name + : $"{parameters[i].DescriptorName}.{parameters[i].Name}"; + + sb.Append($"'{parameterName}=' + {GetFormPostParamNameInJsFunc(parameters[i])}{and}"); + } + + return sb.ToString(); + } + public static string GenerateJsFuncParameterList(ActionApiDescriptionModel action, string ajaxParametersName) { - var methodParamNames = action.ParametersOnMethod.Select(p => p.Name).Distinct().ToList(); + var paramsIsFromForm = action.Parameters.Any(x => x.BindingSourceId == ParameterBindingSources.Form); + var methodParamNames = paramsIsFromForm + ? action.Parameters.Select(p => p.DescriptorName + p.Name).Distinct().ToList() + : action.ParametersOnMethod.Select(p => p.Name).Distinct().ToList(); methodParamNames.Add(ajaxParametersName); return methodParamNames.Select(prmName => NormalizeJsVariableName(prmName.ToCamelCase())).JoinAsString(", "); } From 61d8e68a0aebed305f51c81125e713481e2fa7c3 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Tue, 4 Feb 2020 12:44:27 +0300 Subject: [PATCH 3/8] Default roles on registration resolves https://github.com/abpframework/abp/issues/2721 --- .../Volo/Abp/Account/AccountAppService.cs | 17 +++++++++++-- .../Pages/Account/Register.cshtml.cs | 24 ++++++++++++++++--- .../Abp/Identity/IIdentityRoleRepository.cs | 4 ++++ .../EfCoreIdentityRoleRepository.cs | 5 ++++ .../MongoDB/MongoIdentityRoleRepository.cs | 5 ++++ .../Identity/IdentityRoleRepository_Tests.cs | 11 +++++++++ 6 files changed, 61 insertions(+), 5 deletions(-) diff --git a/modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/AccountAppService.cs b/modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/AccountAppService.cs index 9935078fe1..ab703dd223 100644 --- a/modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/AccountAppService.cs +++ b/modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/AccountAppService.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Linq; +using System.Threading.Tasks; using Microsoft.AspNetCore.Identity; using Volo.Abp.Account.Settings; using Volo.Abp.Application.Services; @@ -9,11 +10,14 @@ namespace Volo.Abp.Account { public class AccountAppService : ApplicationService, IAccountAppService { + private readonly IIdentityRoleRepository _roleRepository; protected IdentityUserManager UserManager { get; } public AccountAppService( - IdentityUserManager userManager) + IdentityUserManager userManager, + IIdentityRoleRepository roleRepository) { + _roleRepository = roleRepository; UserManager = userManager; } @@ -25,9 +29,18 @@ namespace Volo.Abp.Account (await UserManager.CreateAsync(user, input.Password).ConfigureAwait(false)).CheckErrors(); + await SetDefaultRolesAsync(user); + return ObjectMapper.Map(user); } + protected virtual async Task SetDefaultRolesAsync(IdentityUser user) + { + var defaultRoles = await _roleRepository.GetDefaultOnesAsync().ConfigureAwait(false); + + await UserManager.SetRolesAsync(user, defaultRoles.Select(r => r.Name)).ConfigureAwait(false); + } + protected virtual async Task CheckSelfRegistrationAsync() { if (!await SettingProvider.IsTrueAsync(AccountSettingNames.IsSelfRegistrationEnabled).ConfigureAwait(false)) diff --git a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs index 40b819e0e1..e521db5f9d 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs +++ b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs @@ -1,8 +1,10 @@ using System.ComponentModel.DataAnnotations; +using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Volo.Abp.Account.Settings; +using Volo.Abp.Application.Dtos; using Volo.Abp.Identity; using Volo.Abp.Settings; using Volo.Abp.Uow; @@ -12,6 +14,8 @@ namespace Volo.Abp.Account.Web.Pages.Account { public class RegisterModel : AccountPageModel { + private readonly IIdentityRoleRepository _roleRepository; + [BindProperty(SupportsGet = true)] public string ReturnUrl { get; set; } @@ -21,7 +25,12 @@ namespace Volo.Abp.Account.Web.Pages.Account [BindProperty] public PostInput Input { get; set; } - public virtual async Task OnGet() + public RegisterModel(IIdentityRoleRepository roleRepository) + { + _roleRepository = roleRepository; + } + + public virtual async Task OnGetAsync() { await CheckSelfRegistrationAsync().ConfigureAwait(false); } @@ -35,15 +44,24 @@ namespace Volo.Abp.Account.Web.Pages.Account var user = new IdentityUser(GuidGenerator.Create(), Input.UserName, Input.EmailAddress, CurrentTenant.Id); - (await UserManager.CreateAsync(user, Input.Password).ConfigureAwait(false)).CheckErrors(); - + (await UserManager.CreateAsync(user, Input.Password).ConfigureAwait(false)).CheckErrors(); + await UserManager.SetEmailAsync(user, Input.EmailAddress).ConfigureAwait(false); + await SetDefaultRolesAsync(user); + await SignInManager.SignInAsync(user, isPersistent: false).ConfigureAwait(false); return Redirect(ReturnUrl ?? "/"); //TODO: How to ensure safety? IdentityServer requires it however it should be checked somehow! } + protected async Task SetDefaultRolesAsync(IdentityUser user) + { + var defaultRoles = await _roleRepository.GetDefaultOnesAsync().ConfigureAwait(false); + + await UserManager.SetRolesAsync(user, defaultRoles.Select(r => r.Name)).ConfigureAwait(false); + } + protected virtual async Task CheckSelfRegistrationAsync() { if (!await SettingProvider.IsTrueAsync(AccountSettingNames.IsSelfRegistrationEnabled).ConfigureAwait(false) || diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs index 6ce3ce3c0c..ce8c07d22b 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs @@ -21,5 +21,9 @@ namespace Volo.Abp.Identity bool includeDetails = false, CancellationToken cancellationToken = default ); + + Task> GetDefaultOnesAsync( + CancellationToken cancellationToken = default + ); } } \ No newline at end of file diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs index 731cc6df23..d61da8d90e 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs @@ -42,6 +42,11 @@ namespace Volo.Abp.Identity.EntityFrameworkCore .ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false); } + public virtual async Task> GetDefaultOnesAsync(CancellationToken cancellationToken = default) + { + return await DbSet.Where(r => r.IsDefault).ToListAsync(cancellationToken: cancellationToken); + } + public override IQueryable WithDetails() { return GetQueryable().IncludeDetails(); diff --git a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityRoleRepository.cs b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityRoleRepository.cs index 4bae372c8e..ac55012312 100644 --- a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityRoleRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityRoleRepository.cs @@ -43,5 +43,10 @@ namespace Volo.Abp.Identity.MongoDB .PageBy>(skipCount, maxResultCount) .ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false); } + + public virtual async Task> GetDefaultOnesAsync(CancellationToken cancellationToken = default) + { + return await GetMongoQueryable().Where(r => r.IsDefault).ToListAsync(cancellationToken: cancellationToken); + } } } \ No newline at end of file diff --git a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs index cd7ee1eb32..9f427d3f04 100644 --- a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs +++ b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs @@ -36,6 +36,17 @@ namespace Volo.Abp.Identity roles.ShouldContain(r => r.Name == "supporter"); } + [Fact] + public async Task GetDefaultOnesAsync() + { + var roles = await RoleRepository.GetDefaultOnesAsync().ConfigureAwait(false); + + foreach (var role in roles) + { + role.IsDefault.ShouldBe(true); + } + } + [Fact] public async Task GetCountAsync() { From f727696e21ca31d8c60d5c53be53a4a803ab2b56 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Tue, 4 Feb 2020 12:53:30 +0300 Subject: [PATCH 4/8] registration refactor --- .../Volo/Abp/Account/AccountAppService.cs | 2 ++ .../Pages/Account/Register.cshtml.cs | 28 ++++++++----------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/AccountAppService.cs b/modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/AccountAppService.cs index ab703dd223..639e26b7ef 100644 --- a/modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/AccountAppService.cs +++ b/modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/AccountAppService.cs @@ -29,6 +29,8 @@ namespace Volo.Abp.Account (await UserManager.CreateAsync(user, input.Password).ConfigureAwait(false)).CheckErrors(); + await UserManager.SetEmailAsync(user,input.EmailAddress).ConfigureAwait(false); + await SetDefaultRolesAsync(user); return ObjectMapper.Map(user); diff --git a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs index e521db5f9d..a690e9dc35 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs +++ b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs @@ -14,7 +14,7 @@ namespace Volo.Abp.Account.Web.Pages.Account { public class RegisterModel : AccountPageModel { - private readonly IIdentityRoleRepository _roleRepository; + private readonly IAccountAppService _accountAppService; [BindProperty(SupportsGet = true)] public string ReturnUrl { get; set; } @@ -25,9 +25,9 @@ namespace Volo.Abp.Account.Web.Pages.Account [BindProperty] public PostInput Input { get; set; } - public RegisterModel(IIdentityRoleRepository roleRepository) + public RegisterModel(IAccountAppService accountAppService) { - _roleRepository = roleRepository; + _accountAppService = accountAppService; } public virtual async Task OnGetAsync() @@ -42,26 +42,22 @@ namespace Volo.Abp.Account.Web.Pages.Account await CheckSelfRegistrationAsync().ConfigureAwait(false); - var user = new IdentityUser(GuidGenerator.Create(), Input.UserName, Input.EmailAddress, CurrentTenant.Id); - - (await UserManager.CreateAsync(user, Input.Password).ConfigureAwait(false)).CheckErrors(); - - await UserManager.SetEmailAsync(user, Input.EmailAddress).ConfigureAwait(false); + var registerDto = new RegisterDto + { + AppName = "MVC", + EmailAddress = Input.EmailAddress, + Password = Input.Password, + UserName = Input.UserName + }; - await SetDefaultRolesAsync(user); + var userDto = await _accountAppService.RegisterAsync(registerDto); + var user = await UserManager.GetByIdAsync(userDto.Id); await SignInManager.SignInAsync(user, isPersistent: false).ConfigureAwait(false); return Redirect(ReturnUrl ?? "/"); //TODO: How to ensure safety? IdentityServer requires it however it should be checked somehow! } - protected async Task SetDefaultRolesAsync(IdentityUser user) - { - var defaultRoles = await _roleRepository.GetDefaultOnesAsync().ConfigureAwait(false); - - await UserManager.SetRolesAsync(user, defaultRoles.Select(r => r.Name)).ConfigureAwait(false); - } - protected virtual async Task CheckSelfRegistrationAsync() { if (!await SettingProvider.IsTrueAsync(AccountSettingNames.IsSelfRegistrationEnabled).ConfigureAwait(false) || From 4e3c647c29608ac4e04a73e111b31386d386efc2 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Tue, 4 Feb 2020 13:35:34 +0300 Subject: [PATCH 5/8] IIdentityRoleRepository fixes --- .../Volo/Abp/Identity/IIdentityRoleRepository.cs | 1 + .../EntityFrameworkCore/EfCoreIdentityRoleRepository.cs | 5 +++-- .../Volo/Abp/Identity/MongoDB/MongoIdentityRoleRepository.cs | 5 +++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs index ce8c07d22b..3a344b3140 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs @@ -23,6 +23,7 @@ namespace Volo.Abp.Identity ); Task> GetDefaultOnesAsync( + bool includeDetails = false, CancellationToken cancellationToken = default ); } diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs index d61da8d90e..2abb9e7a2e 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs @@ -42,9 +42,10 @@ namespace Volo.Abp.Identity.EntityFrameworkCore .ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false); } - public virtual async Task> GetDefaultOnesAsync(CancellationToken cancellationToken = default) + public virtual async Task> GetDefaultOnesAsync( + bool includeDetails = false, CancellationToken cancellationToken = default) { - return await DbSet.Where(r => r.IsDefault).ToListAsync(cancellationToken: cancellationToken); + return await DbSet.Where(r => r.IsDefault).ToListAsync(GetCancellationToken(cancellationToken)); } public override IQueryable WithDetails() diff --git a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityRoleRepository.cs b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityRoleRepository.cs index ac55012312..af5d288fa1 100644 --- a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityRoleRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityRoleRepository.cs @@ -44,9 +44,10 @@ namespace Volo.Abp.Identity.MongoDB .ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false); } - public virtual async Task> GetDefaultOnesAsync(CancellationToken cancellationToken = default) + public virtual async Task> GetDefaultOnesAsync( + bool includeDetails = false, CancellationToken cancellationToken = default) { - return await GetMongoQueryable().Where(r => r.IsDefault).ToListAsync(cancellationToken: cancellationToken); + return await GetMongoQueryable().Where(r => r.IsDefault).ToListAsync(cancellationToken: GetCancellationToken(cancellationToken)); } } } \ No newline at end of file From 6b8a2ad3f6593dcfd27286ae14ad6a25a9952169 Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 5 Feb 2020 13:18:47 +0800 Subject: [PATCH 6/8] Check EnableLocalLogin in the login method of the account module. Resolve #2758 --- .../Account/Localization/Resources/en.json | 1 + .../Localization/Resources/zh-Hans.json | 1 + .../Localization/Resources/zh-Hant.json | 1 + .../IdentityServerSupportedLoginModel.cs | 4 ++-- .../Account/Controllers/AccountController.cs | 21 +++++++++++++++++-- .../Pages/Account/Login.cshtml.cs | 10 ++++++++- 6 files changed, 33 insertions(+), 5 deletions(-) diff --git a/modules/account/src/Volo.Abp.Account.Application.Contracts/Volo/Abp/Account/Localization/Resources/en.json b/modules/account/src/Volo.Abp.Account.Application.Contracts/Volo/Abp/Account/Localization/Resources/en.json index ea2d94f5f2..7cf2cf7be6 100644 --- a/modules/account/src/Volo.Abp.Account.Application.Contracts/Volo/Abp/Account/Localization/Resources/en.json +++ b/modules/account/src/Volo.Abp.Account.Application.Contracts/Volo/Abp/Account/Localization/Resources/en.json @@ -11,6 +11,7 @@ "InvalidUserNameOrPassword": "Invalid username or password!", "LoginIsNotAllowed": "You are not allowed to login! You need to confirm your email/phone number.", "SelfRegistrationDisabledMessage": "Self-registration is disabled for this application. Please contact the application administrator to register a new user.", + "LocalLoginDisabledMessage": "Local login is disabled for this application.", "Login": "Login", "Cancel": "Cancel", "Register": "Register", diff --git a/modules/account/src/Volo.Abp.Account.Application.Contracts/Volo/Abp/Account/Localization/Resources/zh-Hans.json b/modules/account/src/Volo.Abp.Account.Application.Contracts/Volo/Abp/Account/Localization/Resources/zh-Hans.json index c778a28dd6..c02ff8669b 100644 --- a/modules/account/src/Volo.Abp.Account.Application.Contracts/Volo/Abp/Account/Localization/Resources/zh-Hans.json +++ b/modules/account/src/Volo.Abp.Account.Application.Contracts/Volo/Abp/Account/Localization/Resources/zh-Hans.json @@ -11,6 +11,7 @@ "InvalidUserNameOrPassword": "用户名或密码错误!", "LoginIsNotAllowed": "无法登录!你需要验证邮箱地址/手机号.", "SelfRegistrationDisabledMessage": "应用程序未开放注册,请联系管理员添加新用户.", + "LocalLoginDisabledMessage": "应用程序未开放本地账户登录.", "Login": "登录", "Cancel": "取消", "Register": "注册", diff --git a/modules/account/src/Volo.Abp.Account.Application.Contracts/Volo/Abp/Account/Localization/Resources/zh-Hant.json b/modules/account/src/Volo.Abp.Account.Application.Contracts/Volo/Abp/Account/Localization/Resources/zh-Hant.json index e21631e92e..b75be0f73a 100644 --- a/modules/account/src/Volo.Abp.Account.Application.Contracts/Volo/Abp/Account/Localization/Resources/zh-Hant.json +++ b/modules/account/src/Volo.Abp.Account.Application.Contracts/Volo/Abp/Account/Localization/Resources/zh-Hant.json @@ -11,6 +11,7 @@ "InvalidUserNameOrPassword": "使用者名稱或密碼錯誤!", "LoginIsNotAllowed": "無法登入!你需要驗證電子信箱地址/手機號碼.", "SelfRegistrationDisabledMessage": "應用程式未開放註冊,請聯絡管理員以加入新使用者.", + "LocalLoginDisabledMessage": "應用程序未開放本地賬戶登錄.", "Login": "登入", "Cancel": "取消", "Register": "註冊", diff --git a/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/IdentityServerSupportedLoginModel.cs b/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/IdentityServerSupportedLoginModel.cs index 2fa2aa5778..d5ea1538a1 100644 --- a/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/IdentityServerSupportedLoginModel.cs +++ b/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/IdentityServerSupportedLoginModel.cs @@ -107,8 +107,6 @@ namespace Volo.Abp.Account.Web.Pages.Account [UnitOfWork] //TODO: Will be removed when we implement action filter public override async Task OnPostAsync(string action) { - EnableLocalLogin = await SettingProvider.IsTrueAsync(AccountSettingNames.EnableLocalLogin).ConfigureAwait(false); - if (action == "Cancel") { var context = await Interaction.GetAuthorizationContextAsync(ReturnUrl).ConfigureAwait(false); @@ -122,6 +120,8 @@ namespace Volo.Abp.Account.Web.Pages.Account return Redirect(ReturnUrl); } + await CheckLocalLoginAsync(); + ValidateModel(); await ReplaceEmailToUsernameOfInputIfNeeds().ConfigureAwait(false); diff --git a/modules/account/src/Volo.Abp.Account.Web/Areas/Account/Controllers/AccountController.cs b/modules/account/src/Volo.Abp.Account.Web/Areas/Account/Controllers/AccountController.cs index 74d1b39ff6..6f02f7efa8 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Areas/Account/Controllers/AccountController.cs +++ b/modules/account/src/Volo.Abp.Account.Web/Areas/Account/Controllers/AccountController.cs @@ -1,10 +1,13 @@ -using System; +using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; +using Volo.Abp.Account.Localization; +using Volo.Abp.Account.Settings; using Volo.Abp.Account.Web.Areas.Account.Controllers.Models; using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.Identity; +using Volo.Abp.Settings; using Volo.Abp.Validation; using SignInResult = Microsoft.AspNetCore.Identity.SignInResult; using UserLoginInfo = Volo.Abp.Account.Web.Areas.Account.Controllers.Models.UserLoginInfo; @@ -21,17 +24,23 @@ namespace Volo.Abp.Account.Web.Areas.Account.Controllers { private readonly SignInManager _signInManager; private readonly IdentityUserManager _userManager; + private readonly ISettingProvider _settingProvider; - public AccountController(SignInManager signInManager, IdentityUserManager userManager) + public AccountController(SignInManager signInManager, IdentityUserManager userManager, ISettingProvider settingProvider) { + LocalizationResource = typeof(AccountResource); + _signInManager = signInManager; _userManager = userManager; + _settingProvider = settingProvider; } [HttpPost] [Route("login")] public virtual async Task Login(UserLoginInfo login) { + await CheckLocalLoginAsync(); + ValidateLoginInfo(login); await ReplaceEmailToUsernameOfInputIfNeeds(login).ConfigureAwait(false); @@ -133,5 +142,13 @@ namespace Volo.Abp.Account.Web.Areas.Account.Controllers throw new ArgumentNullException(nameof(login.Password)); } } + + private async Task CheckLocalLoginAsync() + { + if (!await _settingProvider.IsTrueAsync(AccountSettingNames.EnableLocalLogin).ConfigureAwait(false)) + { + throw new UserFriendlyException(L["LocalLoginDisabledMessage"]); + } + } } } diff --git a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Login.cshtml.cs b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Login.cshtml.cs index eb4ad4ad04..d1dc8812fa 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Login.cshtml.cs +++ b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Login.cshtml.cs @@ -89,7 +89,7 @@ namespace Volo.Abp.Account.Web.Pages.Account [UnitOfWork] //TODO: Will be removed when we implement action filter public virtual async Task OnPostAsync(string action) { - EnableLocalLogin = await SettingProvider.IsTrueAsync(AccountSettingNames.EnableLocalLogin).ConfigureAwait(false); + await CheckLocalLoginAsync(); ValidateModel(); @@ -237,6 +237,14 @@ namespace Volo.Abp.Account.Web.Pages.Account LoginInput.UserNameOrEmailAddress = userByEmail.UserName; } + protected virtual async Task CheckLocalLoginAsync() + { + if (!await SettingProvider.IsTrueAsync(AccountSettingNames.EnableLocalLogin).ConfigureAwait(false)) + { + throw new UserFriendlyException(L["LocalLoginDisabledMessage"]); + } + } + public class LoginInputModel { [Required] From 5ac68c00def60dfd6a90f1adf402a38006daeb43 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Wed, 5 Feb 2020 09:41:16 +0300 Subject: [PATCH 7/8] IncludeDetails implementation in EfCoreIdentityRoleRepository.GetDefaultOnesAsync --- .../EntityFrameworkCore/EfCoreIdentityRoleRepository.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs index 2abb9e7a2e..60fec622f2 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs @@ -45,7 +45,7 @@ namespace Volo.Abp.Identity.EntityFrameworkCore public virtual async Task> GetDefaultOnesAsync( bool includeDetails = false, CancellationToken cancellationToken = default) { - return await DbSet.Where(r => r.IsDefault).ToListAsync(GetCancellationToken(cancellationToken)); + return await DbSet.IncludeDetails(includeDetails).Where(r => r.IsDefault).ToListAsync(GetCancellationToken(cancellationToken)); } public override IQueryable WithDetails() From ae1dec6fba1cd3828b355dc039e310b5a561b80d Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Wed, 5 Feb 2020 15:08:41 +0300 Subject: [PATCH 8/8] Update ModuleInfo.cs --- .../Abp/Cli/ProjectBuilding/Building/ModuleInfo.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/ModuleInfo.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/ModuleInfo.cs index dfeabf6aed..fadd830c93 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/ModuleInfo.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/ModuleInfo.cs @@ -7,5 +7,19 @@ public string Namespace { get; set; } public string DocumentUrl { get; set; } + + public string DisplayName { get; set; } + + public string ShortDescription { get; set; } + + public bool IsPro { get; set; } + + public bool EfCoreSupport { get; set; } + + public bool MongoDBSupport { get; set; } + + public bool AngularUi { get; set; } + + public bool MvcUi { get; set; } } } \ No newline at end of file