Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,17 @@ private ConstructorCallSite CreateConstructorCallSite(
{
ServiceCallSite? callSite = null;
Type parameterType = parameters[index].ParameterType;

// reference types cannot be resolved directly, but can be resolved via their underlying type
if (parameterType.IsByRef)
{
Type? elementType = parameterType.GetElementType();
if (elementType != null)
{
parameterType = elementType;
}
}

foreach (var attribute in parameters[index].GetCustomAttributes(true))
{
if (serviceIdentifier.ServiceKey != null && attribute is ServiceKeyAttribute)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public void CreateCallSite_CreatesInstanceCallSite_IfTypeHasDefaultOrPublicParam
[InlineData(typeof(TypeWithParameterizedAndNullaryConstructor))]
[InlineData(typeof(TypeWithMultipleParameterizedConstructors))]
[InlineData(typeof(TypeWithSupersetConstructors))]
[InlineData(typeof(TypeWithPrimaryConstructorWithInDependency))]
public void CreateCallSite_CreatesConstructorCallSite_IfTypeHasConstructorWithInjectableParameters(Type type)
{
// Arrange
Expand Down Expand Up @@ -1008,7 +1009,15 @@ private static IEnumerable<Type> GetParameters(ConstructorCallSite constructorCa
constructorCallSite
.ConstructorInfo
.GetParameters()
.Select(p => p.ParameterType);
.Select(p => {
var type = p.ParameterType;
if (type.IsByRef)
{
var refType = type.GetElementType();
return refType ?? type;
}
return type;
});

private static ConstructorInfo GetConstructor(Type type, Type[] parameterTypes) =>
type.GetTypeInfo().DeclaredConstructors.First(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.DependencyInjection.Specification.Fakes;

namespace Microsoft.Extensions.DependencyInjection.ServiceLookup
{
public class TypeWithPrimaryConstructorWithInDependency(in IFakeService fakeService)
{
private readonly IFakeService _fakeService = fakeService;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,20 @@ public void GetService_DoesNotThrow_WhenGetServiceForNonScopedImplementationWith
Assert.NotNull(result);
}

[Fact]
public void GetService_DoesNotThrow_WhenGetServiceForNonScopedImplementationWithPrimaryConstructorUsingInParameter()
{
// Arrange
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<IBar, Bar>();
serviceCollection.AddSingleton<IFoo, FooWithInDependency>();
var serviceProvider = serviceCollection.BuildServiceProvider(true);

// Act + Assert
var foo = serviceProvider.GetService(typeof(IFoo));
Assert.NotNull(foo);
}

[Fact]
public void BuildServiceProvider_ValidateOnBuild_Throws_WhenScopedIsInjectedIntoSingleton()
{
Expand Down Expand Up @@ -412,6 +426,11 @@ public Foo2(IBar bar)
}
}

private class FooWithInDependency(in IBar bar) : IFoo
{
private readonly IBar bar = bar;
}

private interface IBar
{
}
Expand Down