diff --git a/Test/DurableTask.AzureStorage.Tests/AssemblyFileVersionTests.cs b/Test/DurableTask.AzureStorage.Tests/AssemblyFileVersionTests.cs new file mode 100644 index 000000000..ad380f08e --- /dev/null +++ b/Test/DurableTask.AzureStorage.Tests/AssemblyFileVersionTests.cs @@ -0,0 +1,48 @@ +// ---------------------------------------------------------------------------------- +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +namespace DurableTask.AzureStorage.Tests +{ + using System.Diagnostics; + using System.Reflection; + using System.Reflection.Emit; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class AssemblyFileVersionTests + { + [TestMethod] + public void GetAssemblyFileVersion_UsesFileVersionInfoWhenLocationIsKnown() + { + Assembly assembly = typeof(AssemblyFileVersionTests).Assembly; + string expected = FileVersionInfo.GetVersionInfo(assembly.Location).FileVersion; + + Assert.AreEqual(expected, DurableTask.AzureStorage.Utils.GetAssemblyFileVersion(assembly)); + } + +#if !NETFRAMEWORK + [TestMethod] + public void GetAssemblyFileVersion_FallsBackToAttributeWhenLocationIsEmpty() + { + // A dynamic assembly has an empty Location, like an assembly inside a single-file bundle. + var name = new AssemblyName("AssemblyFileVersionTestAssembly"); + AssemblyBuilder assembly = AssemblyBuilder.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run); + ConstructorInfo ctor = typeof(AssemblyFileVersionAttribute).GetConstructor(new[] { typeof(string) }); + assembly.SetCustomAttribute(new CustomAttributeBuilder(ctor, new object[] { "1.2.3.4" })); + + Assert.AreEqual(string.Empty, assembly.Location); + Assert.AreEqual("1.2.3.4", DurableTask.AzureStorage.Utils.GetAssemblyFileVersion(assembly)); + } +#endif + } +} diff --git a/Test/DurableTask.Core.Tests/AssemblyFileVersionTests.cs b/Test/DurableTask.Core.Tests/AssemblyFileVersionTests.cs new file mode 100644 index 000000000..6e9e6d460 --- /dev/null +++ b/Test/DurableTask.Core.Tests/AssemblyFileVersionTests.cs @@ -0,0 +1,48 @@ +// ---------------------------------------------------------------------------------- +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +namespace DurableTask.Core.Tests +{ + using System.Diagnostics; + using System.Reflection; + using System.Reflection.Emit; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class AssemblyFileVersionTests + { + [TestMethod] + public void GetAssemblyFileVersion_UsesFileVersionInfoWhenLocationIsKnown() + { + Assembly assembly = typeof(AssemblyFileVersionTests).Assembly; + string expected = FileVersionInfo.GetVersionInfo(assembly.Location).FileVersion; + + Assert.AreEqual(expected, DurableTask.Core.Common.Utils.GetAssemblyFileVersion(assembly)); + } + +#if !NETFRAMEWORK + [TestMethod] + public void GetAssemblyFileVersion_FallsBackToAttributeWhenLocationIsEmpty() + { + // A dynamic assembly has an empty Location, like an assembly inside a single-file bundle. + var name = new AssemblyName("AssemblyFileVersionTestAssembly"); + AssemblyBuilder assembly = AssemblyBuilder.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run); + ConstructorInfo ctor = typeof(AssemblyFileVersionAttribute).GetConstructor(new[] { typeof(string) }); + assembly.SetCustomAttribute(new CustomAttributeBuilder(ctor, new object[] { "1.2.3.4" })); + + Assert.AreEqual(string.Empty, assembly.Location); + Assert.AreEqual("1.2.3.4", DurableTask.Core.Common.Utils.GetAssemblyFileVersion(assembly)); + } +#endif + } +} diff --git a/src/DurableTask.AzureStorage/Utils.cs b/src/DurableTask.AzureStorage/Utils.cs index 3e9ca4524..2353f6d41 100644 --- a/src/DurableTask.AzureStorage/Utils.cs +++ b/src/DurableTask.AzureStorage/Utils.cs @@ -18,6 +18,7 @@ namespace DurableTask.AzureStorage using System.Diagnostics; using System.IO; using System.Linq; + using System.Reflection; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -29,7 +30,23 @@ static class Utils { public static readonly Task CompletedTask = Task.FromResult(0); - public static readonly string ExtensionVersion = FileVersionInfo.GetVersionInfo(typeof(AzureStorageOrchestrationService).Assembly.Location).FileVersion; + public static readonly string ExtensionVersion = GetAssemblyFileVersion(typeof(AzureStorageOrchestrationService).Assembly); + + /// + /// Gets the file version of an assembly. Assemblies in a single-file bundle have an empty + /// , so the version is read from the assembly's + /// instead. + /// + internal static string GetAssemblyFileVersion(Assembly assembly) + { + string location = assembly.Location; + if (!string.IsNullOrEmpty(location)) + { + return FileVersionInfo.GetVersionInfo(location).FileVersion; + } + + return assembly.GetCustomAttribute()?.Version ?? string.Empty; + } // DurableTask.Core has a public static variable that contains the app name public static readonly string AppName = DurableTask.Core.Common.Utils.AppName; diff --git a/src/DurableTask.Core/Common/Utils.cs b/src/DurableTask.Core/Common/Utils.cs index ef3927b8e..7b6dddd3f 100644 --- a/src/DurableTask.Core/Common/Utils.cs +++ b/src/DurableTask.Core/Common/Utils.cs @@ -49,7 +49,23 @@ public static class Utils /// /// Gets the version of the DurableTask.Core nuget package, which by convension is the same as the assembly file version. /// - internal static readonly string PackageVersion = FileVersionInfo.GetVersionInfo(typeof(TaskOrchestration).Assembly.Location).FileVersion; + internal static readonly string PackageVersion = GetAssemblyFileVersion(typeof(TaskOrchestration).Assembly); + + /// + /// Gets the file version of an assembly. Assemblies in a single-file bundle have an empty + /// , so the version is read from the assembly's + /// instead. + /// + internal static string GetAssemblyFileVersion(Assembly assembly) + { + string location = assembly.Location; + if (!string.IsNullOrEmpty(location)) + { + return FileVersionInfo.GetVersionInfo(location).FileVersion; + } + + return assembly.GetCustomAttribute()?.Version ?? string.Empty; + } private static readonly JsonSerializerSettings ObjectJsonSettings = new JsonSerializerSettings {