xianhc / apevolo-api

.Net 8 、SqlSugar ORM、Vue 2.X、RBAC、前后端分离的开箱则用的企业级中后台权限管理系统
https://www.apevolo.com
Apache License 2.0
809 stars 74 forks source link

GetManifestResourceStream的返回值有可能为null,UseSwaggerMiddleware的参数Stream需要加上? #5

Closed huster-songtao closed 2 years ago

huster-songtao commented 2 years ago
//Swagger UI
        app.UseSwaggerMiddleware(() =>
            GetType().GetTypeInfo().Assembly.GetManifestResourceStream("ApeVolo.Api.index.html"));

GetManifestResourceStream的返回值有可能为null

UseSwaggerMiddleware 的定义 需要修改为 public static void UseSwaggerMiddleware(this IApplicationBuilder app, Func<Stream?> streamHtml)

//
        // 摘要:
        //     Loads the specified manifest resource from this assembly.
        //
        // 参数:
        //   name:
        //     The case-sensitive name of the manifest resource being requested.
        //
        // 返回结果:
        //     The manifest resource; or null if no resources were specified during compilation
        //     or if the resource is not visible to the caller.
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     The name parameter is null.
        //
        //   T:System.ArgumentException:
        //     The name parameter is an empty string ("").
        //
        //   T:System.IO.FileLoadException:
        //     A file that was found could not be loaded. Note: In .NET for Windows Store apps
        //     or the Portable Class Library, catch the base class exception, System.IO.IOException,
        //     instead.
        //
        //   T:System.IO.FileNotFoundException:
        //     name was not found.
        //
        //   T:System.BadImageFormatException:
        //     name is not a valid assembly.
        //
        //   T:System.NotImplementedException:
        //     Resource length is greater than System.Int64.MaxValue.
        public virtual Stream? GetManifestResourceStream(string name);
huster-songtao commented 2 years ago
app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "1.0.0");

            var stream = streamHtml.Invoke();
            if (stream == null)
            {
                const string msg = "index.html属性错误";
                Log.Error(msg);
                throw new Exception(msg);
            }

            c.IndexStream = () => stream;
            c.RoutePrefix = string.Empty;
        });

streamHtml.Invoke 这样不会调用两次

xianhc commented 2 years ago

@huster-songtao 感谢你的提议,代码已优化