Skip to content

Commit

Permalink
Updated the UserAgent ProductInfoHeaderValue to use the AssemblyInfor…
Browse files Browse the repository at this point in the history
…mationalVersion or the AssemblyFileVersion, if neither attribute is available it will default to the Assembly Version. (Azure#979)
  • Loading branch information
John-Hart authored and amarzavery committed Apr 30, 2016
1 parent b274bbe commit 78ce979
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
3 changes: 3 additions & 0 deletions AutoRest/Generators/CSharp/CSharp.Tests/AcceptanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.5.0.0")]
[assembly: AssemblyInformationalVersion("1.5.0.1")]
54 changes: 41 additions & 13 deletions ClientRuntimes/CSharp/Microsoft.Rest.ClientRuntime/ServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

/// <summary>
/// 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.
/// </summary>
/// <returns>The assembly version of the client.</returns>
private string GetAssemblyVersion()
/// <returns>The version of the client.</returns>
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;
}
}
Expand Down

0 comments on commit 78ce979

Please sign in to comment.