Skip to content

Commit

Permalink
fix(src/knifehub.web/utils/dotnetutil.cs): system.ArgumentException: …
Browse files Browse the repository at this point in the history
…The path is empty

at KnifeHub.Web.Utils.DotNetUtil.get_AspNetCoreFileVersion()

fix #32
  • Loading branch information
yiyungent committed Feb 3, 2025
1 parent cf25999 commit 749f1bf
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/KnifeHub.Web/KnifeHub.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
</PropertyGroup>

<PropertyGroup>
<Version>1.6.5</Version>
<FileVersion>1.6.5.0</FileVersion>
<AssemblyVersion>1.6.5.0</AssemblyVersion>
<Version>1.6.6</Version>
<FileVersion>1.6.6.0</FileVersion>
<AssemblyVersion>1.6.6.0</AssemblyVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
51 changes: 47 additions & 4 deletions src/KnifeHub.Web/Utils/DotNetUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Reflection;

namespace KnifeHub.Web.Utils
{
public class DotNetUtil
Expand All @@ -21,9 +23,30 @@ public static string? AspNetCoreFileVersion
{
get
{
string? aspNetCoreVersion = null;
var assembly = typeof(Microsoft.AspNetCore.Http.HttpContext).Assembly;
var fileVersionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location);
string? aspNetCoreVersion = fileVersionInfo.FileVersion;
if (string.IsNullOrEmpty(assembly.Location))
{
// 当程序集是动态加载的或者是从内存中加载的(比如在ASP.NET Core应用中,某些程序集可能被嵌入到主程序集中,或者通过AssemblyLoadContext动态加载),此时Location属性可能为空。
// 使用 AssemblyFileVersionAttribute 获取文件版本
try
{
var fileVersionAttr = assembly.GetCustomAttribute<System.Reflection.AssemblyFileVersionAttribute>();
aspNetCoreVersion = fileVersionAttr?.Version;
}
catch (Exception ex)

Check warning on line 37 in src/KnifeHub.Web/Utils/DotNetUtil.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'ex' is declared but never used
{ }
}
else
{
try
{
var fileVersionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location);
aspNetCoreVersion = fileVersionInfo.FileVersion;
}
catch (Exception ex)

Check warning on line 47 in src/KnifeHub.Web/Utils/DotNetUtil.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'ex' is declared but never used
{ }
}

return aspNetCoreVersion;
}
Expand All @@ -33,9 +56,29 @@ public static string? AspNetCoreProductVersion
{
get
{
string? aspNetCoreVersion = null;
var assembly = typeof(Microsoft.AspNetCore.Http.HttpContext).Assembly;
var fileVersionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location);
string? aspNetCoreVersion = fileVersionInfo.ProductVersion;
if (string.IsNullOrEmpty(assembly.Location))
{
try
{
var versionAttr1 = assembly.GetCustomAttribute<System.Reflection.AssemblyVersionAttribute>();
var versionAttr2 = assembly.GetCustomAttribute<System.Reflection.AssemblyInformationalVersionAttribute>();
aspNetCoreVersion = versionAttr1?.Version ?? "null" + "+" + versionAttr2?.InformationalVersion ?? "null";
}
catch (Exception ex)

Check warning on line 69 in src/KnifeHub.Web/Utils/DotNetUtil.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'ex' is declared but never used
{ }
}
else
{
try
{
var fileVersionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location);
aspNetCoreVersion = fileVersionInfo.ProductVersion;
}
catch (Exception ex)

Check warning on line 79 in src/KnifeHub.Web/Utils/DotNetUtil.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'ex' is declared but never used
{ }
}

return aspNetCoreVersion;
}
Expand Down
2 changes: 1 addition & 1 deletion src/KnifeHub.Web/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<meta charset="utf-8" />
<title>KnifeHub-v1.6.5</title>
<title>KnifeHub-v1.6.6</title>
<!-- Matomo Tag Manager -->
<script>
var _mtm = window._mtm = window._mtm || [];
Expand Down

0 comments on commit 749f1bf

Please sign in to comment.