Skip to content

Service is registered twice #270

@CoenraadS

Description

@CoenraadS

When I try to register a scoped service, it seems the interface is registered as scoped, but the concrete class is also registered as transient?

using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using Xunit;

namespace Scrutor.Tests;

public class RegistrationTest : TestBase
{
    private IServiceCollection Collection { get; } = new ServiceCollection();

    [Fact]
    public void ShouldRegisterOnce()
    {
        Collection.Scan(scan => scan
            .FromTypes(typeof(MyClass))
            .AddClasses(classes => classes.AssignableTo<MyInterface>())
            .As<MyInterface>()
            .WithScopedLifetime());

        Assert.Single(Collection);
    }
}

public interface MyInterface
{
}

public class MyClass : MyInterface
{
}
Message: 
Assert.Single() Failure: The collection contained 2 items
Collection: [ServiceType: Scrutor.Tests.MyClass Lifetime: Transient ImplementationType: Scrutor.Tests.MyClass, ServiceType: Scrutor.Tests.MyInterface Lifetime: Scoped ImplementationType: Scrutor.Tests.MyClass]

  Stack Trace: 
FluentKeyedServiceTests.CanRegisterKeyedServiceUsingStaticProperty() line 20
MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions