-
-
Notifications
You must be signed in to change notification settings - Fork 239
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
Service Descriptor attribute is not working as expected #182
Comments
Hello @krishnan1159! I just ran the following test without issues: using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace Scrutor.Tests
{
public partial class ScanningTests
{
[Fact]
public void Blah()
{
Collection.Scan(scan => scan
.FromAssemblyOf<ScanningTests>()
.AddClasses(c => c.Where(t => t.Name.EndsWith("Vehicle")))
.UsingAttributes());
var provider = Collection.BuildServiceProvider();
Assert.IsType<Vehicle>(provider.GetRequiredService<IVehicle>());
Assert.IsType<GoodVehicle>(provider.GetRequiredService<IGoodVehicle>());
Assert.IsType<BadVehicle>(provider.GetRequiredService<IBadVehicle>());
}
[ServiceDescriptor(typeof(IGoodVehicle), ServiceLifetime.Transient)]
public class GoodVehicle : Vehicle, IGoodVehicle { }
[ServiceDescriptor(typeof(IBadVehicle), ServiceLifetime.Singleton)]
public class BadVehicle : Vehicle, IBadVehicle { }
[ServiceDescriptor(typeof(IVehicle), ServiceLifetime.Scoped)]
public class Vehicle : IVehicle { }
public interface IVehicle { }
public interface IGoodVehicle { }
public interface IBadVehicle { }
}
} Is that not what you would expect? |
It results in the following registrations, which matches what you'd expect, right?
|
@khellang What is the registration strategy used for your testcase? In your testcase, in what order concerte classes are parsed. I guess that also might make a difference here. |
@khellang Is it right to get all custom attributes from the parent classes also if you have |
@khellang I made a small typo when I am writing the code. Can you please check now? Instead of old block
New Block
|
@khellang What is the scan order in which classes will be scanned? Will the inherited classes will be scanned first and then parent classes will be scanned? Can you please throw some light on it? |
Hi Team,
For the below class structure,
I expect the following registration in services container
But the actual registration is
I register the services using following scan
Root cause:
In the AttributeSelector, when we get the customAttributes we use
type.GetCustomAttributes<ServiceDescriptorAttribute>()
. This will get the attributes from parent classes as well. There is another api (type.GetCustomAttributes<ServiceDescriptorAttribute>(false)
) where we suppress traversing the parent classes. We can use that avoid this issue.I have made the changes tested and wrote unit tests as well. Can I raise a pull request for this issue? Whether there are any workarounds for this issue?
The text was updated successfully, but these errors were encountered: