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

Adding ApplicationInsights to Asp.Net project that uses MSReportViewer breaks reports #684

Closed
elexisvenator opened this issue Sep 11, 2017 · 8 comments

Comments

@elexisvenator
Copy link

.net 4.5.2 (also affects up to 4.7)
Microsoft.AspNet.Mvc 5.2.0 (also affects 5.2.3)
ApplicationInsights 2.4.1 (Issue present as of 2.4.0)

Our application uses MsReportViewer to render .rdlc files. As of ApplicationInsights v2.4.0, there is now a reference to System.Diagnostics.DiagnosticsSource. This new reference appears to be causing issues when rendering rdlc files, see below.

image

What I believe is happening is CreateSnapshot() is attempting to serialise the report and its configuration, but at some point a System.Diagnostics.Activity instance has been attached. This happens regardless of whether Application Insights is enabled, it only needs to be installed.

Activity.cs contains a number of fields that are initialized by generating a new Guids, which I believe is the ultimate cause of the exception. I cant find any way of preventing Activity objects being created when in the reporting requests so for now I have a choice of either having AppInsights logging or having reporting.

Possible fixes (assuming I was right about the cause):

  • Provide a way to disable the use of Activity(s) during a specific request.
  • Alter Activity to if not be serializable, then at least not interfere with serialization of what it has attached to. (through use of NonSerialized attribute?)
@cijothomas
Copy link
Contributor

@lmolkova can you help take a look here?

@lmolkova
Copy link
Member

lmolkova commented Sep 11, 2017

Hello @elexisvenator

I'm not sure VerificationException is related to the fact Activity is Serializable or uses guids during static initialization. Serialization attribute was only applied on .NET 4.5, but not on other .NET versions.

The VerificationException happens presumably because your app runs in partially trusted mode and perhaps some security attributes are missing on Activity.

Could you please check this article and try to configure different trust levels for report viewer?
https://msdn.microsoft.com/en-us/library/hh286315.aspx
Please let me know if/which level worked.

Thanks,
Liudmila

@elexisvenator
Copy link
Author

Hi @lmolkova

I don't believe that the issue is trust related, the application is currently running in Full trust mode. (No strong names in use etc.)

Here is the full stack trace of all inner exceptions, I didn't realise that the screenshot didn't include everything. The very bottom of the stack trace is why I suspect serialization of Activity might be the issue:

[Microsoft.ReportingServices.ReportProcessing.ReportProcessingException] An unexpected error occurred in Report Processing.
   at Microsoft.ReportingServices.ReportProcessing.ReportProcessing.CreateSnapshot(DateTime executionTimeStamp, ProcessingContext pc, IChunkFactory yukonCompiledDefinition)
   at Microsoft.Reporting.LocalService.Render(String format, String deviceInfo, String paginationMode, Boolean allowInternalRenderers, IEnumerable dataSources, CreateAndRegisterStream createStreamCallback)
   at Microsoft.Reporting.WebForms.ReportViewer.OnPreRender(EventArgs e)

[System.Reflection.TargetInvocationException] Exception has been thrown by the target of an invocation.
Server stack trace: 
   at System.RuntimeFieldHandle.SetValue(RtFieldInfo field, Object obj, Object value, RuntimeType fieldType, FieldAttributes fieldAttr, RuntimeType declaringType, Boolean& domainInitialized)
   at System.Reflection.RtFieldInfo.UnsafeSetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
   at System.Runtime.Serialization.FormatterServices.SerializationSetValue(MemberInfo fi, Object target, Object value)
   at System.Runtime.Serialization.FormatterServices.PopulateObjectMembers(Object obj, MemberInfo[] members, Object[] data)
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ParseObjectEnd(ParseRecord pr)
   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.Collections.Generic.IList`1.get_Item(Int32 index)
   at Microsoft.ReportingServices.ReportIntermediateFormat.TextBox.SetExprHost(ReportExprHost reportExprHost, ObjectModelImpl reportObjectModel)
   at Microsoft.ReportingServices.OnDemandProcessing.OnDemandProcessingContext.RuntimeInitializeTextboxObjs(ReportItem reportItem, Boolean setExprHost)
   at Microsoft.ReportingServices.OnDemandProcessing.Merge.SetupReport(ReportInstance reportInstance)
   at Microsoft.ReportingServices.ReportProcessing.Execution.ProcessReportOdp.CreateReportInstance(OnDemandProcessingContext odpContext, OnDemandMetadata odpMetadata, ReportSnapshot reportSnapshot, Merge& odpMerge)
   at Microsoft.ReportingServices.ReportProcessing.Execution.ProcessReportOdp.Execute(OnDemandProcessingContext& odpContext)
   at Microsoft.ReportingServices.ReportProcessing.ReportProcessing.CreateSnapshot(DateTime executionTimeStamp, ProcessingContext pc, IChunkFactory yukonCompiledDefinition)

[System.TypeInitializationException] The type initializer for 'System.Diagnostics.Activity' threw an exception.

[System.Security.VerificationException] Operation could destabilize the runtime.
   at System.Diagnostics.Activity.GetRandomNumber()
   at System.Diagnostics.Activity..cctor()

@lmolkova
Copy link
Member

@elexisvenator
You mentioned you observed this issue on .Net 4.7. Activity is NOT serializable there.
Moreover, serialization (on 4.5) happens when there is cross app-domain call. If something were missing on the Activity for serialization, it would cause SerializationException.

You can validate serialization assumption by trying DiagnosticSource 4.5.0 (latest) from https://dotnet.myget.org/F/dotnet-core/api/v3/index.json nuget source. Activity is no longer serializable even on 4.5: dotnet/corefx@cb1c1df#diff-e4e08aebec9253d67b26eda5bbb91353

Note that according to https://msdn.microsoft.com/en-us/library/hh286315.aspx ReportViewer control runs in the partial trust mode.

Thanks for the full stack trace, it seems that System.Diagnostics.Activity.GetRandomNumber() misses some security related attribute. I'll check on that. But it would really help if you try to change the trust level on the report viewer to validate security assumption?

@elexisvenator
Copy link
Author

Hi @lmolkova
DiagnosticsSource 4.5.0 seems to fix the issue :D. Is there a rough timeline for DiagnosticsSource 4.5.0 stable release?

Regarding trusts, The ReportViewer control that I am using is run in full trust as I am using local processing mode. Any other trust level will cause a security exception. I didn't know this myself until trying to change the trust using the msdn article you linked. (see the important message at the top of the article).

@lmolkova
Copy link
Member

@elexisvenator

interesting! Thanks for checking. Then what happened on net 4.7? Was it the same issue?
The preview should be available on nuget sometime in Q4 2017 and stable release is expected in Q1 next year according to .net schedule.

Would you be able to use 4.5.0 from the myget feed? There were no considerable changes from 4.4.0 except serialization, so if you stick to the particular version of 4.5.0 (e.g. current), it should be as stable as 4.4.0.

There might be some workarounds for the issue with 4.4.0. Would you be able to provide repro app so I try to find some workarounds?

Thanks,
Liudmila

@elexisvenator
Copy link
Author

Hi @lmolkova

The stack trace is the same for .net 4.7.

I am having trouble creating a repro app that encounters the issue, my guess is there is some other library or configuration in our main app that combines with 4.4.0 to create the perfect storm, but I haven't found a way to isolate it yet. I will keep trying.

In the meantime I have tweaked our build to support restoring packages from the myget source.

Thanks for the help,
Ben

@cornem
Copy link

cornem commented Oct 5, 2017

Upgrading to DiagnosticsSource 4.4.1 (stable) fixed this error for me on NET47.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants