Azure-sdk-for-net: ServiceClient.cs is broken on Visual studio for using mono

Created on 10 Aug 2018  路  21Comments  路  Source: Azure/azure-sdk-for-net

Per the comment here, the CLientRuntime improvements to enable mac on mono still seems to be breaking ServiceClient.cs. This needs an investigation and possibly a fix.

Cost: 3

Mgmt-EngSys

All 21 comments

Thank you. I think the fix may be as simple as skipping the two lines in the DefaultUserAgentInfoList property that reference OSNAME and OSVERSION. Because the exception appears to be parsing the empty string or the single period in the ProductInfoHeaderValue.

@tpaulshippy
before we investigate this further, we would appreciate if you could provide us the following information

  • The mono version on your machine
  • the visual studio version
  • detailed stack trace

@dsgouda Absolutely.

  • mono --version returns "Mono JIT compiler version 5.10.1.57 (2017-12/ea8a24b1bbf Tue Apr 24 14:53:01 EDT 2018)
  • Visual Studio version is 7.5.4 (build 3)
  • Stack trace:
System.ArgumentException: Value does not fall within the expected range.
  at System.Net.Http.Headers.Parser+Token.Check (System.String s) [0x00019] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/System.Net.Http/System.Net.Http.Headers/Parser.cs:57
  at System.Net.Http.Headers.ProductHeaderValue..ctor (System.String name, System.String version) [0x0000a] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/System.Net.Http/System.Net.Http.Headers/ProductHeaderValue.cs:45
  at System.Net.Http.Headers.ProductInfoHeaderValue..ctor (System.String productName, System.String productVersion) [0x00006] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/System.Net.Http/System.Net.Http.Headers/ProductInfoHeaderValue.cs:51
  at Microsoft.Rest.ServiceClient`1[T].get_DefaultUserAgentInfoList () [0x0003f] in <04e89b68c4be40839dfca7a77c2cc25b>:0
  at Microsoft.Rest.ServiceClient`1[T].InitializeHttpClient (System.Net.Http.HttpClient httpClient, System.Net.Http.HttpClientHandler httpClientHandler, System.Net.Http.DelegatingHandler[] handlers) [0x00080] in <04e89b68c4be40839dfca7a77c2cc25b>:0
  at Microsoft.Rest.ServiceClient`1[T]..ctor (System.Net.Http.HttpClient serviceHttpClient, System.Net.Http.HttpClientHandler rootHandler, System.Boolean disposeHttpClient, System.Net.Http.DelegatingHandler[] delHandlers) [0x0000d] in <04e89b68c4be40839dfca7a77c2cc25b>:0
  at Microsoft.Rest.ServiceClient`1[T]..ctor () [0x00007] in <04e89b68c4be40839dfca7a77c2cc25b>:0
  at ServiceClientConsole.MyService..ctor () <0x7a87a0 + 0x0002b> in <56f2c89f9e1348bc926173195a2e118d>:0
  at ServiceClientConsole.MainClass.Main (System.String[] args) [0x00001] in /Users/paulshippy/Projects/temp/ServiceClientConsole/ServiceClientConsole/Program.cs:10

Code sample I used to reproduce:

using System;
using Microsoft.Rest;

namespace ServiceClientConsole
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            MyService svc = new MyService();
        }
    }

    public interface IMyService
    {}
    public class MyService : ServiceClient<MyService>, IMyService
    {

    }
}

Perfect, we will take a look into this. Appreciate the feedback!

@tpaulshippy
Tried repro-ing this issue on a Mac.
This was my program

using System;
using Microsoft.Rest.Azure;
using Microsoft.Rest;
namespace sctest {
 class Program {
  static void Main(string[] args) {

   MyService svc = new MyService();
   Console.WriteLine("Hello World!");
   var ua = svc.UserAgent.GetEnumerator();
   while (ua.MoveNext()) {
    Console.WriteLine(ua.Current.Product.Name);
    Console.WriteLine(ua.Current.Product.Version);
    Console.WriteLine(ua.Current.Comment);
   }
  }

  public interface IMyService {}
  public class MyService: ServiceClient < MyService > , IMyService {

  }
 }
}

