Skip to content

Application Insights profiler (AddServiceProfiler) fails when using Lamar #352

@andreas-valtech

Description

@andreas-valtech

In the method AddServiceProfiler from the package Microsoft.ApplicationInsights.Profiler.AspNetCore a dependecy on a Func<int> is used which crashes Lamar if no such registration is found. The built in IoC container just ignores the parameter since it has a default value.

The failure happens in ProcessInfoCPUMetricsProvider since it cant resolve the Func<int> getProcessorCount dependency:

public ProcessInfoCPUMetricsProvider(
      IProcessProvider processProvider,
      ILogger<ProcessInfoCPUMetricsProvider> logger,
      (DateTimeOffset, TimeSpan)? initialSnap = null,
      Func<int> getProcessorCount = null)

I have added a workaround in my Startup.cs which works but is a bit sketchy:

services.AddTransient((_) => new Func<int>(() => System.Environment.ProcessorCount));
services.AddApplicationInsightsTelemetry();
services.AddServiceProfiler();

Using Lamar.Microsoft.DependencyInjection version 8.0.1.

The stack trace I get without the workaround above is:


fail: Microsoft.ApplicationInsights.Profiler.AspNetCore.ServiceProfilerStartupFilter[0]
      Unexpected error.
      System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
       ---> Lamar.IoC.LamarMissingRegistrationException: No service registrations exist or can be derived for int
         at Lamar.IoC.Scope.GetInstance(Type serviceType)
         at Lamar.IoC.Scope.GetInstance[T]()
         at Microsoft.ServiceProfiler.Orchestration.MetricsProviders.ProcessInfoCPUMetricsProvider..ctor(IProcessProvider processProvider, ILogger`1 logger, Nullable`1 initialSnap, Func`1 getProcessorCount)
         --- End of inner exception stack trace ---
         at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
         at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
         at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
         at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
         at Lamar.IoC.Instances.ConstructorInstance.quickResolve(Scope scope)
         at Lamar.IoC.Instances.ConstructorInstance.QuickResolve(Scope scope)
         at Lamar.IoC.Instances.ConstructorInstance.<>c__DisplayClass18_0.<quickResolve>b__0(CtorArg x)
         at System.Linq.Enumerable.SelectArrayIterator`2.ToArray()
         at Lamar.IoC.Instances.ConstructorInstance.quickResolve(Scope scope)
         at Lamar.IoC.Instances.ConstructorInstance.QuickResolve(Scope scope)
         at Lamar.IoC.Instances.ConstructorInstance.<>c__DisplayClass18_0.<quickResolve>b__0(CtorArg x)
         at System.Linq.Enumerable.SelectArrayIterator`2.ToArray()
         at Lamar.IoC.Instances.ConstructorInstance.quickResolve(Scope scope)
         at Lamar.IoC.Instances.ConstructorInstance.QuickResolve(Scope scope)
         at Lamar.IoC.Instances.ConstructorInstance.<>c__DisplayClass18_0.<quickResolve>b__0(CtorArg x)
         at System.Linq.Enumerable.SelectArrayIterator`2.ToArray()
         at Lamar.IoC.Instances.ConstructorInstance.quickResolve(Scope scope)
         at Lamar.IoC.Instances.ConstructorInstance.<>c__DisplayClass15_0.<ToResolver>b__0(Scope s)
         at Lamar.ServiceGraph.<>c__DisplayClass48_0.<FindResolver>b__0(Scope s)
         at Lamar.IoC.Scope.GetInstance(Type serviceType)
         at Lamar.Container.Microsoft.Extensions.DependencyInjection.ISupportRequiredService.GetRequiredService(Type serviceType)
         at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
         at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
         at Microsoft.Extensions.DependencyInjection.ServiceCollectionBuilder.<>c.<Build>b__0_5(IServiceProvider p)
         at Lamar.IoC.Resolvers.SingletonLambdaResolver`2.Build(Scope scope)
         at Lamar.IoC.Resolvers.SingletonResolver`1.Resolve(Scope scope)
         at Lamar.IoC.Instances.LambdaInstance`2.Resolve(Scope scope)
         at Lamar.IoC.Instances.Instance.QuickResolve(Scope scope)
         at Lamar.IoC.Instances.ConstructorInstance.<>c__DisplayClass18_0.<quickResolve>b__0(CtorArg x)
         at System.Linq.Enumerable.SelectArrayIterator`2.ToArray()
         at Lamar.IoC.Instances.ConstructorInstance.quickResolve(Scope scope)
         at Lamar.IoC.Instances.ConstructorInstance.QuickResolve(Scope scope)
         at Lamar.IoC.Instances.ConstructorInstance.<>c__DisplayClass18_0.<quickResolve>b__0(CtorArg x)
         at System.Linq.Enumerable.SelectArrayIterator`2.ToArray()
         at Lamar.IoC.Instances.ConstructorInstance.quickResolve(Scope scope)
         at Lamar.IoC.Instances.ConstructorInstance.<>c__DisplayClass15_0.<ToResolver>b__0(Scope s)
         at Lamar.ServiceGraph.<>c__DisplayClass48_0.<FindResolver>b__0(Scope s)
         at Lamar.IoC.Scope.GetInstance(Type serviceType)
         at Lamar.Container.Microsoft.Extensions.DependencyInjection.ISupportRequiredService.GetRequiredService(Type serviceType)
         at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
         at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
         at Microsoft.ApplicationInsights.Profiler.AspNetCore.ServiceProfilerStartupFilter.<>c__DisplayClass1_1.<<Configure>b__1>d.MoveNext()


Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions