Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Dependency Inject AppService #133


User avatar
0
daniel created

Hi,

For some reason if I inject one AppService into another, the code will fail to work. However, if I resolve the AppService the code works fine. What's the correct way to go about this?

Streamlined code example below:

public class MyAppService : ApplicationService, IMyAppService
{
	private readonly IAnotherAppService _anotherAppService

	public MyAppService(IAnotherAppService anotherAppService)
	{
		_anotherAppService = anotherAppService;
	}

	public Create(CreateInput input)
	{
		_anotherAppService.Create(new CreateInputDto {}); // This fails

		var AAS = IocManager.Instance.Resolve<AnotherAppService>();
		AAS.Create(new CreateInputDto {}); // This works correctly
	}
}

4 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    Interesting, while I definetely reject injecting one App service into another, I did see it working before. What type of exception are you getting?

  • User Avatar
    0
    daniel created

    I've managed to inject other AppServices to the same one too, they're all working perfectly. So, for example, in my "MyAppService", I've injected "TaskAppService", and I can call GetTasks() from there. This completes successfully.

    I'm not receiving any error at all, the code will not call the function at all. It just skips over it and exits the function.

    If you reject the injection method, which route should we go down? Injecting the IIocManager and resolving from there, or resolving as I did in the working example above?

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    There should be problem with this non-injectable service. Can you share it, at least class definition and ctor. If it's resolving using IIocResolver, then it must be injectable. ABP uses Castle Windows as DI container. Your situation is almost impossible, there should be some wrong here.

  • User Avatar
    0
    daniel created

    Hi, I've renamed my AppServices/Repositories to be generic, but this is the basics of the code which I'm running.

    The problem is bizarre, as, as you can see, I can Create to another app services perfectly fine - There are no problems stepping into the function. It's as if the ComplexAppService isn't being correctly resolved.

    I do have it working using IocManager.Instance.Resolve, so it's not a major bug for me currently, just more curiosity.

    public class MyAppService : ApplicationService, IMyAppService
    {
    	private readonly IRepository<Thing> _thingRepository;
    	private readonly IAnotherAppService _anotherAppService
    	private readonly IComplexAppService _complexAppService
    
    	public MyAppService(IRepository<Thing> thingRepository, IAnotherAppService anotherAppService, IComplexAppService complexAppService)
    	{
    		_thingRepository = thingRepository;
    		_anotherAppService = anotherAppService;
    		_complexAppService = complexAppService;
    	}
    
    	public Create(CreateInput input)
    	{
    		var ThingItem = _thingRepository.Get(1);
    		ThingItem.Prop = true; // This updates correctly
    
    		var AnotherId = _anotherAppService.Create(new CreateAnotherInput { Value = "Some text" }); // This will complete
    
    		_complexAppService.Create(new CreateComplexInput { AnotherId = AnotherId }); // This fails
    
    		var CAS = IocManager.Instance.Resolve<ComplexAppService>();
    		CAS.Create(new CreateComplexInput { AnotherId = AnotherId }); // This works correctly
    	}
    }