diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/AcceptanceTests.cs b/AutoRest/Generators/CSharp/CSharp.Tests/AcceptanceTests.cs index 804db915e6111..bdd10a6f30136 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/AcceptanceTests.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/AcceptanceTests.cs @@ -1366,6 +1366,9 @@ public void HeaderTests() SwaggerPath("header.json"), ExpectedPath("Header")); using (var client = new AutoRestSwaggerBATHeaderService(Fixture.Uri)) { + // Check the UserAgent ProductInfoHeaderValue + Assert.Equal("1.5.0.1", client.UserAgent.Select(c => c.Product.Version.ToString()).FirstOrDefault()); + // POST param/prim/integer client.Header.ParamInteger("positive", 1); client.Header.ParamInteger("negative", -2); diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Properties/AssemblyInfo.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Properties/AssemblyInfo.cs index 68d34505c6d76..61dbf33914dc9 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Properties/AssemblyInfo.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Properties/AssemblyInfo.cs @@ -17,3 +17,4 @@ [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.5.0.0")] +[assembly: AssemblyInformationalVersion("1.5.0.1")] diff --git a/ClientRuntimes/CSharp/Microsoft.Rest.ClientRuntime/ServiceClient.cs b/ClientRuntimes/CSharp/Microsoft.Rest.ClientRuntime/ServiceClient.cs index e80afc887e0f8..6763f4e6641fa 100644 --- a/ClientRuntimes/CSharp/Microsoft.Rest.ClientRuntime/ServiceClient.cs +++ b/ClientRuntimes/CSharp/Microsoft.Rest.ClientRuntime/ServiceClient.cs @@ -208,25 +208,53 @@ protected void InitializeHttpClient(HttpClientHandler httpClientHandler, params HttpClient = newClient; Type type = this.GetType(); HttpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(type.FullName, - GetAssemblyVersion())); + GetClientVersion())); } /// - /// Get the assembly version of a service client. + /// Gets the AssemblyInformationalVersion if available + /// if not it gets the AssemblyFileVerion + /// if neither are available it will default to the Assembly Version of a service client. /// - /// The assembly version of the client. - private string GetAssemblyVersion() + /// The version of the client. + private string GetClientVersion() { + + string version = String.Empty; Type type = this.GetType(); - string version = - type - .GetTypeInfo() - .Assembly - .FullName - .Split(',') - .Select(c => c.Trim()) - .First(c => c.StartsWith("Version=", StringComparison.OrdinalIgnoreCase)) - .Substring("Version=".Length); + Assembly assembly = type.GetTypeInfo().Assembly; + + try + { + // try to get AssemblyInformationalVersion first + AssemblyInformationalVersionAttribute aivAttribute = + assembly.GetCustomAttribute(typeof(AssemblyInformationalVersionAttribute)) as AssemblyInformationalVersionAttribute; + version = aivAttribute?.InformationalVersion; + + // if not available try to get AssemblyFileVersion + if (String.IsNullOrEmpty(version)) + { + AssemblyFileVersionAttribute fvAttribute = + assembly.GetCustomAttribute(typeof(AssemblyFileVersionAttribute)) as AssemblyFileVersionAttribute; + version = fvAttribute?.Version; + } + } + catch (AmbiguousMatchException) + { + // in case there are more then one attribute of the type + } + + // no usable version attribute found so default to Assembly Version + if (String.IsNullOrEmpty(version)) + { + version = + assembly + .FullName + .Split(',') + .Select(c => c.Trim()) + .First(c => c.StartsWith("Version=", StringComparison.OrdinalIgnoreCase)) + .Substring("Version=".Length); + } return version; } }