dotnetcore / BootstrapBlazor

A set of enterprise-class UI components based on Bootstrap and Blazor
https://www.blazor.zone
Apache License 2.0
2.45k stars 294 forks source link

bug: 单文件发布出现错误 publish to single file get version incorrect #1328

Closed densen2014 closed 1 year ago

densen2014 commented 1 year ago

Is there an existing issue for this?

Describe the bug

发布后进入界面提示错误

System.ArgumentException: The path is empty. (Parameter 'path')
   at System.IO.Path.GetFullPath(String path)
   at System.Diagnostics.FileVersionInfo.GetVersionInfo(String fileName)
   at BootstrapBlazor.Components.DefaultJSVersionService.<GetVersion>g__GetVersionImpl|4_0()
   at BootstrapBlazor.Components.DefaultJSVersionService.GetVersion()
   at BootstrapBlazor.Components.BootstrapModuleComponentBase.OnAfterRenderAsync(Boolean firstRender)

image

Expected Behavior

System.ArgumentException: The path is empty. (Parameter 'path') at System.IO.Path.GetFullPath(String path) at System.Diagnostics.FileVersionInfo.GetVersionInfo(String fileName) at BootstrapBlazor.Components.DefaultJSVersionService.g__GetVersionImpl|4_0() at BootstrapBlazor.Components.DefaultJSVersionService.GetVersion() at BootstrapBlazor.Components.BootstrapModuleComponentBase.OnAfterRenderAsync(Boolean firstRender)

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

7.0

Anything else?

bb-auto[bot] commented 1 year ago

@densen2014 Thank you for filing this issue. In order for us to investigate this issue, please provide a minimalistic repro project that illustrates the problem.

densen2014 commented 1 year ago

理论上下面代码可以用

        string GetVersionImpl()
        {
            string? ver = null;
            try
            {
                if (OperatingSystem.IsBrowser())
                {
                    ver = typeof(BootstrapComponentBase).Assembly.GetName().Version?.ToString();
                }
                else
                {
                    if (string.IsNullOrEmpty(typeof(BootstrapComponentBase).Assembly.Location))
                    {
                        // 发布单文件
                        ver = typeof(BootstrapComponentBase).Assembly.GetName().Version?.ToString();
                    }
                    else
                    {
                        ver = FileVersionInfo.GetVersionInfo(typeof(BootstrapComponentBase).Assembly.Location).ProductVersion;
                    }
                }
            }
            catch { }
            return ver ?? "7.0.0.0";
        }
    }

单文件发布后截图

发布时选择单文件时, typeof(BootstrapComponentBase).Assembly.Location 为空, 需要回落到 typeof(BootstrapComponentBase).Assembly.GetName().Version 读取版本信息, 但是读不到beta字眼还是啥原因,我这里显示有两份版本资源. 但是我自己测试控制台程序也是一样.

testSingleFIle.zip

image

image

debug模式截图

image

我本地发布bb测试的, 坐等老张审查

densen2014 commented 1 year ago

warning IL3000: 'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.

参考资料 https://github.com/dotnet/machinelearning/issues/5987

考虑曲线救国? wwwroot文件夹写入版本代号文件名 v7.xx

正常单文件发布读取版本号可直接读exe版本号. bb库就不行了

    if (string.IsNullOrEmpty(typeof(Program).Assembly.Location))
            {
                Console.WriteLine("is singlefile");

                Console.WriteLine(System.AppContext.BaseDirectory);
                Console.WriteLine(FileVersionInfo.GetVersionInfo(Path.Combine(System.AppContext.BaseDirectory, "testSingleFIle.exe"))?.ProductVersion);
            }
            else
            {
                Console.WriteLine(typeof(Program).Assembly.Location);
                Console.WriteLine(FileVersionInfo.GetVersionInfo(typeof(Program).Assembly.Location)?.ProductVersion);

            }
ArgoZhang commented 1 year ago

@densen2014

最新代码如下 Version ??= ConfigVersion ?? GetVersionImpl();

这里优先走 ConfigVersion 这个是针对单文件做配置用的

如果需要发布单文件,就做如下配置

SSR 模式

"BootstrapBlazorOptions": {
  "JSModuleVersion": "7.6.7"
}

WASM 模式

builder.Services.AddBootstrapBlazorServices(options =>
{
    options.JSModuleVersion = "7.6.7";
});
densen2014 commented 1 year ago

@densen2014

最新代码如下 Version ??= ConfigVersion ?? GetVersionImpl();

这里优先走 ConfigVersion 这个是针对单文件做配置用的

如果需要发布单文件,就做如下配置

SSR 模式

"BootstrapBlazorOptions": {
  "JSModuleVersion": "7.6.7"
}

WASM 模式

builder.Services.AddBootstrapBlazorServices(options =>
{
    options.JSModuleVersion = "7.6.7";
});

写配置文件我觉得不够优雅. 我先完整测试一下

densen2014 commented 1 year ago

image

选release发布包本地测试, 结果符合预期!

densen2014 commented 1 year ago

bb7.7.0


我自己测试的blazor winform 发布net7独立无赖框架单文件+ReadyRun 347M你敢信? :-< net7独立无赖框架单文件 241M