ABP VNext 版本控制


在Host项目的Module进行配置

1.PreConfigureServices

 public override void PreConfigureServices(ServiceConfigurationContext context)
    {
        PreConfigure<AbpAspNetCoreMvcOptions>(options =>
        {
            //2.0 Version
            options.ConventionalControllers.Create(typeof(AbpVersioningDemoModule).Assembly, opts =>
            {
                opts.TypePredicate = t => !string.IsNullOrEmpty(t.Namespace) && t.Namespace.Contains("Abp.Versioning.Demo.App.v2");
                //opts.TypePredicate = t => t.Namespace == typeof(Abp.Versioning.Demo.App.AutoAppService).Namespace;
                opts.ApiVersions.Add(new ApiVersion(2, 0));
            });

            //1.0 Compatibility version
            options.ConventionalControllers.Create(typeof(AbpVersioningDemoModule).Assembly, opts =>
            {
                opts.TypePredicate = t => t.Namespace == typeof(Abp.Versioning.Demo.App.v1.TodoAppService).Namespace;
                //opts.TypePredicate = t => t.Namespace == typeof(Abp.Versioning.Demo.App.AutoAppService).Namespace;
                opts.ApiVersions.Add(new ApiVersion(1, 0));
            });
        });
    }

2.ConfigureServices

 public override void ConfigureServices(ServiceConfigurationContext context)
    {
        var preActions = context.Services.GetPreConfigureActions<AbpAspNetCoreMvcOptions>();
        Configure<AbpAspNetCoreMvcOptions>(options =>
        {
            preActions.Configure(options);
        });

        context.Services.AddAbpApiVersioning(options =>
        {
            options.UseApiBehavior = false;
            options.ReportApiVersions = true;
            options.AssumeDefaultVersionWhenUnspecified = true;
            //options.ApiVersionReader = new HeaderApiVersionReader("api-version"); //Supports header too
            //options.ApiVersionReader = new MediaTypeApiVersionReader(); //Supports accept header too
            options.ConfigureAbp(preActions.Configure());
        });

        context.Services.AddVersionedApiExplorer(
          options =>
          {
              // add the versioned api explorer, which also adds IApiVersionDescriptionProvider service
              // note: the specified format code will format the version as "'v'major[.minor][-status]"
              options.GroupNameFormat = "'v'VVV";
              // note: this option is only necessary when versioning by url segment. the SubstitutionFormat
              // can also be used to control the format of the API version in route templates
              options.SubstituteApiVersionInUrl = true;
          });

        Configure<AbpAspNetCoreMvcOptions>(options =>
        {
            options.ChangeControllerModelApiExplorerGroupName = false;
        });

        context.Services.AddAbpSwaggerGen(options =>
        {
            options.SwaggerDoc("v1", new OpenApiInfo { Title = "API V1", Version = "v1", Description = "v1版本" });
            options.SwaggerDoc("v2", new OpenApiInfo { Title = "API V2", Version = "v2", Description = "v2版本" });
            options.DocInclusionPredicate((docName, description) =>
            {
                if (!description.TryGetMethodInfo(out MethodInfo method) || method.DeclaringType is null)
                {
                    return false;
                }

                if (docName.Equals("v1") && !string.IsNullOrEmpty(method.DeclaringType.FullName) && method.DeclaringType.FullName.Contains(".v1"))
                {
                    return true;
                }

                if (docName.Equals("v2") && !string.IsNullOrEmpty(method.DeclaringType.FullName) && method.DeclaringType.FullName.Contains(".v2"))
                {
                    return true;
                }

                return false;
            });
            options.CustomSchemaIds(type => type.FullName);
        });

        context.Services.AddHttpClientProxies(typeof(AbpVersioningDemoModule).Assembly);

        Configure<AbpRemoteServiceOptions>(options =>
        {
            options.RemoteServices.Default = new RemoteServiceConfiguration("/");
        });
    }

3.OnApplicationInitialization

public override void OnApplicationInitialization(ApplicationInitializationContext context)
    {
        var app = context.GetApplicationBuilder();
        app.UseStaticFiles();
        app.UseRouting();

        app.UseSwagger();
        app.UseAbpSwaggerUI(c =>
        {
            var provider = app.ApplicationServices.GetRequiredService<IApiVersionDescriptionProvider>();
            // build a swagger endpoint for each discovered API version
            foreach (var description in provider.ApiVersionDescriptions)
            {
                c.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
            }
        });

        app.UseConfiguredEndpoints();
    }

4.源码

ABP VNext版本控制

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值