Azure / autorest

OpenAPI (f.k.a Swagger) Specification code generator. Supports C#, PowerShell, Go, Java, Node.js, TypeScript, Python
MIT License
4.61k stars 737 forks source link

[.NET] AmbiguousMatchException on initializing ServiceClient class #1542

Closed OlehUdovytskyi closed 4 years ago

OlehUdovytskyi commented 8 years ago

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 ?

SergeyKanzhelev commented 7 years ago

@mpbelov I just checked with Cloud Service team. They said they reverted WAD changes due to another issue and deployed it back ~2/13/2017 4:01 PM PCT. I'm really sorry you wasted your time trying to get it fixed when it was reverted back.

SergeyKanzhelev commented 7 years ago

@peters do you know which custom attributes MongoDB driver is complaining about? As far as I know the only attributes was fixed is AssemblyVersion as we needed to do it faster. We will have a version that doesn't mess up with attributes at all later

peters commented 7 years ago

@SergeyKanzhelev AssemblyFileVersionAttribute

SergeyKanzhelev commented 7 years ago

@peters this should be fixed. Thanks!

maxvella commented 7 years ago

The uwp problem does not appear to have been resolved. Will work as expected in Debug mode but then throws an AmbiguousMatchException when compiled using the .Net Native toolchain even when code optimization is turned off. This precludes apps relying on this package from being distributed via the Windows Store. I encountered the problem with Azure Search SDK 3.0.2 which has a dependency on it. Any possible fix in sight?

SergeyKanzhelev commented 7 years ago

@sergiy-k, @morganbr, Azure Search module uses assembly attributes to get the version of the framework. It threw Ambiguios exception in case when we injected code into mscorlib (which we fixed). Now something wrong with .NET Native.

Can you please investigate?

maxvella commented 7 years ago

