Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Additional statistics when a ServiceProvider is created
* Number of open generics
* Number of closed generics

fix #52364
  • Loading branch information
allantargino authored and github-actions committed Aug 24, 2021
commit f2a280606fec0d8ac92159730152e5a44152e001
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ public void ServiceRealizationFailed(string? exceptionMessage, int serviceProvid
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Parameters to this method are primitive and are trimmer safe.")]
[Event(7, Level = EventLevel.Informational, Keywords = Keywords.ServiceProviderInitialized)]
private void ServiceProviderBuilt(int serviceProviderHashCode, int singletonServices, int scopedServices, int transientServices)
private void ServiceProviderBuilt(int serviceProviderHashCode, int singletonServices, int scopedServices, int transientServices, int closedGenericsServices, int openGenericsServices)
{
WriteEvent(7, serviceProviderHashCode, singletonServices, scopedServices, transientServices);
WriteEvent(7, serviceProviderHashCode, singletonServices, scopedServices, transientServices, closedGenericsServices, openGenericsServices);
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Expand Down Expand Up @@ -174,6 +174,8 @@ private void WriteServiceProviderBuilt(ServiceProvider provider)
int singletonServices = 0;
int scopedServices = 0;
int transientServices = 0;
int closedGenericsServices = 0;
int openGenericsServices = 0;

StringBuilder descriptorBuilder = new StringBuilder("{ \"descriptors\":[ ");
bool firstDescriptor = true;
Expand Down Expand Up @@ -202,11 +204,23 @@ private void WriteServiceProviderBuilt(ServiceProvider provider)
transientServices++;
break;
}

if (descriptor.ServiceType.IsGenericType)
{
if (descriptor.ServiceType.IsConstructedGenericType)
{
closedGenericsServices++;
}
else
{
openGenericsServices++;
}
}
}
descriptorBuilder.Append(" ] }");

int providerHashCode = provider.GetHashCode();
ServiceProviderBuilt(providerHashCode, singletonServices, scopedServices, transientServices);
ServiceProviderBuilt(providerHashCode, singletonServices, scopedServices, transientServices, closedGenericsServices, openGenericsServices);

string descriptorString = descriptorBuilder.ToString();
int chunkCount = descriptorString.Length / MaxChunkSize + (descriptorString.Length % MaxChunkSize > 0 ? 1 : 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,18 @@ public void EmitsServiceProviderBuilt()
serviceCollection.AddScoped<IFakeMultipleService, FakeDisposableCallbackInnerService>();
serviceCollection.AddTransient<IFakeMultipleService, FakeDisposableCallbackInnerService>();
serviceCollection.AddSingleton<IFakeService, FakeDisposableCallbackInnerService>();
serviceCollection.AddScoped(typeof(IFakeOpenGenericService<>), typeof(FakeOpenGenericService<>));
serviceCollection.AddTransient<IFakeOpenGenericService<PocoClass>, FakeOpenGenericService<PocoClass>>();

using ServiceProvider provider = serviceCollection.BuildServiceProvider();

EventWrittenEventArgs serviceProviderBuiltEvent = _listener.EventData.Single(e => e.EventName == "ServiceProviderBuilt");
GetProperty<int>(serviceProviderBuiltEvent, "serviceProviderHashCode"); // assert hashcode exists as an int
Assert.Equal(4, GetProperty<int>(serviceProviderBuiltEvent, "singletonServices"));
Assert.Equal(1, GetProperty<int>(serviceProviderBuiltEvent, "scopedServices"));
Assert.Equal(2, GetProperty<int>(serviceProviderBuiltEvent, "transientServices"));
Assert.Equal(2, GetProperty<int>(serviceProviderBuiltEvent, "scopedServices"));
Assert.Equal(3, GetProperty<int>(serviceProviderBuiltEvent, "transientServices"));
Assert.Equal(1, GetProperty<int>(serviceProviderBuiltEvent, "closedGenericsServices"));
Assert.Equal(1, GetProperty<int>(serviceProviderBuiltEvent, "openGenericsServices"));
Assert.Equal(7, serviceProviderBuiltEvent.EventId);

EventWrittenEventArgs serviceProviderDescriptorsEvent = _listener.EventData.Single(e => e.EventName == "ServiceProviderDescriptors");
Expand Down Expand Up @@ -290,6 +294,16 @@ public void EmitsServiceProviderBuilt()
" \"serviceType\": \"Microsoft.Extensions.DependencyInjection.Specification.Fakes.IFakeService\",",
" \"lifetime\": \"Singleton\",",
" \"implementationType\": \"Microsoft.Extensions.DependencyInjection.Specification.Fakes.FakeDisposableCallbackInnerService\"",
" },",
" {",
" \"serviceType\": \"Microsoft.Extensions.DependencyInjection.Specification.Fakes.IFakeOpenGenericService`1[TValue]\",",
" \"lifetime\": \"Scoped\",",
" \"implementationType\": \"Microsoft.Extensions.DependencyInjection.Specification.Fakes.FakeOpenGenericService`1[TVal]\"",
" },",
" {",
" \"serviceType\": \"Microsoft.Extensions.DependencyInjection.Specification.Fakes.IFakeOpenGenericService`1[Microsoft.Extensions.DependencyInjection.Specification.Fakes.PocoClass]\",",
" \"lifetime\": \"Transient\",",
" \"implementationType\": \"Microsoft.Extensions.DependencyInjection.Specification.Fakes.FakeOpenGenericService`1[Microsoft.Extensions.DependencyInjection.Specification.Fakes.PocoClass]\"",
" }",
" ]",
"}"),
Expand Down