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

Collection Expressions-y Injections #1408

Open
TonyValenti opened this issue Jan 27, 2024 · 7 comments
Open

Collection Expressions-y Injections #1408

TonyValenti opened this issue Jan 27, 2024 · 7 comments
Labels
enhancement pending-input Pending input or update

Comments

@TonyValenti
Copy link

Problem Statement

AutoFac cannot create immutable or frozen collections.

Desired Solution

AutoFac should use rules/processes similar to what is outlined in collection expressions to create collections of the specified types.

Alternatives You've Considered

Additional Context

@tillig
Copy link
Member

tillig commented Jan 27, 2024

Is this the same as #1372?

@TonyValenti
Copy link
Author

This would eliminate the need for it as this would be a general solution to that specific case.

@tillig
Copy link
Member

tillig commented Jan 27, 2024

Then I'll close the other as duplicate. No need to address the same request twice.

@tillig
Copy link
Member

tillig commented Jan 27, 2024

to create collections of the specified types

Can you provide a specific list of types you think qualify here? "Just know about all the types" isn't specific or feasible. Put another way, if we were to write unit tests to make sure this is "done," what would we test to verify it's done and we can close the issue?

Also, FYI, "Autofac" - no cap F 😉

@tillig tillig added enhancement pending-input Pending input or update labels Feb 3, 2024
@MaQy
Copy link

MaQy commented May 17, 2024

Hi,

It seems a recent change in the 8.0.300 SDK made this more urgent. We are unable to run our application with the latest SDK as it's using a new type called ReadOnlySingleElementList that seems to be used for collection expressions of one element: https://github.com/dotnet/dotnet/blob/84c07ba732cfaf598d5691c1203041af91993b7c/src/roslyn/src/Compilers/CSharp/Portable/Symbols/Synthesized/ReadOnlyListType/SynthesizedReadOnlyListTypeSymbol.cs#L21

This is the kind of errors we are getting now:

Autofac.Core.DependencyResolutionException : An exception was thrown while activating λ:Microsoft.Extensions.Hosting.IHost -> Microsoft.Extensions.Hosting.Internal.ApplicationLifetime -> Microsoft.Extensions.Logging.Logger`1[[Microsoft.Extensions.Hosting.Internal.ApplicationLifetime, Microsoft.Extensions.Hosting, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]] -> Microsoft.Extensions.Logging.LoggerFactory -> Microsoft.Extensions.Options.OptionsMonitor`1[[Microsoft.Extensions.Logging.LoggerFilterOptions, Microsoft.Extensions.Logging, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]] -> Microsoft.Extensions.Options.OptionsFactory`1[[Microsoft.Extensions.Logging.LoggerFilterOptions, Microsoft.Extensions.Logging, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]] -> <>z__ReadOnlySingleElementList`1[[Microsoft.Extensions.Options.IPostConfigureOptions`1[[Microsoft.Extensions.Logging.LoggerFilterOptions, Microsoft.Extensions.Logging, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]], Microsoft.Extensions.Options, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].
---- Autofac.Core.DependencyResolutionException : None of the constructors found on type '<>z__ReadOnlySingleElementList`1[Microsoft.Extensions.Options.IPostConfigureOptions`1[Microsoft.Extensions.Logging.LoggerFilterOptions]]' can be invoked with the available services and parameters:
Cannot resolve parameter 'Microsoft.Extensions.Options.IPostConfigureOptions`1[Microsoft.Extensions.Logging.LoggerFilterOptions] item' of constructor 'Void .ctor(Microsoft.Extensions.Options.IPostConfigureOptions`1[Microsoft.Extensions.Logging.LoggerFilterOptions])'.
Autofac.Core.DependencyResolutionException: An exception was thrown while activating ?:Microsoft.Extensions.Hosting.IHost -> Microsoft.Extensions.Hosting.Internal.ConsoleLifetime -> Microsoft.Extensions.Options.UnnamedOptionsManager`1[[Microsoft.Extensions.Hosting.ConsoleLifetimeOptions, Microsoft.Extensions.Hosting, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]] -> Microsoft.Extensions.Options.OptionsFactory`1[[Microsoft.Extensions.Hosting.ConsoleLifetimeOptions, Microsoft.Extensions.Hosting, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]] -> <>z__ReadOnlySingleElementList`1[[Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.Extensions.Hosting.ConsoleLifetimeOptions, Microsoft.Extensions.Hosting, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]], Microsoft.Extensions.Options, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].
 ---> Autofac.Core.DependencyResolutionException: None of the constructors found on type '<>z__ReadOnlySingleElementList`1[Microsoft.Extensions.Options.IConfigureOptions`1[Microsoft.Extensions.Hosting.ConsoleLifetimeOptions]]' can be invoked with the available services and parameters:
Cannot resolve parameter 'Microsoft.Extensions.Options.IConfigureOptions`1[Microsoft.Extensions.Hosting.ConsoleLifetimeOptions] item' of constructor 'Void .ctor(Microsoft.Extensions.Options.IConfigureOptions`1[Microsoft.Extensions.Hosting.ConsoleLifetimeOptions])'.

Could you please look into it?

@alistairjevans
Copy link
Member

@MaQy, can you please raise a new issue for reporting this issue rather than adding to a feature request?

Please ensure you include a minimal reproducible code example that causes the failure on 8.0.300. A public repo with an example is ideal.

@MaQy
Copy link

MaQy commented May 18, 2024

Hi again,

It took me a while to pinpoint the issue as I was not able to reproduce it in an isolated way. The problem was actually partially in our code, though I think Microsoft is also to be blamed for the sudden change they did. As mentioned above, it seems they have introduced new collections for collection expressions:

int[] a = [1];

This now gets compiled into an auto-generated collection that is inserted into the assembly where that code is located. The name of the class is <>z__ReadOnlySingleElementList. This class is not marked as compiler generated with the CompilerGeneratedAttribute attribute. We have the typical "catch all" method for generic interfaces for some of our assemblies, and this type was suddenly getting registered for IEnumerable. That caused problems with the Autofac resolution when an IEnumerable was requested to find all existing registrations.

I'm sorry for the off-topic and the false alarm, but I wanted to leave this somewhere as a word of warning in case anyone else encounters this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement pending-input Pending input or update
Projects
None yet
Development

No branches or pull requests

4 participants