@SergeyKanzhelev thank you for the prompt reply. This is the stack trace of the AmbiguousMatchException that I'm getting:

   at System.Reflection.CustomAttributeExtensions.OneOrNull[T](IEnumerable`1 results) in f:\dd\ndp\fxcore\src\System.Reflection.Extensions\System\Reflection\CustomAttributeExtensions.cs:line 318
   at System.Reflection.CustomAttributeExtensions.GetCustomAttribute(Assembly element, Type attributeType) in f:\dd\ndp\fxcore\src\System.Reflection.Extensions\System\Reflection\CustomAttributeExtensions.cs:line 67
   at Microsoft.Rest.ServiceClient`1.get_FrameworkVersion()
   at Microsoft.Rest.ServiceClient`1.SetDefaultUserAgentInfo()
   at Microsoft.Rest.ServiceClient`1.SetUserAgent(String productName, String version)
   at Microsoft.Rest.ServiceClient`1.InitializeHttpClient(HttpClientHandler httpClientHandler, DelegatingHandler[] handlers)
   at Microsoft.Rest.ServiceClient`1..ctor(HttpClientHandler rootHandler, DelegatingHandler[] handlers)
   at Microsoft.Rest.ServiceClient`1..ctor(DelegatingHandler[] handlers)
   at Microsoft.Azure.Search.SearchIndexClient..ctor(DelegatingHandler[] handlers)
   at Microsoft.Azure.Search.SearchIndexClient..ctor(String searchServiceName, String indexName, SearchCredentials credentials)
   at XXXX.CloudServices.SearchClient..ctor(String searchServiceName, String searchIndexName, String apiKey) in D:\Programming\XXXX\XXXX.CloudServices\SearchClient.cs:line 32

As I said in my previous post, the exception is only raised when compiling against the .Net Native tool chain. Turning code optimization off makes no difference. When I run against the .Net Framework everything works as expected.

I could only by-pass the issue by downgrading Azure Search to version 1.1.3. That seems to work on my local machine. This seems to be a short-lived solution, however, because the package is also failing WACK certification since "The binary Microsoft.Rest.ClientRuntime.dll is built in debug mode." and that "The binary Microsoft.Rest.ClientRuntime.Azure.dll is built in debug mode."

Not sure whether this is related, but the problems I am experiencing always seem to lead back to the ClientRuntime.dll

SergeyKanzhelev commented 7 years ago

@maxvella can you please run this code to get the list of dups. Maybe it will give an idea of what's going on... Maybe modify it to output the value of the attribute as well:

Console.WriteLine("Number of custom attributes:" + System.Attribute.GetCustomAttributes((typeof(object)).Assembly).Length);

foreach(var a in System.Attribute.GetCustomAttributes((typeof(object)).Assembly))
{
    Console.WriteLine("attribute: " + a.TypeId);
}

I was told that @sergiy-k, @morganbr are the right people for .NET Native questions. This investigation step will definitely help.

brjohnstmsft commented 7 years ago

Not sure whether this is related, but the problems I am experiencing always seem to lead back to the ClientRuntime.dll

@maxvella The FrameworkVersion property that throws the exception is implemented in ClientRuntime. Versions of the Azure Search SDK prior to 3.x depend on an older version of ClientRuntime that doesn't have this property, so that's why downgrading works for you.

morganbr commented 7 years ago

@SergeyKanzhelev , we can probably help with that. Since there's been a lot of iteration in this thread, can you help us understand the current state a bit? In particular:

  1. Which attribute is the SDK looking for?
  2. Which assemblies is it looking on?
  3. What version of Visual Studio are you testing with?
SergeyKanzhelev commented 7 years ago

@morganbr to the best of my knowledge - Azure Search SDK tries to get the framework version by looking at AssemblyFileVersionAttribute of the mscorlib (actually of the ((typeof(object)).Assembly. It throws if there are multiple attributes if this type.

We caused having multiple attributes in cloud services as we injected some code into mscorlib in runtime. I do not know what may cause having multiple attributes of this type in .NET Native.

I do not have a repro with .NET Native. Maybe @maxvella can help with repro and VS version.

brjohnstmsft commented 7 years ago

@SergeyKanzhelev To clarify, it is the ClientRuntime library that is shared by many Azure SDKs that looks at AssemblyFileVersionAttribute. The Azure Search library is but one of many libraries depending on ClientRuntime that could potentially experience this problem.

mms- commented 7 years ago

Same issue on UWP and my own api client. Creating an instance using Microsoft.Rest.ServiceClient(params DelegatingHandler[] handlers) gives the following when using .Net Native Toolchain in Debug or Release:

'TTStation.exe' (Win32): Loaded 'C:\Windows\SysWOW64\version.dll'. Skipped loading symbols. Module is native, and native debugging is currently disabled.
'TTStation.exe' (Win32): Loaded 'C:\Windows\SysWOW64\Windows.Graphics.dll'. Skipped loading symbols. Module is native, and native debugging is currently disabled.
Exception thrown: 'System.Reflection.AmbiguousMatchException' in System.Reflection.Extensions.dll
'TTStation.exe' (Win32): Loaded 'C:\Windows\SysWOW64\xmllite.dll'. Skipped loading symbols. Module is native, and native debugging is currently disabled.
'TTStation.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\wshbth.dll'
'TTStation.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\FWPUCLNT.DLL'
Exception thrown: 'System.Reflection.AmbiguousMatchException' in System.Reflection.Extensions.dll
Exception thrown: 'System.Reflection.AmbiguousMatchException' in System.Private.CoreLib.dll
TTSessionSink.StartValidation Ex: System.Reflection.AmbiguousMatchException: Ambiguous match found.
   at SharedLibrary!<BaseAddress>+0x49a895
   at SharedLibrary!<BaseAddress>+0x8cdcde
   at Microsoft.Rest.ServiceClient<TT.Apis.v2.TTAPIv2>.get_FrameworkVersion()
   at Microsoft.Rest.ServiceClient<TT.Apis.v2.TTAPIv2>.SetDefaultUserAgentInfo()
   at Microsoft.Rest.ServiceClient<TT.Apis.v2.TTAPIv2>.SetUserAgent(String productName, String version)
   at Microsoft.Rest.ServiceClient<TT.Apis.v2.TTAPIv2>.InitializeHttpClient(Net.Http.HttpClientHandler httpClientHandler, Net.Http.DelegatingHandler[] handlers)
   at Microsoft.Rest.ServiceClient<TT.Apis.v2.TTAPIv2>..ctor(Net.Http.HttpClientHandler rootHandler, Net.Http.DelegatingHandler[] handlers)
   at Microsoft.Rest.ServiceClient<TT.Apis.v2.TTAPIv2>..ctor(Net.Http.DelegatingHandler[] handlers)

The code you provided does not compile under UWP, but

System.Diagnostics.Debug.WriteLine("Number of custom attributes:" + typeof(object).GetTypeInfo().Assembly.GetCustomAttributes().Count());

foreach (var a in typeof(object).GetTypeInfo().Assembly.GetCustomAttributes().OrderBy(o => o.GetType().GetTypeInfo().FullName))
{
     System.Diagnostics.Debug.WriteLine("attribute: " + a.GetType().GetTypeInfo().FullName);
}

gives

Number of custom attributes:28
attribute: System.CLSCompliantAttribute
attribute: System.CLSCompliantAttribute
attribute: System.Diagnostics.DebuggableAttribute
attribute: System.Diagnostics.DebuggableAttribute
attribute: System.Reflection.AssemblyCompanyAttribute
attribute: System.Reflection.AssemblyCompanyAttribute
attribute: System.Reflection.AssemblyCopyrightAttribute
attribute: System.Reflection.AssemblyCopyrightAttribute
attribute: System.Reflection.AssemblyDefaultAliasAttribute
attribute: System.Reflection.AssemblyDefaultAliasAttribute
attribute: System.Reflection.AssemblyDescriptionAttribute
attribute: System.Reflection.AssemblyDescriptionAttribute
attribute: System.Reflection.AssemblyFileVersionAttribute
attribute: System.Reflection.AssemblyFileVersionAttribute
attribute: System.Reflection.AssemblyInformationalVersionAttribute
attribute: System.Reflection.AssemblyInformationalVersionAttribute
attribute: System.Reflection.AssemblyMetadataAttribute
attribute: System.Reflection.AssemblyMetadataAttribute
attribute: System.Reflection.AssemblyProductAttribute
attribute: System.Reflection.AssemblyProductAttribute
attribute: System.Reflection.AssemblyTitleAttribute
attribute: System.Reflection.AssemblyTitleAttribute
attribute: System.Runtime.CompilerServices.CompilationRelaxationsAttribute
attribute: System.Runtime.CompilerServices.CompilationRelaxationsAttribute
attribute: System.Runtime.CompilerServices.ExtensionAttribute
attribute: System.Runtime.CompilerServices.ExtensionAttribute
attribute: System.Runtime.CompilerServices.RuntimeCompatibilityAttribute
attribute: System.Runtime.CompilerServices.RuntimeCompatibilityAttribute

Definitely some duplicates, let me know if you want me to run other code. Dumping the properties on the attributes using reflection gives all the same values on the duplicates, i.e.

value: AssemblyTitleAttribute, Title: System.Private.CoreLib
or 
AssemblyProductAttribute Product: Microsoft® .NET Framework

This is on Windows 14393.639 and VC 2017 RC 4+26206. No idea what could be causing this, but .Net Native has way too many issues, this is the third unique one in 3 days.

maxvella commented 7 years ago

My conf is

Microsoft Visual Studio Community 2015 Version 14.0.25431.01 Update 3 Win 14393.639

The problem would seem to start from here:

https://github.com/SyntaxC4-MSFT/AutoRest/blob/master/Microsoft.Rest/ClientRuntime/ServiceClient.cs

    protected void InitializeHttpClient(HttpClientHandler httpClientHandler, params DelegatingHandler[] handlers)

    {

        HttpClientHandler = httpClientHandler;

        DelegatingHandler currentHandler = new RetryDelegatingHandler();

        currentHandler.InnerHandler = HttpClientHandler;

        if (handlers != null)

        {

            for (int i = handlers.Length - 1; i >= 0; --i)

            {

                DelegatingHandler handler = handlers[i];

                // Non-delegating handlers are ignored since we always 

                // have RetryDelegatingHandler as the outer-most handler

                while (handler.InnerHandler is DelegatingHandler)

                {

                    handler = handler.InnerHandler as DelegatingHandler;

                }

                handler.InnerHandler = currentHandler;

                currentHandler = handlers[i];

            }

        }

        var newClient = new HttpClient(currentHandler, false);

        FirstMessageHandler = currentHandler;

        HttpClient = newClient;

        Type type = this.GetType();

        HttpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(type.FullName,

            GetAssemblyVersion()));

    }
SanderMeester commented 7 years ago

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)

SergeyKanzhelev commented 7 years ago

@SanderMeester I'm glad you found a solution quickly. I'm sorry for an inconvenience. Just FYI - simply update to the latest version of extension would have been fixed it. Application Insights extension enables some advanced scenarios and does better data collection. For instance, full SQL query will be collected, not just stored procedure names and cross component correlation over http will work.

morganbr commented 7 years ago

I've confirmed the bug in .NET Native and we're tracking it for a future release. I can think of two workarounds:

  1. In AutoRest, you can use one of the other GetCustomAttribute or AttributeData methods on Assembly that don't take a Type and do your own filtering that doesn't mind duplicates.
  2. For folks trying to get their app to work before 1 can happen, you can add the following to a PropertyGroup in your csproj <UseDotNetNativeSharedAssemblyFrameworkPackage>false</UseDotNetNativeSharedAssemblyFrameworkPackage> This will disable the shared framework feature. It removes the attribute duplication, but may make your compiled app a few MB larger.

/CC @michalstrehovsky

jsgoupil commented 7 years ago

I was facing the same problem on one slot on Azure... Finally figured it out by updating or deleting the Application Insights as mentioned by @SergeyKanzhelev
Then restart the web app.

here: image

SergeyKanzhelev commented 7 years ago

@jsgoupil thanks for update, I hope you found this issue quickly. Sorry for inconvenience.

bordnerm commented 7 years ago

@SergeyKanzhelev You mentioned on Dec 14 (in https://github.com/Microsoft/ApplicationInsights-dotnet-server/issues/241) that an update for the Status Monitor is pending. Do you know the status of that update? What version of the Status Monitor would contain this fix?

We're running into this same issue where we get the System.Reflection.AmbiguousMatchException when trying to use Azure Search; and this error goes away if we uninstall the App Insights Status Monitor.

Thank you!

bordnerm commented 7 years ago

Follow-up to previous post...when I installed the latest App Insights Status Monitor from https://docs.microsoft.com/en-us/azure/application-insights/app-insights-monitor-performance-live-website-now (look for the "Status Monitor installer" link), then I no longer get the AmbiguousMatchException when doing a search using Azure Search.

Note: when looking at Control Panel -> Programs and Features, the version of Status Monitor will be 9.1.0.0.

Thanks!

rbonestell commented 7 years ago

@SergeyKanzhelev FYI, this is also blocking the use of SQL Server Always Encrypted with Azure Key Vault using Microsoft.Rest.ClientRuntime v2.3.7

I removed the app insights status monitor to resolve the problem.

SergeyKanzhelev commented 7 years ago

@rbonestell thanks for letting us know. You can always update to the latest version of the Status Monitor instead of uninstalling. The issue was fixed

rbonestell commented 7 years ago

@SergeyKanzhelev I tried this first, installing the latest from the website then tried from the web platform installer, both resulted in v9.0.0 being installed on my servers and causing exceptions with my Always Encrypted software.

SergeyKanzhelev commented 7 years ago

@rbonestell if it's not too much to ask - do you have a small repro app? Or perhaps you can get the full exception information including the call stack, the name of custom attribute that was ambiguous and versions of all modules loaded into the process?

rbonestell commented 7 years ago

I don't have a public repo or any more details, but the info below lead me to this GitHub issue which helped me resolve the problem by uninstalling the Application Insights agent from all of our servers.

Using these libraries: Microsoft.Rest.ClientRuntime v2.3.7 Microsoft.Azure.KeyVault v2.0.6 Microsoft.SqlServer.Management.AlwaysEncrypted.AzureKeyVaultProvider v2.1.0

Here's the stack trace from my AKV initialization call:

2017-05-06 18:04:32,859 [ERROR] WebApiApplication - Failed to initialize Azure Key Vault provider System.Reflection.AmbiguousMatchException: Multiple custom attributes of the same type found. at System.Attribute.GetCustomAttribute(Assembly element, Type attributeType, Boolean inherit) at Microsoft.Rest.ServiceClient'1.get_FrameworkVersion() at Microsoft.Rest.ServiceClient'1.get_DefaultUserAgentInfoList() at Microsoft.Rest.ServiceClient'1.SetUserAgent(String productName, String version) at Microsoft.Rest.ServiceClient'1.InitializeHttpClient(HttpClient httpClient, HttpClientHandler httpClientHandler, DelegatingHandler[] handlers) at Microsoft.Azure.KeyVault.KeyVaultClient..ctor(ServiceClientCredentials credentials, DelegatingHandler[] handlers) at Microsoft.SqlServer.Management.AlwaysEncrypted.AzureKeyVaultProvider.SqlColumnEncryptionAzureKeyVaultProvider..ctor(AuthenticationCallback authenticationCallback, String[] trustedEndPoints) at Microsoft.SqlServer.Management.AlwaysEncrypted.AzureKeyVaultProvider.SqlColumnEncryptionAzureKeyVaultProvider..ctor(AuthenticationCallback authenticationCallback)

rbonestell commented 7 years ago

For reference, this is how I initialize the SQL AlwaysEncrypted provider for AKV: https://github.com/Microsoft/azure-docs/blob/master/articles/sql-database/sql-database-always-encrypted-azure-key-vault.md#register-the-azure-key-vault-provider

SergeyKanzhelev commented 7 years ago

I appreciate all the additional details. @rbonestell I confirmed it is exactly the same problem. I'm very surprised that the Status Monitor update didn't help. I'll investigate whether update of Status Monitor may have some files kept on the system.

Is it the web application you are hosting this code? IIS based? Have you restarted the IIS after updating of Status Monitor?

rbonestell commented 7 years ago

I restarted each server after upgrading, still experienced the same issue. FWIW they are all Azure VMs.

SergeyKanzhelev commented 7 years ago

I double checked Status Monitor. This bug is definitely fixed in v 9.1.0.0. Direct download link. This version will be installed by platform installer. I'm not sure why it wasn't picked up in your environment.

image

Versions of MicrosoftInstrumentationEngine_x64.dll under C:\Program Files\Microsoft Application Insights\Runtime Instrumentation Agent\x64 should be 0.1.22.16258 or above. Assembly Microsoft.Diagnostics.Instrumentation.Extensions.Base.dll in the same folder should NOT have AssemblyFileVersionAttribute attribute.

vdevappa commented 7 years ago

Is this issue fixed for those unfortunate ones (like me) who use the SDK and cloud services?

SergeyKanzhelev commented 7 years ago

@vdevappa, yes. Windows Azure Diagnostics on cloud services will install the correct version of Status Monitor. Please let me know if you still experience the issue

vdevappa commented 7 years ago

Thanks, that explains why the issues are resolved for me now.

SvenAelterman commented 7 years ago

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.

SergeyKanzhelev commented 7 years ago

@iusafaro why update may behave the way @SvenAelterman reported? Is it a bug or special case?

rrrr-o commented 6 years ago

We are currently seeing this issue in Azure on a site which does use Application Insights. Originally we enabled this in the portal but more recently have bundled it in with our project after the monitoring stopped working.

We experienced the error suddenly without warning when our web app was switched instances in the early hours of the morning yesterday.

I was able to mitigate it by redeploying the same code base to our staging deployment slot and then swapping that with live. That means we do have an environment up on staging that the error still is persisted in if that helps to diagnose anything.

This is initiated by Mongo in our dependency injection code where it attempts to get the framework description.

[AmbiguousMatchException: Multiple custom attributes of the same type found.]
   System.Attribute.GetCustomAttribute(Assembly element, Type attributeType, Boolean inherit) +110
   System.Runtime.InteropServices.RuntimeInformation.get_FrameworkDescription() +87
   MongoDB.Driver.Core.Connections.ClientDocumentHelper.GetPlatformString() +9

I have a support case open with Azure support at the moment.

SergeyKanzhelev commented 6 years ago

@roboffer what version of Application Insights extension do you have installed?

@iusafaro @WilliamXieMSFT I wonder can this be a regression with the recently shipped extension version?

puneetg1983 commented 6 years ago

I am also engaged on issue for Azure WebApps where things are failing for a particular slot only and they are using MongoDb driver (looks like it is @roboffer only )

We collected a memory dump on this exception and I see the following DLL's and versions loaded

Image name: Microsoft.ApplicationInsights.NLogTarget.dll
Product version:  2.4.1.441
Image name: Microsoft.ApplicationInsights.dll
Product version:  2.4.0.32153
Image name: 2.4.0.0.Microsoft.ApplicationInsights.Extensions.Intercept_x86.dll
Product version:  2.4.0.40021
Image name: Microsoft.ApplicationInsights.Extensions.Base_x86.dll
Product version:  2.0.0.205
Image name: Microsoft.ApplicationInsights.ExtensionsHost.dll
Product version:  2.0.0.205

Does this help ?

wiktork commented 6 years ago

@puneetg1983 For the app service scenario, the newest dll's should be Microsoft.InstrumentationEngine.Extensions.Base_x86.dll and Microsoft.InstrumentationEngine.ExtensionsHost_x64. (Note the name changes). Both should be version 1.0.3+. Can you update to the latest version of ApplicationInsights on this slot?

rrrr-o commented 6 years ago

@SergeyKanzhelev AFAIK we are using the latest application insights versions, they are installed through NuGet and it shows the versions that are installed are as follows:

Microsoft.ApplicationInsights v2.4.0 Microsoft.ApplicationInsights.Agent.Intercept v2.4.0 Microsoft.ApplicationInsights.DependencyCollector v2.4.1 Microsoft.ApplicationInsights.NLogTarget v2.4.1 Microsoft.ApplicationInsights.PerfCounterCollector v2.4.1 Microsoft.ApplicationInsights.Web v2.4.1 Microsoft.ApplicationInsights.WindowsServer v2.4.1 Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel v2.4.0

What I did just notice is that our staging environment did have Application Insights installed on it as a Site Extension through Kudu and live didn't (however this problem originally happened on live before swapping the slots to resolve). The installed version was 2.0.0, removing this entirely or updating it to the latest version 2.4.7 made no difference and the problem still persists.

After this update I have found DLLs matching those outlined by @wiktork (Microsoft.InstrumentationEngine.ExtensionsHost_x64 v1.3.3.37395 and Microsoft.InstrumentationEngine.Extensions.Base_x86 v1.3.3.37395) in the SiteExtensions folder. I can't confirm what versions were there before.

Without knowing the full details it seems that some code is asking for attributes from a class that has multiple of the same custom attribute. Is the fix to handle that error in the code that is asking for the attributes or is the fix to remove the additional attribute?

SergeyKanzhelev commented 6 years ago

@roboffer Application Insights consists of two parts. One is SDK, another one is additional code instrumentation that is enabled by site extension. That additional code instrumentation makes possible to improve data collection in runtime. Some details in my blog post.

The problem is that older version of that extension by mistake added extra attributes to assembly metadata. This caused problems with AutoRest library as that library expects certain attributes to be defined exactly once.

Please confirm that the issue got fixed after upgrade.

rrrr-o commented 6 years ago

@SergeyKanzhelev Thanks, that makes sense with what I thought might be happening (changes were being made to a different assembly). Do you know exactly which assembly would have been modified?

We are still seeing the issue after upgrading, but if the assembly that was modified was a system DLL then I'm assuming upgrading won't remove the invalid attribute?

SergeyKanzhelev commented 6 years ago

@roboffer attributes only inserted in runtime in-memory. Not on disk. So upgrade of extension should fix the issue. Have you restarted app after an upgrade of extension?

KingEgonSANCTITY commented 6 years ago

@SergeyKanzhelev I've run into this issue today while deploying to a production web app. In the staging environment I do not get this error, this environment doesn't have AI extension installed. I tried updating the AI web extension but that didn't fix the issue. This does seem to be associated with the mongo driver. Any suggestions are appreciated as this is supposed to be public tomorrow.

[AmbiguousMatchException: Multiple custom attributes of the same type found.]
   System.Attribute.GetCustomAttribute(Assembly element, Type attributeType, Boolean inherit) +119
   System.Runtime.InteropServices.RuntimeInformation.get_FrameworkDescription() +127
   System.Lazy`1.CreateValue() +730
   System.Lazy`1.LazyInitValue() +438
   MongoDB.Driver.Core.Connections.ClientDocumentHelper.CreateClientDocument(String applicationName) +76
   MongoDB.Driver.Core.Connections.BinaryConnectionFactory..ctor(ConnectionSettings settings, IStreamFactory streamFactory, IEventSubscriber eventSubscriber) +176
   MongoDB.Driver.Core.Configuration.ClusterBuilder.BuildCluster() +172
   MongoDB.Driver.ClusterRegistry.CreateCluster(ClusterKey clusterKey) +486
   MongoDB.Driver.ClusterRegistry.GetOrCreateCluster(ClusterKey clusterKey) +113
   MongoDB.Driver.MongoClient..ctor(MongoClientSettings settings) +118
   SitefinityWebApp.Mvc.Models.ContextModels.SurveyContext..cctor() +103

[TypeInitializationException: The type initializer for 'SitefinityWebApp.Mvc.Models.ContextModels.SurveyContext' threw an exception.]
   SitefinityWebApp.Mvc.Models.ContextModels.SurveyContext..ctor() +0
   SitefinityWebApp.Mvc.Controllers.EntreeFavoriteSurveyController..cctor() +44

[TypeInitializationException: The type initializer for 'SitefinityWebApp.Mvc.Controllers.EntreeFavoriteSurveyController' threw an exception.]
   SitefinityWebApp.Mvc.Controllers.EntreeFavoriteSurveyController.GetTotals() +36
   SitefinityWebApp.Mvc.Controllers.EntreeFavoriteSurveyController.Insert(EntreeFavoriteModel survey) +92
   lambda_method(Closure , ControllerBase , Object[] ) +175
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +209
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +35
   System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +80
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +452
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +452
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +452
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +452
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +906
   Telerik.Sitefinity.Mvc.ControllerWrapper.Execute() +217
   Telerik.Sitefinity.Mvc.ControllerActionInvoker.ExecuteController(MvcProxyBase proxyControl) +227
   Telerik.Sitefinity.Mvc.ControllerActionInvoker.TryInvokeAction(MvcProxyBase proxyControl, String& output) +311
   Telerik.Sitefinity.Mvc.Proxy.MvcControllerProxy.ExecuteController() +66
   System.Web.UI.Control.PreRenderRecursiveInternal() +200
   System.Web.UI.Control.PreRenderRecursiveInternal() +297
   System.Web.UI.Control.PreRenderRecursiveInternal() +297
   System.Web.UI.Control.PreRenderRecursiveInternal() +297
   System.Web.UI.Control.PreRenderRecursiveInternal() +297
   System.Web.UI.Control.PreRenderRecursiveInternal() +297
   System.Web.UI.Control.PreRenderRecursiveInternal() +297
   System.Web.UI.Control.PreRenderRecursiveInternal() +297
   System.Web.UI.Control.PreRenderRecursiveInternal() +297
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +7487
SergeyKanzhelev commented 6 years ago

@wiktork can it be that extension didn't clean up older binaries after update?

wiktork commented 6 years ago

@jblackwell-thuzi would you be able to provide the list of files in your D:\home\SiteExtensions\Microsoft.ApplicationInsights.AzureWebSites\Instrumentation32? (Instrumentation64 if this is a 64-bit web app)

KingEgonSANCTITY commented 6 years ago

@wiktork Yes, I can provide both. My app is 64-bit. Thanks let me know if you need anything else.

32-bit

Mode                LastWriteTime         Length Name                          
----                -------------         ------ ----                          
-a----         1/3/2018   4:05 PM          29512 Microsoft.Diagnostics.Instrumentation.Extensions.Base.dll   
-a----         1/3/2018   4:05 PM         554304 Microsoft.InstrumentationEngine.Extensions.Base_x86.dll     
-a----         1/3/2018   4:05 PM            486 Microsoft.InstrumentationEngine.Extensions.config           
-a----         1/3/2018   4:05 PM         259392 Microsoft.InstrumentationEngine.ExtensionsHost_x86.dll      
-a----         1/3/2018   4:05 PM        1105648 MicrosoftInstrumentationEngine_x86.dll                      
-a----         1/3/2018   4:05 PM            448 ProductionBreakpoints_x86.config                            
-a----         1/3/2018   4:05 PM         688848 ProductionBreakpoints_x86.dll 
-a----         1/3/2018   4:05 PM         288952 SnapshotHolder_x86.exe 

64-bit

-a----         1/3/2018   4:05 PM          29512 Microsoft.Diagnostics.Instrumentation.Extensions.Base.dll   
-a----         1/3/2018   4:05 PM         718656 Microsoft.InstrumentationEngine.Extensions.Base_x64.dll     
-a----         1/3/2018   4:05 PM            486 Microsoft.InstrumentationEngine.Extensions.config           
-a----         1/3/2018   4:05 PM         312120 Microsoft.InstrumentationEngine.ExtensionsHost_x64.dll      
-a----         1/3/2018   4:06 PM        1283832 MicrosoftInstrumentationEngine_x64.dll                      
-a----         1/3/2018   4:05 PM            448 ProductionBreakpoints_x64.config                            
-a----         1/3/2018   4:05 PM         790224 ProductionBreakpoints_x64.dll 
-a----         1/3/2018   4:05 PM         341176 SnapshotHolder_x64.exe
puneetg1983 commented 6 years ago

Probably a good idea to run the following command from KUDU -> Debug Console -> Powershell as that will show the file versions easily...

dir *.dll | fl

KingEgonSANCTITY commented 6 years ago

@wiktork The versions for the 64-bit. Thanks @puneetg1983.

Name           : Microsoft.Diagnostics.Instrumentation.Extensions.Base.dll
Length         : 29512
CreationTime   : 1/3/2018 4:06:02 PM
LastWriteTime  : 1/3/2018 4:05:59 PM
LastAccessTime : 1/3/2018 4:06:02 PM
Mode           : -a----
LinkType       : 
Target         : 
VersionInfo    : File:             D:\home\SiteExtensions\Microsoft.Application
                 Insights.AzureWebSites\Instrumentation64\Microsoft.Diagnostics
                 .Instrumentation.Extensions.Base.dll
                 InternalName:     
                 Microsoft.Diagnostics.Instrumentation.Extensions.Base.dll
                 OriginalFilename: 
                 Microsoft.Diagnostics.Instrumentation.Extensions.Base.dll
                 FileVersion:      0.0.0.0
                 FileDescription:   
                 Product:          
                 ProductVersion:   0.0.0.0
                 Debug:            False
                 Patched:          False
                 PreRelease:       False
                 PrivateBuild:     False
                 SpecialBuild:     False
                 Language:         Language Neutral

Name           : Microsoft.InstrumentationEngine.Extensions.Base_x64.dll
Length         : 718656
CreationTime   : 1/3/2018 4:06:02 PM
LastWriteTime  : 1/3/2018 4:05:59 PM
LastAccessTime : 1/3/2018 4:06:02 PM
Mode           : -a----
LinkType       : 
Target         : 
VersionInfo    : File:             D:\home\SiteExtensions\Microsoft.Application
                 Insights.AzureWebSites\Instrumentation64\Microsoft.Instrumenta
                 tionEngine.Extensions.Base_x64.dll
                 InternalName:     InstrumentationEngine.Extensions.Base
                 OriginalFilename: 
                 FileVersion:      1.0.3.37395
                 FileDescription:  
                 Product:          Microsoft® Visual Studio®
                 ProductVersion:   1.0.3.37395
                 Debug:            False
                 Patched:          False
                 PreRelease:       False
                 PrivateBuild:     False
                 SpecialBuild:     False
                 Language:         English (United States)

Name           : Microsoft.InstrumentationEngine.ExtensionsHost_x64.dll
Length         : 312120
CreationTime   : 1/3/2018 4:06:02 PM
LastWriteTime  : 1/3/2018 4:05:59 PM
LastAccessTime : 1/3/2018 4:06:02 PM
Mode           : -a----
LinkType       : 
Target         : 
VersionInfo    : File:             D:\home\SiteExtensions\Microsoft.Application
                 Insights.AzureWebSites\Instrumentation64\Microsoft.Instrumenta
                 tionEngine.ExtensionsHost_x64.dll
                 InternalName:     InstrumentationEngine.ExtensionsHost
                 OriginalFilename: 
                 FileVersion:      1.0.3.37395
                 FileDescription:  
                 Product:          Microsoft® Visual Studio®
                 ProductVersion:   1.0.3.37395
                 Debug:            False
                 Patched:          False
                 PreRelease:       False
                 PrivateBuild:     False
                 SpecialBuild:     False
                 Language:         English (United States)

Name           : MicrosoftInstrumentationEngine_x64.dll
Length         : 1283832
CreationTime   : 1/3/2018 4:06:02 PM
LastWriteTime  : 1/3/2018 4:06:00 PM
LastAccessTime : 1/3/2018 4:06:02 PM
Mode           : -a----
LinkType       : 
Target         : 
VersionInfo    : File:             D:\home\SiteExtensions\Microsoft.Application
                 Insights.AzureWebSites\Instrumentation64\MicrosoftInstrumentat
                 ionEngine_x64.dll
                 InternalName:     MicrosoftInstrumentationEngine_x64
                 OriginalFilename: MicrosoftInstrumentationEngine_x64.dll
                 FileVersion:      1.0.3.37395
                 FileDescription:  Microsoft Instrumentation Engine
                 Product:          Microsoft® Visual Studio®
                 ProductVersion:   1.0.3.37395
                 Debug:            False
                 Patched:          False
                 PreRelease:       False
                 PrivateBuild:     False
                 SpecialBuild:     False
                 Language:         English (United States)

Name           : ProductionBreakpoints_x64.dll
Length         : 790224
CreationTime   : 1/3/2018 4:05:36 PM
LastWriteTime  : 1/3/2018 4:05:36 PM
LastAccessTime : 1/3/2018 4:05:36 PM
Mode           : -a----
LinkType       : 
Target         : 
VersionInfo    : File:             D:\home\SiteExtensions\Microsoft.Application
                 Insights.AzureWebSites\Instrumentation64\ProductionBreakpoints
                 _x64.dll
                 InternalName:     ProductionBreakpoints_x64
                 OriginalFilename: ProductionBreakpoints_x64.dll
                 FileVersion:      2017121202 
                 79af0959ba3685f5c477036da0eb8842efd0a0d5 
                 ProductionBreakpoints-Release
                 FileDescription:  
                 Product:          Microsoft® Visual Studio®
                 ProductVersion:   2017121202 
                 79af0959ba3685f5c477036da0eb8842efd0a0d5 
                 ProductionBreakpoints-Release
                 Debug:            False
                 Patched:          False
                 PreRelease:       False
                 PrivateBuild:     False
                 SpecialBuild:     False
                 Language:         English (United States)
wiktork commented 6 years ago

Thanks for putting this together. Unfortunately these look like the latest bits. I will try repro this issue, but so far I've had no luck.