microsoft / ApplicationInsights-Profiler-AspNetCore

Application Insights Profiler sample and documentation
MIT License
66 stars 24 forks source link

Error when using Autofac IoC container #139

Closed dolly22 closed 3 years ago

dolly22 commented 3 years ago

Profiler is not working when using different Autofac IoC container for Microsoft.Extensions.DependencyInjection.

The error is:

Microsoft.ApplicationInsights.Profiler.Core.ServiceProfilerProvider: Error: Unexpected error happens on stopping service profiler tracing.

Autofac.Core.DependencyResolutionException: Unable to generate a function to return type 'System.Threading.Tasks.Task`1[[System.Int32, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' with input parameter types [System.String, System.String]. The input parameter type list has duplicate types. Try registering a custom delegate type instead of using a generic Func relationship. at lambda_method755(Closure , String , String ) at Microsoft.ApplicationInsights.Profiler.Core.UploaderProxy.TraceUploaderProxy.UploadAsync(DateTimeOffset sessionId, String traceFilePath, String metadataFilePath, String sampleFilePath, Guid dataCube) at Microsoft.ApplicationInsights.Profiler.Core.ServiceProfilerProvider.UploadTraceAsync(PostStopOptions options, Int32 processId, String serializedSampleFilePath) at Microsoft.ApplicationInsights.Profiler.Core.ServiceProfilerProvider.PostStopProcessAsync(PostStopOptions e) at Microsoft.ApplicationInsights.Profiler.Core.ServiceProfilerProvider.StopServiceProfilerAsync(IProfilerSource source)

The problem is with Microsoft.ApplicationInsights.Profiler.Core.UploaderProxy.TraceUploaderProxy constructor signature, precisely with it's last parameter callUploadAsync.

public TraceUploaderProxy(
      IEnumerable<IPrioritizedUploaderLocator> uploaderLocators,
      IProfilerFrontendClient profilerFrontendClient,
      IFileWrapper fileUtilities,
      IOutOfProcCaller uploader,
      IServiceProfilerContext context,
      ILogger<TraceUploaderProxy> logger,
      IOptions<UserConfiguration> serviceProfilerConfiguration,
      IUploadContextValidator uploadContextValidator,
      IProfilerCoreAssemblyInfo profilerVersion,
      Func<string, string, Task<int>> callUploadAsync = null)

It's possible to workaround the problem by overriding the offending DI registration later in Autofac container initialization, but both interface and implementing class are marked as internal and clearly not meant to be tampered with and also there is another problem with different implementation registration when using StandaloneMode.

Autofac registration workaround is something like this...

var interfaceType = typeof(IServiceProfilerProvider).Assembly.GetType("Microsoft.ApplicationInsights.Profiler.Core.UploaderProxy.ITraceUploader");
var targetType = typeof(IServiceProfilerProvider).Assembly.GetType("Microsoft.ApplicationInsights.Profiler.Core.UploaderProxy.TraceUploaderProxy");

builder.RegisterType(targetType)
    .As(interfaceType)
    .WithParameter(new NamedParameter("callUploadAsync", null));

But it would be much cleaner to redesign callUploadAsync extension point to something more IoC container friendly so ServiceProfiler could be used with different IoC container directly.

xiaomi7732 commented 3 years ago

Hey @dolly22 just so that you know, I have a potential fix for this. My opinion on it is that it was a bad design and I am willing to correct it. It will be released in a beta package soon alone with other changes but it will take a while to be stable enough before moving it out of beta.

Will you be able to use a beta package or will it a problem for you?

dolly22 commented 3 years ago

@xiaomi7732 Thank you, no problem with using beta package.

xiaomi7732 commented 3 years ago

@dolly22 Just so that you know, the release of the beta package has been blocked by some other issues. We are looking into address those, it might take a while.

Feel free to let us know if you need this change in urgent and we will see how to priorities it. I don't have a concreate ETA yet.

xiaomi7732 commented 3 years ago

Please try it out with https://www.nuget.org/packages/Microsoft.ApplicationInsights.Profiler.AspNetCore/2.3.0-beta2.