question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ArgumentException on Mono

See original GitHub issue

We have some components that target .NET Framework 4.6 when built, but run on Linux via Mono. In one of those components, we use the Azure KeyVault client, which uses the Microsoft.Rest.ServiceClient class from here.

We had one exception with ADAL, which we have a workaround for and they are fixing for the next version.

However we also have another exception, only when running on Linux:

Unhandled Exception:
System.ArgumentException: Value does not fall within the expected range.
  at System.Net.Http.Headers.Parser+Token.Check (System.String s) [0x00019] in <f9ac0c719f3449a0aa7ac0136a1ad250>:0
  at System.Net.Http.Headers.ProductHeaderValue..ctor (System.String name, System.String version) [0x0000a] in <f9ac0c719f3449a0aa7ac0136a1ad250>:0
  at System.Net.Http.Headers.ProductInfoHeaderValue..ctor (System.String productName, System.String productVersion) [0x00006] in <f9ac0c719f3449a0aa7ac0136a1ad250>:0
  at Microsoft.Rest.ServiceClient`1[T].get_DefaultUserAgentInfoList () [0x0003f] in <7d943830d9bc44fab4bd01d7a9130a9c>:0
  at Microsoft.Rest.ServiceClient`1[T].SetUserAgent (System.String productName, System.String version) [0x00010] in <7d943830d9bc44fab4bd01d7a9130a9c>: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 <7d943830d9bc44fab4bd01d7a9130a9c>:0
  at Microsoft.Rest.ServiceClient`1[T].InitializeHttpClient (System.Net.Http.HttpClientHandler httpClientHandler, System.Net.Http.DelegatingHandler[] handlers) [0x00000] in <7d943830d9bc44fab4bd01d7a9130a9c>:0
  at Microsoft.Rest.ServiceClient`1[T]..ctor (System.Net.Http.HttpClientHandler rootHandler, System.Net.Http.DelegatingHandler[] handlers) [0x00006] in <7d943830d9bc44fab4bd01d7a9130a9c>:0
  at Microsoft.Rest.ServiceClient`1[T]..ctor (System.Net.Http.DelegatingHandler[] handlers) [0x00006] in <7d943830d9bc44fab4bd01d7a9130a9c>:0
  at Microsoft.Azure.KeyVault.KeyVaultClient..ctor (System.Net.Http.DelegatingHandler[] handlers) [0x00000] in <b66b7a9568464c98a370d795acdef11c>:0
  at Microsoft.Azure.KeyVault.KeyVaultClient..ctor (Microsoft.Rest.ServiceClientCredentials credentials, System.Net.Http.DelegatingHandler[] handlers) [0x00000] in <b66b7a9568464c98a370d795acdef11c>:0
  at Microsoft.Azure.KeyVault.KeyVaultClient..ctor (Microsoft.Azure.KeyVault.KeyVaultClient+AuthenticationCallback authenticationCallback, System.Net.Http.DelegatingHandler[] handlers) [0x00007] in <b66b7a9568464c98a370d795acdef11c>:0
  at KeyVaultSecrets.KeyVaultHelper.RetrieveSecret (System.String secretKey) [0x00081] in <23498eee002143eda79164cb7d001b9d>:0
  at KeyVaultSecrets.Program.Main (System.String[] args) [0x0001c] in <23498eee002143eda79164cb7d001b9d>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentException: Value does not fall within the expected range.
  at System.Net.Http.Headers.Parser+Token.Check (System.String s) [0x00019] in <f9ac0c719f3449a0aa7ac0136a1ad250>:0
  at System.Net.Http.Headers.ProductHeaderValue..ctor (System.String name, System.String version) [0x0000a] in <f9ac0c719f3449a0aa7ac0136a1ad250>:0
  at System.Net.Http.Headers.ProductInfoHeaderValue..ctor (System.String productName, System.String productVersion) [0x00006] in <f9ac0c719f3449a0aa7ac0136a1ad250>:0
  at Microsoft.Rest.ServiceClient`1[T].get_DefaultUserAgentInfoList () [0x0003f] in <7d943830d9bc44fab4bd01d7a9130a9c>:0
  at Microsoft.Rest.ServiceClient`1[T].SetUserAgent (System.String productName, System.String version) [0x00010] in <7d943830d9bc44fab4bd01d7a9130a9c>: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 <7d943830d9bc44fab4bd01d7a9130a9c>:0
  at Microsoft.Rest.ServiceClient`1[T].InitializeHttpClient (System.Net.Http.HttpClientHandler httpClientHandler, System.Net.Http.DelegatingHandler[] handlers) [0x00000] in <7d943830d9bc44fab4bd01d7a9130a9c>:0
  at Microsoft.Rest.ServiceClient`1[T]..ctor (System.Net.Http.HttpClientHandler rootHandler, System.Net.Http.DelegatingHandler[] handlers) [0x00006] in <7d943830d9bc44fab4bd01d7a9130a9c>:0
  at Microsoft.Rest.ServiceClient`1[T]..ctor (System.Net.Http.DelegatingHandler[] handlers) [0x00006] in <7d943830d9bc44fab4bd01d7a9130a9c>:0
  at Microsoft.Azure.KeyVault.KeyVaultClient..ctor (System.Net.Http.DelegatingHandler[] handlers) [0x00000] in <b66b7a9568464c98a370d795acdef11c>:0
  at Microsoft.Azure.KeyVault.KeyVaultClient..ctor (Microsoft.Rest.ServiceClientCredentials credentials, System.Net.Http.DelegatingHandler[] handlers) [0x00000] in <b66b7a9568464c98a370d795acdef11c>:0
  at Microsoft.Azure.KeyVault.KeyVaultClient..ctor (Microsoft.Azure.KeyVault.KeyVaultClient+AuthenticationCallback authenticationCallback, System.Net.Http.DelegatingHandler[] handlers) [0x00007] in <b66b7a9568464c98a370d795acdef11c>:0

I’ve traced the problem to this code, which assumes that its safe to get the OS Name and Version from the registry if compiled with FullNetFx. However, that directive only tells you that the compilation target was the full framework. It doesn’t tell you that the runtime environment is Windows. It’s not safe to call Win32 functions from there, which the OsName and OsVersion properties do here.

I’ve got a temporary workaround, which is to create fake entries in Mono’s emulated registry that return something for these values, but the real fix would be to not rely on registry at all, but rather use the information in System.Environment.OSVersion for the full framework, or from System.Runtime.InteropServices.RuntimeInformation if you were to target .NET Standard.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
larsekmancommented, Aug 17, 2017

Never mind, I figured it out! If anyone else is having this problem, you can add the fake registry keys by doing something along these lines:

Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion").SetValue("ProductName", "Windows 8.1 Pro")

1reaction
0xcedcommented, May 27, 2018

I also stumbled on this problem when creating a ServiceBusManagementClient instance (from the Microsoft.Azure.Management.ServiceBus package).

Here is what I did to workaround the problem on macOS.

  1. Create the fake registry directory structure:
$ cd /Library/Frameworks/Mono.framework/Versions/Current/etc/mono/registry/LocalMachine
$ sudo mkdir -p "SOFTWARE/Microsoft/Windows NT/CurrentVersion"
$ cd "SOFTWARE/Microsoft/Windows NT/CurrentVersion"
  1. Check my actual system values:
$ cat /System/Library/CoreServices/SystemVersion.plist 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>ProductBuildVersion</key>
	<string>16G1314</string>
	<key>ProductCopyright</key>
	<string>1983-2018 Apple Inc.</string>
	<key>ProductName</key>
	<string>Mac OS X</string>
	<key>ProductUserVisibleVersion</key>
	<string>10.12.6</string>
	<key>ProductVersion</key>
	<string>10.12.6</string>
</dict>
</plist>

Create a values.xml file in the previously created directory structure, i.e./Library/Frameworks/Mono.framework/Versions/Current/etc/mono/registry/LocalMachine/SOFTWARE/Microsoft/Windows NT/CurrentVersion:

<values>
<value name="ProductName" type="string">Mac OS X</value>
<value name="CurrentVersion" type="string">10.12.6</value>
<value name="CurrentBuild" type="string">16G1314</value>
</values>

After saving this file, I was able to successfully create a ServiceBusManagementClient.

Read more comments on GitHub >

github_iconTop Results From Across the Web

System.ArgumentOutOfRangeException Class
ArgumentOutOfRangeException is thrown when a method is invoked and at least one of the arguments passed to the method is not null and...
Read more >
Mono doesn't throw ArgumentException when creating a ...
Mono doesn't throw ArgumentException when creating a deleagate for a method from an open constructed type #19207.
Read more >
ArgumentException in Mono.Cecil while saving assembly
I remove 'ret' opcode from first code and then merge them into one function. When I try to save it, I get this:...
Read more >
GetComponent requires that the requested component ...
ArgumentException : GetComponent requires that the requested component 'GameObject' derives from Mono. Discussion in 'Scripting' started by ...
Read more >
Gendarme Rules: Exceptions
This rule checks that every System.ArgumentException, System.ArgumentNullException, System.ArgumentOutOfRangeException, or System.DuplicateWaitObjectException ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found