which produces the following output

Hello World!

FxVersion

4.6.26606.02



sctest.Program+MyService

1.0.0.0

The mono version installed is

mono --version

Mono JIT compiler version 5.10.1.57 (2017-12/ea8a24b1bbf Tue Apr 24 14:53:01 EDT 2018)

Lastly, Visual Studio version is 15.7
Also, I used the latest ClientRuntime Nuget packages

Microsoft.Rest.ClientRuntime : 2.3.12 
Microsoft.Rest.ClientRuntime.Azure: 3.3.15

I am not able to repro this exact issue. Could you provide some additional information that can help us repro the issue (target framework, nuget package versions you are using, anything that I might have missed in the information above)

Ok. I was able to reproduce your results as well...with a .NET Core Console App.
I am using a .NET Framework 4.7 Console App (which uses mono under the hood, I believe).

Here is my .csproj file:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
    <ProjectGuid>{E95CD936-B0E5-41A0-A108-082B20D9051A}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <RootNamespace>ServiceClientConsole</RootNamespace>
    <AssemblyName>ServiceClientConsole</AssemblyName>
    <TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug</OutputPath>
    <DefineConstants>DEBUG;</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <ExternalConsole>true</ExternalConsole>
    <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
    <Optimize>true</Optimize>
    <OutputPath>bin\Release</OutputPath>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <ExternalConsole>true</ExternalConsole>
    <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="Newtonsoft.Json">
      <HintPath>..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Rest.ClientRuntime">
      <HintPath>..\packages\Microsoft.Rest.ClientRuntime.2.3.12\lib\net452\Microsoft.Rest.ClientRuntime.dll</HintPath>
    </Reference>
    <Reference Include="System.Net" />
    <Reference Include="System.Net.Http" />
    <Reference Include="System.Net.Http.WebRequest" />
    <Reference Include="System.Runtime" />
    <Reference Include="System.Runtime.Serialization" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Program.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <ItemGroup>
    <None Include="packages.config" />
  </ItemGroup>
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

Thanks @tpaulshippy I am now able to repro the issue, will update when we are ready with a fix.

Upon investigation, we are going for a 2-pronged solution.

  • Modifying ClientRuntime to ignore empty values for the ProductInfoHeaderValues for DefaultRequestHeaders, this should cover basic client creation , PR opened here
  • Opened an issue on the mono github repo here since this is unexpected behavior, empty values are perfectly fine when working with the windows implementation of .NET framework

PR is out for the fix. Closing this.

@dsgouda this hasn't been fixed - see the issue referenced by @sighillrob. Can this be reopened? Are there any workarounds? Do we need to downgrade our version?

