使用远程处理时无法找到程序集 System.Diagnostics.DiagnosticSource

Unable to find assembly System.Diagnostics.DiagnosticSource when using Remoting

将 Application Insights 添加到我的 ASP.NET 应用程序(标准 ASP.NET 使用 .NET 4.5.2)后,我发现在使用 .NET 远程处理时出现异常。例外情况是:

无法找到程序集'System.Diagnostics.DiagnosticSource, Version=4.0.2.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'

我不太明白这里发生了什么(跟踪似乎跨越了应用程序域边界),但是如果我将 System.Diagnostics.DiagnosticSource NuGet 包添加到远程服务,错误就会消失。

不幸的是,我刚刚发现我在尝试使用 SqlDependency 触发缓存刷新时也会遇到此错误。有没有办法在使用远程处理时禁用此跟踪,如果这是实际发生的事情,或者是否有其他方法可以解决此问题?

Server stack trace: 
   at System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo, String name)
   at System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInformationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Remoting.Channels.CrossAppDomainSerializer.DeserializeObject(MemoryStream stm)
   at System.Runtime.Remoting.Messaging.SmuggledMethodCallMessage.FixupForNewAppDomain()
   at System.Runtime.Remoting.Channels.CrossAppDomainSink.DoDispatch(Byte[] reqStmBuff, SmuggledMethodCallMessage smuggledMcm, SmuggledMethodReturnMessage& smuggledMrm)
   at System.Runtime.Remoting.Channels.CrossAppDomainSink.DoTransitionDispatchCallback(Object[] args)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at System._AppDomain.CreateInstance(String assemblyName, String typeName)
   at System.Data.SqlClient.SqlDependency.CreateProcessDispatcher(_AppDomain masterDomain)
   at System.Data.SqlClient.SqlDependency.ObtainProcessDispatcher()
   at System.Data.SqlClient.SqlDependency.Start(String connectionString, String queue, Boolean useDefaults)

我通过升级到 System.Diagnostics.DiagnosticSource 4.5.0

修复了它

问题在于 Application Insights 创建了一个 System.Diagnostics.Activity 的实例并将其存储在 CallContext (LogicalCallContext) 中。在远程调用期间,CallContext 被编组到远程服务器。不幸的是,这个 .dll 不在那里。

就我而言,更糟糕的是,远程服务器是 .NET 2.0 上的遗留应用程序 运行,复制 dll 并没有解决问题。

您可以在 https://github.com/Microsoft/ApplicationInsights-dotnet-server/issues/613

找到更多相关信息