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


You are currently using
AsMatchingInterface(), whichWhich would apply to a match like below.
In your example
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(), whichNote however that this will register those interfaces as singleton by default unless a lifetime is provided.
I would suggest registering them as scoped.