Before this "fix" was made, we had a successful workaround using fake registry values (from https://github.com/Azure/azure-sdk-for-net/issues/3410 ) but now that doesn't work either.

```at System.Net.Http.Headers.Parser+Token.Check (System.String s) [0x00019] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/System.Net.Http/System.Net.Http.Headers/Parser.cs:57

at System.Net.Http.Headers.ProductHeaderValue..ctor (System.String name, System.String version) [0x0000a] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/System.Net.Http/System.Net.Http.Headers/ProductHeaderValue.cs:45

at System.Net.Http.Headers.ProductInfoHeaderValue..ctor (System.String productName, System.String productVersion) [0x00006] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/System.Net.Http/System.Net.Http.Headers/ProductInfoHeaderValue.cs:51

at Microsoft.Rest.ServiceClient`1[T].get_DefaultUserAgentInfoList () [0x0003f] in <8de96d8c20204f0697e94bce765128e1>:0

at Microsoft.Rest.ServiceClient`1[T].SetUserAgent (System.String productName, System.String version) [0x00010] in <8de96d8c20204f0697e94bce765128e1>:0

at Microsoft.Rest.ServiceClient`1[T].InitializeHttpClient (System.Net.Http.HttpClient httpClient, System.Net.Http.HttpClientHandler httpClientHandler, System.Net.Http.DelegatingHandler[] handlers) [0x00092] in <8de96d8c20204f0697e94bce765128e1>:0

at Microsoft.Rest.ServiceClient`1[T].InitializeHttpClient (System.Net.Http.HttpClientHandler httpClientHandler, System.Net.Http.DelegatingHandler[] handlers) [0x00000] in <8de96d8c20204f0697e94bce765128e1>:0

at Microsoft.Rest.ServiceClient`1[T]..ctor (System.Net.Http.HttpClientHandler rootHandler, System.Net.Http.DelegatingHandler[] handlers) [0x00006] in <8de96d8c20204f0697e94bce765128e1>:0

at Microsoft.Rest.ServiceClient`1[T]..ctor (System.Net.Http.DelegatingHandler[] handlers) [0x00006] in <8de96d8c20204f0697e94bce765128e1>:0

at Microsoft.Azure.KeyVault.KeyVaultClient..ctor (System.Net.Http.DelegatingHandler[] handlers) [0x00000] in :0

at Microsoft.Azure.KeyVault.KeyVaultClient..ctor (Microsoft.Rest.ServiceClientCredentials credentials, System.Net.Http.DelegatingHandler[] handlers) [0x00000] in :0

at Microsoft.Azure.KeyVault.KeyVaultClient..ctor (Microsoft.Azure.KeyVault.KeyVaultClient+AuthenticationCallback authenticationCallback, System.Net.Http.DelegatingHandler[] handlers) [0x00007] in :0```

Experiencing the same validation error, although on a different value -- OSVersion.
MacOS High Sierra v10.13.16

The value of OSVersion: Darwin17.7.0DarwinKernelVersion17.7.0ThuJun21225314PDT2018rootxnu-4570.71.21/RELEASE_X86_64

Stepping through code in debugger, removing the "/" and "." characters allows the client to function.

@dsgouda Customers are still having trouble with this. Please investigate.

@jrodenburg can you provide the version of ClientRuntime you are using.
Are you using the latest version 2.3.17?

@jrodenburg can you describe what is your environment that you are using that made you error on "/" and '."
I was not able to reproduce this issue. What version of Mono are you using?

@shahabhijeet I updated to the latest version of Microsoft.Rest.ClientRuntime, 2.3.17.

Same result:

/usr/local/share/dotnet/dotnet /integration/client-integration/bin/Debug/netcoreapp2.1/client-integration.dll

Unhandled Exception: System.FormatException: The format of value 'Darwin17.7.0DarwinKernelVersion17.7.0ThuJun21225314PDT2018rootxnu-4570.71.21/RELEASE_X86_64' is invalid.
   at System.Net.Http.Headers.HeaderUtilities.CheckValidToken(String value, String parameterName)
   at System.Net.Http.Headers.ProductHeaderValue..ctor(String name, String version)
   at Microsoft.Rest.ServiceClient`1.UpdateDefaultUserAgentList(String headerName, String headerValue)
   at Microsoft.Rest.ServiceClient`1.get_DefaultUserAgentInfoList()
   at Microsoft.Rest.ServiceClient`1.InitializeHttpClient(HttpClient httpClient, HttpClientHandler httpClientHandler, DelegatingHandler[] handlers)
   at IntegrationAPI..ctor(DelegatingHandler[] handlers) in /integration/client-integration/csar/IntegrationAPI.cs:line 56
   at client_integration.Program.Main(String[] args) in /integration/client-integration/Program.cs:line 10

@shahabhijeet you'll have to help me with the request for Mono version. Here's dotnet info:

[ $ ] dotnet --info
.NET Core SDK (reflecting any global.json):
 Version:   2.1.302
 Commit:    9048955601

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.13
 OS Platform: Darwin
 RID:         osx.10.13-x64
 Base Path:   /usr/local/share/dotnet/sdk/2.1.302/

Host (useful for support):
  Version: 2.1.2
  Commit:  811c3ce6c0

.NET Core SDKs installed:
  1.0.0-preview2-003121 [/usr/local/share/dotnet/sdk]
  2.1.301 [/usr/local/share/dotnet/sdk]
  2.1.302 [/usr/local/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 1.0.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.1 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.2 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

@shahabhijeet https://github.com/Azure/azure-sdk-for-net/pull/4669

It appears you've actually seen and commented on this error previously. Fix looks fairly simple too.

In the meantime, is there a workaround to this issue?

I experience the same error, token validation fails. Occurs after adding and configuring Azure Keyvault to a web application.

OS: macOS High Sierra Version 10.13.6

dotnet info

.NET Core SDK (reflecting any global.json):
 Version:   2.1.302
 Commit:    9048955601

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.13
 OS Platform: Darwin
 RID:         osx.10.13-x64
 Base Path:   /usr/local/share/dotnet/sdk/2.1.302/

Host (useful for support):
  Version: 2.1.2
  Commit:  811c3ce6c0

.NET Core SDKs installed:
  1.0.4 [/usr/local/share/dotnet/sdk]
  1.1.4 [/usr/local/share/dotnet/sdk]
  2.0.0 [/usr/local/share/dotnet/sdk]
  2.1.4 [/usr/local/share/dotnet/sdk]
  2.1.101 [/usr/local/share/dotnet/sdk]
  2.1.301 [/usr/local/share/dotnet/sdk]
  2.1.302 [/usr/local/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 1.0.5 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.0.7 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.2 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.4 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.5 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.6 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.1 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.2 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

Exception:

Exception has occurred: CLR/System.FormatException
An unhandled exception of type 'System.FormatException' occurred in System.Net.Http.dll: 'The format of value 'Darwin17.7.0DarwinKernelVersion17.7.0ThuJun21225314PDT2018rootxnu-4570.71.21/RELEASE_X86_64' is invalid.'
   at System.Net.Http.Headers.HeaderUtilities.CheckValidToken(String value, String parameterName)
   at System.Net.Http.Headers.ProductHeaderValue..ctor(String name, String version)
   at Microsoft.Rest.ServiceClient`1.UpdateDefaultUserAgentList(String headerName, String headerValue)
   at Microsoft.Rest.ServiceClient`1.get_DefaultUserAgentInfoList()
   at Microsoft.Rest.ServiceClient`1.InitializeHttpClient(HttpClient httpClient, HttpClientHandler httpClientHandler, DelegatingHandler[] handlers)
   at Microsoft.Azure.KeyVault.KeyVaultClient..ctor(ServiceClientCredentials credentials, DelegatingHandler[] handlers)
   at Microsoft.Extensions.Configuration.AzureKeyVaultConfigurationExtensions.AddAzureKeyVault(IConfigurationBuilder configurationBuilder, String vault, String clientId, String clientSecret, IKeyVaultSecretManager manager)
   at Web.Program.<>c.<CreateWebHostBuilder>b__2_0(WebHostBuilderContext ctx, IConfigurationBuilder builder) in /Users/bennym/Dev/SingleTenant/Web/Program.cs:line 29
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException& hostingStartupErrors)
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()

