Skip to content
This repository was archived by the owner on Jul 5, 2020. It is now read-only.
This repository was archived by the owner on Jul 5, 2020. It is now read-only.

DiagnosticSource.dll not found exception in RoleEnvironment.Changed #613

@lmolkova

Description

@lmolkova

Symptoms

When application is running in cloud service and subscribes to RoleEnvironment change events, it gets
SerializationException: Type is not resolved for member 'System.Diagnostics.Activity,System.Diagnostics.DiagnosticSource, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'

[SerializationException: Type is not resolved for member 'System.Diagnostics.Activity,System.Diagnostics.DiagnosticSource, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.]
   System.AppDomain.get_Id() +0
   <CrtImplementationDetails>.DoCallBackInDefaultDomain(IntPtr function, Void* cookie) +126
   <CrtImplementationDetails>.LanguageSupport.InitializeDefaultAppDomain(LanguageSupport* ) +62
   <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* ) +323
   <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* ) +37
...

Assembly Binding Log Viewer/fuselogvw shows following issue:

*** Assembly Binder Log Entry  (6/6/2017 @ 3:06:07 PM) ***

The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  C:\Program Files (x86)\IIS Express\iisexpress.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = System.Diagnostics.DiagnosticSource, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
 (Fully-specified)
LOG: Appbase = file:///C:/Program Files (x86)/IIS Express/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = iisexpress.exe
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using host configuration file: C:\Users\lmolkova.EUROPE\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Diagnostics.DiagnosticSource, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/IIS Express/System.Diagnostics.DiagnosticSource.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/IIS Express/System.Diagnostics.DiagnosticSource/System.Diagnostics.DiagnosticSource.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/IIS Express/System.Diagnostics.DiagnosticSource.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/IIS Express/System.Diagnostics.DiagnosticSource/System.Diagnostics.DiagnosticSource.EXE.
LOG: All probing URLs attempted and failed.

Note that it is issexpress that could not load the DiagnosticSource assembly, so the presence of dll in the siteroot of the application does not help.

Root cause

  • At the moment when Application_Start is called, there is an active Activity created to track incoming request. Activity declared in the DiagnosticSource.dll
  • Activity.Current is stored in LogicalCallContext
  • When RoleEnvironment.Changed is called, it causes cross AppDomain call and iis tries to deserialize activity from the LogicalCallContext.
  • The causes iis to look for DiagnosticSource dll and it fails.

Workarounds

  1. Install preview DiagnosticSource package with the fix

  2. Put DiagnosticSource dll to one of the folders where iis looks for dlls ("%PROGRAMFILES(X86)%\IIS Express" - IIS Express; %systemroot%\System32\inetsr - IIS).

  3. (Carefully). If you have .NET 4.6 is installed on the dev/test/prod machines, you can manually change reference to DiagnosticSource package to net46 one in the VS by removing reference and adding new one to the
    SolutionDir\packages\System.Diagnostics.DiagnosticSource.4.4.0-preview2-<latest>\lib\net46\System.Diagnostics.DiagnosticSource.dll or by changing csproj file directly

  4. (Carefully) Before calling RoleEnvironment.Changed, clean up current activity with the following code: while (Activity.Current != null) { Activity.Current.Stop(); }
    As the downside, the first request in the instance may not be correlated with dependencies.
    Also, there may be other RoleEnvironment calls that cause cross AppDomain calls, so it may not be suitable in all cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions