Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception when using Lamar #181

Closed
Drol opened this issue Oct 3, 2022 · 7 comments
Closed

Exception when using Lamar #181

Drol opened this issue Oct 3, 2022 · 7 comments
Assignees
Milestone

Comments

@Drol
Copy link

Drol commented Oct 3, 2022

Exception:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> 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, ILogger1 logger, Nullable1 initialSnap, Func1 getProcessorCount)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] 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.<>c__DisplayClass15_0.b__0(Scope s)
at Lamar.ServiceGraph.<>c__DisplayClass48_0.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.Core.Orchestration.MetricsProviders.MetricsProviderResolver.Resolve(MetricsProviderCategory category)
at Microsoft.ApplicationInsights.Profiler.Core.Orchestration.ResourceUsageSource..ctor(IMetricsProviderResolver1 metricsProviderResolver, CpuTriggerSettings cpuTriggerSettings, MemoryTriggerSettings memoryTriggerSettings, ISerializationProvider serializer, ILogger1 logger)
--- End of inner exception stack trace ---`

@xiaomi7732
Copy link
Member

Hi Drol,
Thanks for the report. Is Lamar your IoC container? We don't support Lamar. That said, this is the signature of the constructor of ProcessInfoCPUMetricsProvider:

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

Generally, if there's a way to satisfy the constructors with the default parameters, it should work. A guess is that Lamar doesn't how to satisfy the delegate of getProcessorCount, and it doesn't leverage the default of null, thus the exception upon building the dependency graph to resolve the service.

Please seek help from Lamar to see if there's a way to satisfy the dependency.

@Drol
Copy link
Author

Drol commented Oct 3, 2022

Thanks for the quick reply. Yes we use Lamar for IoC.

For anyone having the same problem. You can solve it by adding this line. Taken from JasperFx/lamar#352
services.AddTransient((_) => new Func<int>(() => System.Environment.ProcessorCount));

The author of Lamar says "The real issue here being that the optional constructor argument should be ignored? I'll get around to this at some point, but I'd point at foolish design on their part for DI usage." @ JasperFx/lamar#352

I don't want to point fingers but maybe you could make a change for this to make it work with Lamar (and possibly other IoC)?

Best regards

@xiaomi7732
Copy link
Member

Hey @Drol, Thanks for the update. I agree that it is a bad design (and it was coded by me). I will bring this to the team for triage to see if we can prioritize a fix.

@Drol
Copy link
Author

Drol commented Oct 4, 2022

Thanks @xiaomi7732 :-)

@xiaomi7732 xiaomi7732 added this to the Backlog milestone Oct 21, 2022
@xiaomi7732 xiaomi7732 added workaround provided bug Something isn't working labels Oct 21, 2022
@xiaomi7732 xiaomi7732 modified the milestones: Backlog, 2.5.1 Nov 15, 2022
@xiaomi7732
Copy link
Member

Update: There will be a release for 2.5.1-beta in 1 to 2 weeks.

@xiaomi7732 xiaomi7732 added the status: investigating Issue in investigating, fix not available yet. label Jan 30, 2023
@xiaomi7732 xiaomi7732 self-assigned this Jan 30, 2023
@xiaomi7732
Copy link
Member

This should have been fixed. Please check it out in 2.5.1-beta1.
https://www.nuget.org/packages/Microsoft.ApplicationInsights.Profiler.AspNetCore/2.5.1-beta1

Please feel free to file a new issue if there are more issues.

@xiaomi7732 xiaomi7732 added status: resolved and removed status: investigating Issue in investigating, fix not available yet. labels Feb 8, 2023
@xiaomi7732
Copy link
Member

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

No branches or pull requests

2 participants