autorest: [.NET] AmbiguousMatchException on initializing ServiceClient class
Hi guys.
I have an issue with initializing my service client created by AutoRest and inherited from ServiceClient
.
It throws AmbiguousMatchException
and it arises in ServiceClient
class.
[AmbiguousMatchException: Multiple custom attributes of the same type found.]
System.Attribute.GetCustomAttribute(Assembly element, Type attributeType, Boolean inherit) +122
Microsoft.Rest.ServiceClient`1.get_FrameworkVersion() +103
Microsoft.Rest.ServiceClient`1.SetDefaultUserAgentInfo() +177
Microsoft.Rest.ServiceClient`1.SetUserAgent(String productName, String version) +50
Microsoft.Rest.ServiceClient`1..ctor(DelegatingHandler[] handlers) +61
It only appears on my Remote machine with Windows Server 2008 R2 Standart .NET Framework 4.6.1. Locally on Windows 10 Anniversary 4.6.2 Framework everything is ok.
In Microsoft.Rest.ServiceClient I’ve found next part of code in ClientVersion
property getter:
try
{
AssemblyInformationalVersionAttribute customAttribute1 = assembly.GetCustomAttribute(typeof (AssemblyInformationalVersionAttribute)) as AssemblyInformationalVersionAttribute;
this._clientVersion = customAttribute1 != null ? customAttribute1.InformationalVersion : (string) null;
if (string.IsNullOrEmpty(this._clientVersion))
{
AssemblyFileVersionAttribute customAttribute2 = assembly.GetCustomAttribute(typeof (AssemblyFileVersionAttribute)) as AssemblyFileVersionAttribute;
this._clientVersion = customAttribute2 != null ? customAttribute2.Version : (string) null;
}
}
catch (AmbiguousMatchException ex)
{
}
I think it is source of my problem but I don’t understand why it occurs and to handle it. I see that current exception must be caught but I don’t have any other ideas what else can be wrong.
Guys, any ideas ?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 7
- Comments: 107 (6 by maintainers)
Downgrading Microsoft.Rest.ClientRuntime from 2.3.3 to 2.3.2 has fixed the issue for me.
Adding info from my troubleshooting this problem.
In my case, the “Application Insights Monitor” (https://github.com/Azure/azure-content/blob/master/articles/application-insights/app-insights-monitor-performance-live-website-now.md) was injecting an additional “AssemblyFileVersionAttribute” value into mscorlib.dll. So, the Microsoft.Rest.ClientRuntime error makes sense because when it tries to get the file version using that attribute, it is, in fact, ambiguous.
Does it ever make sense to have more than one “AssemblyFileVersionAttribute” on any file? If not, there’s at least a bug in the tool I just mentioned.
Regardless of the answer to number 1, you’ll see that the code for Microsoft.Rest.ClientRuntime tries to use “AssemblyFileVersionAttribute” more than once. In one instance, it catches and ignores AmbiguousReferenceException and in other instances, it is not. I believe it should be consistent. Either it’s always safe to ignore it, or it’s not.
FYI: I have an Azure Web App that uses Key Vault quite extensively and with updating to the latest App Insights SDK (coming from a v1) this issue popped up. I then upgraded the App Insights Extension to the latest (2.3.2) but no avail. I uninstalled the extension and restarted the app and the app works. I reinstalled the extension, restarted the app again and the app is now still working. So apparently upgrading the Extension does not suffice, it must be removed and reinstalled.
This is happening on all 3 instances of my app. No issues locally on Win10 dev workstations. Following Application Insights related NuGet packages are referenced in the app:
The app targets .NET Framework 4.6.1.
I came here because my KeyVaultKeyResolver threw a System.Reflection.AmbiguousMatchException - Just in case someone’s googling 😃 The solution for me was to throw away the Application Insight Extension from my Azure Web App (no need for the extension - the lib is referenced in code)
@vdevappa I have downgraded Microsoft.Azure.Search from 3.0.1 to 1.1.3 and it solved the problem 😉
BTW, this is the magic we are trying to make with the status monitor if you are interested: http://apmtips.com/blog/2016/11/18/how-application-insights-status-monitor-not-monitors-dependencies/
If you want to keep using application insights - you can disable profiler and only use Application Insights SDK. It will still collect interesting telemetry. Let me know if you need help configuring it
I’m also having this same problem. Working fine on my Windows 10 development workstation but when I deploy to my Server 2012 environment, I get the above exception. I also can’t seem to find the source code for the Microsoft.Rest.ClientRuntime package to try to debug. I can’t really make heads or tails of this issue.