Microservice.HttpApi.Convertion.Contracts
1.0.1
dotnet add package Microservice.HttpApi.Convertion.Contracts --version 1.0.1
NuGet\Install-Package Microservice.HttpApi.Convertion.Contracts -Version 1.0.1
<PackageReference Include="Microservice.HttpApi.Convertion.Contracts" Version="1.0.1" />
<PackageVersion Include="Microservice.HttpApi.Convertion.Contracts" Version="1.0.1" />
<PackageReference Include="Microservice.HttpApi.Convertion.Contracts" />
paket add Microservice.HttpApi.Convertion.Contracts --version 1.0.1
#r "nuget: Microservice.HttpApi.Convertion.Contracts, 1.0.1"
#:package Microservice.HttpApi.Convertion.Contracts@1.0.1
#addin nuget:?package=Microservice.HttpApi.Convertion.Contracts&version=1.0.1
#tool nuget:?package=Microservice.HttpApi.Convertion.Contracts&version=1.0.1
如何使用 Microservice 项目自动生成 API
首先我们假设存在2个 WebHost 项目,其中 Main 需要用到 Sub 服务的接口
配置提供服务的 Host(本例为 Sub)
创建
Sub.Contracts项目引入以下项目:
Microservice.HttpApi.ClientMicroservice.HttpApi.Convertion.ContractsMicroservice.HttpApi.Convertion创建服务接口,例如
ISubService:public interface ISubService : IApplicationService // 需要实现 IApplicationService { public Task<string> EchoAsync(string message); }创建 Sub.Contracts 模块,例如
SubContractsModule:[DependsOn(typeof(AbpApplicationContractsModule))] public class SubContractsModule : AbpModule { }接下来在提供对应服务的项目(本例为
Sub)中操作,引入以下项目:Microservice.HttpApi.ConvertionSub.Contracts在该项目中实现
ISubService接口:[AllowAnonymous] [ApiExplorerSettings(GroupName = "Api 分组")] public class SubService : ApplicationService, ISubService { [ApiConvertion("/sub/echo", Description = "Echo")] public Task<string> EchoAsync(string message) { var list = AppMenus(); return Task.FromResult(message); } [ApiConvertion(true)] public async Task<List<AppMenuTree>> App() { } }在该项目的模块文件中添加以下代码:
public class SubModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { base.ConfigureServices(context); var srv = context.Services; // 在这添加自动生成 APIController 配置 ConfigureAutoApiControllers(); // <- 添加这行 } /// <summary> /// 自动 API 生成 /// </summary> private void ConfigureAutoApiControllers() { Configure<AbpAspNetCoreMvcOptions>( // 这里的 Module 是提供上述 SubService 项目对应模块 o => o.ConventionalControllers.Create(typeof(SubModule).Assembly, opts => { // 接口前缀,前后不需要斜杠 opts.RootPath = "api"; // 关闭自动搜索类型 opts.TypePredicate = type => false; // 需要自动转换的应用服务添加在这里 opts.ControllerTypes.Add(typeof(SubService)); })); } public override void OnApplicationInitialization(ApplicationInitializationContext context) { var app = context.GetApplicationBuilder(); // 自动 API 生成 app.UseConfiguredEndpoints(); // <- 添加这行 } }
至此,如果设置了 Swagger,应该能在页面上看到该服务,并使用
配置使用服务的 Host(本例为 Main)
Main项目中引入以下依赖:Volo.Abp.Http.Client(如果需要转发用户信息,需要配合Microservice.HttpApi.Client)Sub.Constracts在
Module类中添加以下配置:/// 注意:必须在模块类上增加依赖 AbpHttpClientModule public override void ConfigureServices(ServiceConfigurationContext context) { base.ConfigureServices(context); // 添加远程服务 context.Services.AddHttpClientProxies( // 注意这里的 Sub, 需要和后面 appsettings.json 中对应 typeof(SubContractsModule).Assembly, "Sub", asDefaultServices: false); }在
appsettings.json中指定服务 url:"RemoteServices": { "Sub": { "BaseUrl": "http://localhost:5010/" } }在需要使用该服务的地方注入:
IHttpClientProxy<ISubService>:public class SomeController { private readonly IHttpClientProxy<ISubService> SubServiceClient; public SomeController(IHttpClientProxy<ISubService> subServiceClient) { SubServiceClient = subServiceClient; } public Task<string> Hello(string message) { return SubServiceClient.Service.EchoAsync(message); } }完毕
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net5.0
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
- System.IO.FileSystem.Primitives (>= 4.3.0)
- System.Runtime.Handles (>= 4.3.0)
- System.Runtime.InteropServices (>= 4.3.0)
- Volo.Abp.Core (>= 4.3.3)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Microservice.HttpApi.Convertion.Contracts:
| Package | Downloads |
|---|---|
|
CloudPlatform20.Application.Contracts
接口层 |
|
|
Microservice.HttpApi.Convertion
Microservice.HttpApi.Convertion |
GitHub repositories
This package is not used by any popular GitHub repositories.
Microservice.HttpApi.Convertion.Contracts