feat: 添加客户端代理层

This commit is contained in:
陈淳
2024-03-20 17:52:59 +08:00
parent b6ec8cf6f0
commit f53a6cf05b
16 changed files with 300 additions and 3 deletions

View File

@@ -0,0 +1,33 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Yi.Abp.Client.Console;
using Yi.Framework.Rbac.Application.Contracts.IServices;
try
{
IHost host = Host.CreateDefaultBuilder()
.ConfigureServices(async (host, service) =>
{
await service.AddApplicationAsync<YiAbpClientConsoleModule>();
})
.UseAutofac()
.Build();
//控制台直接调用
var account = host.Services.GetRequiredService<IAccountService>();
//获取验证码
var data1 = await account.GetCaptchaImageAsync();
//登录
var data2 = await account.PostLoginAsync(new Yi.Framework.Rbac.Application.Contracts.Dtos.Account.LoginInputVo { UserName = "cc", Password = "123456", Code = string.Empty, Uuid = string.Empty });
host.Run();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}

View File

@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Volo.Abp.Autofac" Version="8.0.5" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Yi.Abp.HttpApi.Client\Yi.Abp.HttpApi.Client.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Modularity;
using Yi.Abp.HttpApi.Client;
namespace Yi.Abp.Client.Console
{
[DependsOn(typeof(YiAbpHttpApiClientModule))]
public class YiAbpClientConsoleModule:AbpModule
{
}
}

View File

@@ -0,0 +1,37 @@
using Microsoft.AspNetCore.Mvc;
using Yi.Framework.Rbac.Application.Contracts.Dtos.Account;
using Yi.Framework.Rbac.Application.Contracts.IServices;
namespace Yi.Abp.Client.WebApi.Controllers
{
[ApiController]
[Route("[controller]")]
public class AccountController : ControllerBase
{
private readonly ILogger<AccountController> _logger;
private IAccountService _accountService;
public AccountController(ILogger<AccountController> logger, IAccountService accountService)
{
_logger = logger;
_accountService = accountService;
}
[HttpPost("my-login")]
public async Task<IActionResult> Login(LoginInputVo input)
{
await _accountService.PostLoginAsync(input);
return Ok();
}
[HttpGet("my-captcha-image")]
public async Task<IActionResult> CaptchaImageAsync()
{
var output = await _accountService.GetCaptchaImageAsync();
return Ok(output);
}
}
}

View File

@@ -0,0 +1,28 @@
using Autofac.Core;
using Yi.Abp.Client.WebApi;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Host.UseAutofac();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
await builder.Services.AddApplicationAsync<YiAbpClientWebApiModule>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:35597",
"sslPort": 44322
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5002",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7108;http://localhost:5002",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Yi.Abp.HttpApi.Client\Yi.Abp.HttpApi.Client.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,6 @@
@Yi.Abp.Client.WebApi_HostAddress = http://localhost:5002
GET {{Yi.Abp.Client.WebApi_HostAddress}}/weatherforecast/
Accept: application/json
###

View File

@@ -0,0 +1,10 @@
using Volo.Abp.Modularity;
using Yi.Abp.HttpApi.Client;
namespace Yi.Abp.Client.WebApi
{
[DependsOn(typeof(YiAbpHttpApiClientModule))]
public class YiAbpClientWebApiModule:AbpModule
{
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

View File

@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Library</OutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Volo.Abp.Http.Client" Version="8.0.5" />
<PackageReference Include="Volo.Abp.Autofac" Version="8.0.5" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Yi.Abp.Application.Contracts\Yi.Abp.Application.Contracts.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,32 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Autofac;
using Volo.Abp.Http.Client;
using Volo.Abp.Modularity;
using Yi.Framework.Rbac.Application.Contracts;
namespace Yi.Abp.HttpApi.Client
{
[DependsOn(typeof(AbpHttpClientModule),
typeof(AbpAutofacModule),
typeof(YiFrameworkRbacApplicationContractsModule))]
public class YiAbpHttpApiClientModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
//创建动态客户端代理
context.Services.AddHttpClientProxies(
typeof(YiFrameworkRbacApplicationContractsModule).Assembly
);
Configure<AbpRemoteServiceOptions>(options =>
{
options.RemoteServices.Default =
new RemoteServiceConfiguration("http://localhost:19001");
});
}
}
}