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

How can I call the application service in controller? #864


User avatar
0
winson created

for example, I have an IAbcAppService and AppService class, but how can I call it in my controller? I don't know how to instantiate it

in my app service:

public class ABCAppService: IABCAppService
{
        private readonly IRepository<ABCModel> _abcRepository;

        public ABCAppService(IRepository<ABCModel> abcRepository)
        {
            _abcRepository = abcRepository;
        }
}

and in my controller:

public class ABCController : ABCControllerBase
{
        private readonly IABCAppService _abcAppService;

        public ABCController()
        {
        	//should I use this way? but it doesn't work :(
            _abcAppService = IocManager.Instance.Resolve<IABCAppService>();
        }
}

just don't know how to init the _abcAppService in controller.


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

    Just inject your application service into controller as you did it for repository.

    Note1: Your IABCAppService should inherit from IApplicationService. Note2: Do not resolve it from IocManager.Instance since it may cause to memory leaks. I suggest you to read DI document to fully understand it: <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Dependency-Injection">http://www.aspnetboilerplate.com/Pages/ ... -Injection</a>

  • User Avatar
    0
    winson created

    yes, actually I tried everything before, but still not working, but when I reset the DB and run update-database again, that's working, maybe I use the difference table name between entity and database, but suppose that's not be an issue, I have set the table attribute in the entity model

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Do you still have this error ? Can you share your logs and final code of service and controller ?

  • User Avatar
    0
    winson created

    Yes, that strange, when I create a new table and controller, I got the error again, don't know why :(

    I got the below errors:

    Server Error in '/' Application.
    
    Can't create component 'MPeLife.FormLayoutApp.FormLayoutAppService' as it has dependencies to be satisfied.
    
    'MPeLife.FormLayoutApp.FormLayoutAppService' is waiting for the following dependencies:
    - Service 'Abp.Domain.Repositories.IRepository`1[[MPeLife.Models.FormLayout, MPeLife.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' which was not registered.
    

    in my controller:

    private readonly IFormLayoutAppService _formLayoutAppService;
    
    public FormLayoutController(IFormLayoutAppService formLayoutAppService)
    {
           _formLayoutAppService = formLayoutAppService;
    }
    

    in app service:

    private readonly IRepository&lt;FormLayout&gt; _formLayoutRepository;
    
    public FormLayoutAppService(IRepository&lt;FormLayout&gt; formLayoutRepository)
    {
         _formLayoutRepository = formLayoutRepository;
    }
    

    I didn't change any things, just create the new table:(

  • User Avatar
    0
    winson created

    I found that if I use a repository to inject the app service, and it's working:

    private readonly IFormLayoutRepository _formLayoutRepository;
    
            public FormLayoutAppService(IFormLayoutRepository formLayoutRepository)
            {
                _formLayoutRepository = formLayoutRepository;
            }
    

    but why it must be use Repository? I have no other custom method to use, so if I create the Repository for each table, these Repositories are empty, and I have to create an interface and implements it, but I just want to use the simple generic methods:

    IRepository<MyModel>
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    It should work as you expected. Can you share your FormLayout entity definition ?

  • User Avatar
    0
    winson created

    Yes, after I used the Repository and it's working, below is my entity, there is no any especially:

    public class FormLayout : Entity, IModificationAudited, ICreationAudited
    {
        public virtual bool Active { get; set; }
    
        [Required]
        public virtual int CateTabId { get; set; }
    
        [Required]
        public virtual string Name { get; set; }
    
        [Required]
        public virtual string Value { get; set; }
    
        public DateTime CreationTime { get; set; }        
    
        public DateTime? LastModificationTime { get; set; }
    
        public long? LastModifierUserId { get; set; }
    
        public long? CreatorUserId { get; set; }
    
        [ForeignKey("CreatorUserId")]
        public virtual AdminUser CreatorUser { get; set; }
    
        [ForeignKey("LastModifierUserId")]
        public virtual AdminUser LastModifierUser { get; set; }
    
        [ForeignKey("CateTabId")]
        public virtual CategoryTab CateTab { get; set; }
    }