I have 3 project
1 with controllers
2 with Appservices
3 with interfaces
I wrote interface
public interface ICheckoutAppService
{
OrderDto GetOrder();
}
Then implement it in app service
public class CheckoutAppService : ICheckoutAppService
{
public OrderDto GetOrder()
{
var checkout = new OrderDto
{
Amount = 50,
ServiceType = "ecom",
BillRefNo = "BIZ-TEST-PR05004",
CurrencyCode = "SGD",
Payee = "{{payee_payeeliquid}}",
OrderId = "Order_11121314",
OrderItems = new List<OrderItemDto>
{
new()
{
ItemName = "Dell Laptop",
ItemNumber = "1",
ItemUnitPrice = 1000,
OrderQuantity = 1
},
new()
{
ItemName = "Dell Monitor",
ItemNumber = "1",
ItemUnitPrice = 500,
OrderQuantity = 1
}
}
};
return checkout;
}
And then I call it from the controller in the first projects, like this
[Route("api/[controller]")]
public class CustomersController : Controller
{
private readonly ICheckoutAppService _checkoutAppService;
public CustomersController(ICheckoutAppService checkoutAppService)
{
_checkoutAppService = checkoutAppService;
}
[HttpGet]
public OrderDto Get()
{
try
{
return _checkoutAppService.GetOrder();
}
catch (Exception ex)
{
throw ex;
}
}
}
I register the interface in startup via Scrutor like this
services.Scan(scan =>
scan.FromAssemblyOf<ICheckoutAppService>()
.AddClasses()
.AsMatchingInterface());
And get this error
System.InvalidOperationException: Unable to resolve service for type 'TestTaskShared.Interfaces.ICheckoutAppService' while attempting to activate 'CheckoutAPI.Controllers.CustomersController'.
How I can fix this?
Is
ICheckoutAppServiceandCheckoutAppServicein the same assembly?.If not your current scan is using only the referenced interface's project and will not find the class. I assume the class is in the
Appservicesproject.You will need to make reference of that project so scrutor know where to look for the implementation.
Consider changing the scan approach
The above will add the classes from
Appservicesproject as their matched interface. And since theAppservicesproject would have referenced theinterfacesproject, scrutor would know how to find the required interface