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

Adding check for service and instances count. #10741

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand Down Expand Up @@ -98,11 +99,16 @@ public static IServiceCollection CreateChildContainer(this IServiceProvider serv
else if (services.All(s => s.Lifetime == ServiceLifetime.Singleton))
{
// We can resolve them from the main container.
var instances = serviceProvider.GetServices(services.Key);
// Materialize services and instances to arrays once.
var servicesArray = services.ToArray();
var instancesArray = serviceProvider.GetServices(services.Key)?.ToArray() ?? Array.Empty<object>();

for (var i = 0; i < services.Count(); i++)
// Validate counts
ValidateServiceAndInstanceCounts(servicesArray, instancesArray, services.Key);

for (var i = 0; i < servicesArray.Length; i++)
{
clonedCollection.CloneSingleton(services.ElementAt(i), instances.ElementAt(i));
clonedCollection.CloneSingleton(servicesArray[i], instancesArray[i]);
}
}

Expand All @@ -112,25 +118,45 @@ public static IServiceCollection CreateChildContainer(this IServiceProvider serv
// We need a service scope to resolve them.
using var scope = serviceProvider.CreateScope();

var instances = scope.ServiceProvider.GetServices(services.Key);
// Materialize services and instances to arrays once.
var servicesArray = services.ToArray();
var instancesArray = scope.ServiceProvider.GetServices(services.Key)?.ToArray() ?? Array.Empty<object>();

ValidateServiceAndInstanceCounts(servicesArray, instancesArray, services.Key);

// Then we only keep singleton instances.
for (var i = 0; i < services.Count(); i++)
for (var i = 0; i < servicesArray.Length; i++)
{
if (services.ElementAt(i).Lifetime == ServiceLifetime.Singleton)
if (servicesArray[i].Lifetime == ServiceLifetime.Singleton)
{
clonedCollection.CloneSingleton(services.ElementAt(i), instances.ElementAt(i));
clonedCollection.CloneSingleton(servicesArray[i], instancesArray[i]);
}
else
{
clonedCollection.Add(services.ElementAt(i));
clonedCollection.Add(servicesArray[i]);
}
}
}
}

return clonedCollection;
}

private static void ValidateServiceAndInstanceCounts<TService, TInstance>(TService[] servicesArray, TInstance[] instancesArray, Type serviceType)
{
if (servicesArray.Length != instancesArray.Length)
{
var messageBuilder = new StringBuilder()
.AppendLine($"Mismatch detected for type {serviceType}:")
.AppendLine($"services.Count = {servicesArray.Length}, instances.Count = {instancesArray.Length}")
.Append("Services:\n\t")
.Append(string.Join(",\n\t", servicesArray.Select(s => s.ToString())))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this be printed only in kusto logs or also in AppInsights logs? I am wondering if this could reveal too much information about our system. Generally what is the reason for this mismatch between services and instances and how can we fix it by knowing this additional info?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't be sent to AppInsights because the exception occurs before the AppInsights pipeline is initialized. However, it will appear on the portal. I don't see any issues with this, but I'll confirm with Fabio.

In one specific app, an exception was thrown while attempting to load the CloudWorkFlow extension, though it recovered later. We reviewed the logs and memory dump but couldn't determine the root cause. We were unable to reproduce this scenario.

.Append("\nInstances:\n\t")
.AppendLine(string.Join(",\n\t", instancesArray.Select(i => i?.GetType()?.ToString() ?? "null")));
Console.WriteLine(messageBuilder.ToString());
throw new InvalidOperationException(messageBuilder.ToString());
}
}
}

internal static class ServiceCollectionExtensions
Expand Down
Loading