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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Microservice.HttpApi.Convertion.Contracts" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Microservice.HttpApi.Convertion.Contracts" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Microservice.HttpApi.Convertion.Contracts" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Microservice.HttpApi.Convertion.Contracts --version 1.0.1
                    
#r "nuget: Microservice.HttpApi.Convertion.Contracts, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Microservice.HttpApi.Convertion.Contracts@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Microservice.HttpApi.Convertion.Contracts&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Microservice.HttpApi.Convertion.Contracts&version=1.0.1
                    
Install as a Cake Tool

如何使用 Microservice 项目自动生成 API

首先我们假设存在2个 WebHost 项目,其中 Main 需要用到 Sub 服务的接口

配置提供服务的 Host(本例为 Sub)

  1. 创建 Sub.Contracts 项目

  2. 引入以下项目:

    Microservice.HttpApi.Client Microservice.HttpApi.Convertion.Contracts Microservice.HttpApi.Convertion

  3. 创建服务接口,例如 ISubService

    public interface ISubService : IApplicationService // 需要实现 IApplicationService
    {
        public Task<string> EchoAsync(string message);
    }
    
  4. 创建 Sub.Contracts 模块,例如 SubContractsModule

    [DependsOn(typeof(AbpApplicationContractsModule))]
    public class SubContractsModule : AbpModule
    {
    
    }
    
  5. 接下来在提供对应服务的项目(本例为 Sub)中操作,引入以下项目:

    Microservice.HttpApi.Convertion

    Sub.Contracts

  6. 在该项目中实现 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()
        {
        }
    }
    
  7. 在该项目的模块文件中添加以下代码:

    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)

  1. Main 项目中引入以下依赖:

    Volo.Abp.Http.Client(如果需要转发用户信息,需要配合 Microservice.HttpApi.Client

    Sub.Constracts

  2. Module 类中添加以下配置:

    /// 注意:必须在模块类上增加依赖 AbpHttpClientModule
    
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        base.ConfigureServices(context);
    
        // 添加远程服务
        context.Services.AddHttpClientProxies(
            // 注意这里的 Sub, 需要和后面 appsettings.json 中对应
            typeof(SubContractsModule).Assembly, "Sub", asDefaultServices: false);
    }
    
  3. appsettings.json 中指定服务 url:

    "RemoteServices": {
      "Sub": {
        "BaseUrl": "http://localhost:5010/"
      }
    }
    
  4. 在需要使用该服务的地方注入: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);
        }
    }
    
  5. 完毕

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last Updated
1.0.1 921 6/30/2021
1.0.0.1 504 6/30/2021
1.0.0 637 6/29/2021

Microservice.HttpApi.Convertion.Contracts