/// <summary>
        /// 添加Swagger服务,用于生成api说明页
        /// swagger必须同时添加服务和中间件,配置swaggerdoc到appsetting中,并且在项目属性的生成中勾选xml文档文件,然后在controller或action上添加特性(ApiMethodGroup\ApiClassGroup)
        /// </summary>
        /// <param name="services"></param>
        /// <param name="action"></param>
        public static void AddSwagger(this IServiceCollection services, Action <OptionsSwagger> action)
        {
            OptionsSwagger options = new OptionsSwagger();

            action.Invoke(options);
            ServiceSwagger srv = new ServiceSwagger(services, options);

            srv.AddSwaggerDocs();
        }
 /// <summary>
 /// 使用Swagger显示api接口文档,路由/swagger
 /// </summary>
 /// <param name="app"></param>
 /// <returns></returns>
 public static void UseSwagger(this IApplicationBuilder app, OptionsSwagger options)
 {
     if (options == null || options.SwaggerDocs.Count == 0)
     {
         return;
     }
     //启用swagger,并设每个服务文档的路由,documentName为服务名
     app.UseSwagger(c => { c.RouteTemplate = "swagger/{documentName}/swagger.json"; });
     app.UseSwaggerUI(c =>
     {
         //设置不同服务的终结点
         foreach (var item in options.Value.SwaggerDocs)
         {
             c.SwaggerEndpoint($"/swagger/{item.Key}/swagger.json", item.Value.Title);
         }
         //swagger路由的前缀,这里通过/swagger 访问
         c.RoutePrefix = "swagger";
     });
 }
Beispiel #3
0
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLifeTime)
        {
            //if (env.IsDevelopment())
            app.UseDeveloperExceptionPage();
            //else
            //    //使用全局异常捕获,并在error页面呈现
            //    app.UseExceptionHandler("/message/error");

            //使访问支持静态内容返回,不加这句话图片等静态资源都返回不了
            app.UseStaticFiles();
            //启用上传下载删除文件功能
            app.UseUpload();
            app.UseDownload();
            app.UseRemoveFile();
            //启用心跳包功能
            app.UseHealth();
            //把appsetting的配置加载到context.items["AppSetting"]中
            app.UseAppSetting();


            //var log=app.ApplicationServices.GetRequiredService<ILoggerFactory>().AddConsole().CreateLogger("aa");
            app.UseMvc(routes => routes.MapRoute(
                           name: "default",
                           template: "{controller=Home}/{action=Index}/{id?}"));

            //启用swagger
            var swaggerdoc = new OptionsSwagger();

            Configuration.GetSection("OptionsSwagger").Bind(swaggerdoc);
            app.UseSwagger(swaggerdoc);


            //发现服务(consul等)的配置文件
            var serviceInfo = new OptionsServiceInfo();

            Configuration.GetSection("OptionsServiceInfo").Bind(serviceInfo);
            //心跳包的配置文件
            var health = new OptionsHealth();

            Configuration.GetSection("OptionsHealth").Bind(health);
            //执行服务注册到服务发现中心(consul等)
            app.ExecServiceRegister(appLifeTime, serviceInfo, health);
        }
Beispiel #4
0
 public ServiceSwagger(IServiceCollection _services, OptionsSwagger _options)
 {
     options  = _options;
     services = _services;
 }