Installed Packages:

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="2.1.1" />
    <PackageReference Include="Microsoft.Azure.KeyVault" Version="3.0.1" />
    <PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.0.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.4" />
    <PackageReference Include="Microsoft.Extensions.Configuration.AzureKeyVault" Version="2.1.1" />
    <PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="5.3.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.5" />
    <PackageReference Include="WindowsAzure.Storage" Version="9.3.2" />
  </ItemGroup>

Web app config

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((ctx, builder) =>
                {
                    var builtConfig = builder.Build();
                    builder.AddAzureKeyVault(
                        $"https://{builtConfig["KeyVault:Name"]}.vault.azure.net/",
                        builtConfig["AzureAd:ClientId"],
                        builtConfig["AzureAd:ClientSecret"]);
                })
                .UseStartup<Startup>();

@jrodenburg @BennyM Extremely sorry the delay in getting this fix out.
Yes the fix is trivial, but I wanted to make sure I address all scenarios that were reported.
In this issue, @jrodenburg reported that having "." also create exception, so wanted to make sure under what platform he is seeing this issue as I was not able to reproduce it for the "." character on Ubuntu and Mac Sierra.
Will make sure this scenario is unblocked today.
Will update once I release the fix.

@jrodenburg @BennyM New version of ClientRuntime has been published. Version 2.3.18 has been published.
Please open a new issue if you see any issues with ClientRuntime component.

Was this page helpful?
0 / 5 - 0 ratings