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

Missing Type in ServiceCollection when upgrading Microsoft.ApplicationInsights.AspNetCore 2.1.0 to 2.1.1 #502

Closed
SergeyKanzhelev opened this issue Jul 25, 2017 · 10 comments

Comments

@SergeyKanzhelev
Copy link
Contributor

Moved from https://github.com/Microsoft/ApplicationInsights-Home/issues/139

I have a unittest that tries to create all services in my service collection.

While populating the service collection I call

services.AddApplicationInsightsTelemetry( configuration );

in 2.1.0 i could create all services application insights adds

in 2.1.1 i get

System.InvalidOperationException : failed to create Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet

System.InvalidOperationException : Unable to resolve service for type 'System.Text.Encodings.Web.JavaScriptEncoder' while attempting to activate 'Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet'.

am i supposed to call another add stuff method or should this type be registered in AddApplicationInsightsTelemetry

@SergeyKanzhelev
Copy link
Contributor Author

Yeah, it looks like something introduced recently to address potential security vulnerability. This class only registered by MVC. Something like this: https://github.com/aspnet/HtmlAbstractions/blob/c2690023d8f14f8deb64b9571212ad2da5ff6541/src/Microsoft.Extensions.WebEncoders/EncoderServiceCollectionExtensions.cs#L37

Try to register it as a workaround. In the next version we will need to have an override that do not depend on this class.

@JanEggers
Copy link

thx sergey

@cijothomas
Copy link
Contributor

@SergeyKanzhelev
Should we try to not throw if JavaScriptEncoder is not registered, and fall-back to the same logic we have prior to this? (https://github.com/Microsoft/ApplicationInsights-aspnetcore/pull/411/files#diff-f2533377302ae5764e5470a777b4ca1bL62)

Or should AI SDK try to add JavaScriptEncoder, if not already added?

@cijothomas
Copy link
Contributor

cijothomas commented Nov 3, 2017

Or check if JavaScriptEncoder is already injected, if not dont add AI JavascriptSnippet..? something like this..
if(services.Any(service => service.ServiceType == typeof(JavaScriptEncoder))) { services.AddSingleton<JavaScriptSnippet>(); }
https://github.com/Microsoft/ApplicationInsights-aspnetcore/blob/develop/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs#L174

@SergeyKanzhelev
Copy link
Contributor Author

@cijothomas the point of this bug is make it not to throw. Implementation of this encoder is like 20 lines of code or so... So we can fall back to custom encoder

@cijothomas cijothomas modified the milestones: 2.2-beta2, 2.2.0 Nov 22, 2017
@cijothomas
Copy link
Contributor

This bug exists, but not introduced in 2.2.0-betas. While this needs fixed, it should not block 2.2.0 stable release.
Will move this to the next immediate milestone.

@cijothomas cijothomas modified the milestones: 2.2.0, 2.3.0-beta1, 2.3.0 Feb 2, 2018
@cijothomas cijothomas modified the milestones: 2.3.0, 2.4.0 May 2, 2018
@cijothomas cijothomas modified the milestones: 2.4.0, 2.5.0 Aug 3, 2018
@Yush0
Copy link

Yush0 commented Aug 7, 2018

I also encounter this error in my Integration Tests. The only solution I found is to completly remove Application Insight from my Webapp on the client side. I would wather keep using it since its very helpfull to our team.
Has anyone got it working with a modified AddApplicationInsightsTelemetry function pointed out by @cijothomas? I don't get it working with manually adding the services in my Startup.

@dazinator
Copy link

dazinator commented Aug 29, 2018

I just came accross this here: aspnet/Mvc#8340 (comment) when using asp.net core 2.0

The only solution I found was either to call services.AddWebEncoders(); or to disable insights.

My case is non standard, but even so this was problematic. ApplicationInsights seems to be a very special cased library and its hard to diagnose what is going wrong when there is a failure.

@cijothomas cijothomas modified the milestones: 2.5.0, 2.6.0 Oct 3, 2018
@Yush0
Copy link

Yush0 commented Nov 5, 2018

Only thing that got it working for me is injecting the following services manually while testing:

services.AddScoped<TelemetryConfiguration>();
services.AddScoped<JavaScriptSnippet>();

The CustomAuth had to be removed also for this to work.

@cijothomas cijothomas modified the milestones: 2.6.0, 2.7.0 Jan 25, 2019
@cijothomas cijothomas modified the milestones: 2.7.0, Future May 31, 2019
@cijothomas cijothomas modified the milestones: Future, 2.8.0 Jun 18, 2019
@cijothomas
Copy link
Contributor

This is an issue with default webapi apps in asp.net core 3.0 preview builds. Tagging to fix for the next immediate release.

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

No branches or pull requests

5 participants