Automatic dependency injecting using Scrutor

1.8k Views Asked by At

I have many services in my project, and trying to use Scrutor for automatic DI instead registering each service manually on startup.cs

BarService.cs

public class BarService : IBar
    {
        public Bar Get(int id)
        {
            var bar = new Bar
            {
                bar_date = DateTime.UtcNow,
                bar_name = "bar"
            };
            return bar;
        }

        public List<Bar> GetMany()
        {
            List<Bar> list = new List<Bar>
            {
                new Bar
                {
                    bar_date = DateTime.UtcNow,
                    bar_name = "bar 1"
                },

                new Bar
                {
                    bar_date = DateTime.UtcNow,
                    bar_name = "bar 2"
                }
            };

            return list;
        }
    }

IBar.cs

public interface IBar
    {
        Bar Get(int id);
        List<Bar> GetMany();
    }

Bar.cs

public class Bar
    {
        public string bar_name { get; set; }
        public DateTime bar_date { get; set; }
    }

BarController.cs

[Route("api/[controller]")]
    [ApiController]
    public class BarController : ControllerBase
    {
        public IBar _service { get; set; }

        public BarController(IBar service)
        {
            _service = service;
        }

        [HttpGet("{id:int}")]
        public IActionResult Get(int id)
        {
            var result = _service.Get(id);
            if (result != null)
            {
                return Ok(result);
            }
            return NotFound("No data found");
        }


        [HttpGet]
        public IActionResult GetMany()
        {
            var result = _service.GetMany();
            if (result != null)
            {
                return Ok(result);
            }
            return NotFound("No data found");
        }

    }

Adding services.AddScoped<IBar, BarService>(); to Startup.cs works fine, but not with Scrutor to auto map.

     services.Scan(scan =>
                    scan.FromCallingAssembly()
                        .AddClasses()
                        .AsMatchingInterface());

I get error

error

2

There are 2 best solutions below

0
Nkosi On BEST ANSWER

You are currently using AsMatchingInterface(), which

Registers services which match a standard naming convention

Which would apply to a match like below.

public class BarService : IBarService

In your example

public class BarService : IBar

Does not follow that convention so does not provide your expected behavior

So either refactor your interfaces to follow Scrutor's expected naming convention between abstraction and implementation,

Or use AsImplementedInterfaces(), which

Registers an implementation as all implemented interfaces

Note however that this will register those interfaces as singleton by default unless a lifetime is provided.

I would suggest registering them as scoped.

0
Michael Wang On

@Vikash Rathee, you could try AsImplementedInterfaces(). It registers each matching concrete type as all of its implemented interfaces.

         services.Scan(scan =>
                        scan.FromCallingAssembly()
                            .AddClasses()
                            .AsImplementedInterfaces());

The result is shown below.

enter image description here