Base solution for your next web application
Open Closed

Resolve dependency for AppService #2962


User avatar
0
chauey created

I have a AppService name with "PaymentProcessAppService" that is working fine with the angular app. Now i need to initialize the PaymentProcessAppService in quartz job execute method. I am using IRepository<PaymentCardDetail> in PaymentProcessAppService.

I am trying to resolve the dependency with this code

public void Execute(IJobExecutionContext context)
        {
            var container = new WindsorContainer();
            container.Register(Component.For<IPaymentProcessAppService>().ImplementedBy<PaymentProcessAppService>().LifestyleTransient());

            IPaymentProcessAppService paymentProcessAppService = container.Resolve<IPaymentProcessAppService>();
            paymentProcessAppService.CheckSubscriptionStatus();
        }

it give me error:

Castle.MicroKernel.Handlers.HandlerException: 'Can't create component 'Ncg.Payment.PaymentProcessAppService' as it has dependencies to be satisfied.



'Ncg.Payment.PaymentProcessAppService' is waiting for the following dependencies:

How can i resolve the dependency ? I need to initialize my app service only and call a function.


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

    It's obvious that you have only registered PaymentProcessAppService but not registered it's dependencies. But you should never create a new WindsorContainer normally, so your code is completely wrong :)

    If you need to resolve a dependency just use IIocResolver. Inject IIocResolver into your background job (in it's constructor) and use it's Resolve method. If you can not inject IIocResolver into a class you can use IocManager.Instance static instance, like IocManager.Instance.Resolve<IPaymentProcessAppService>().

    BTW, we don't suggest to use application services in your background jobs, because app services may have validation and authorization which is not valid in a background job context (they are valid in a HTTP request context). Instead, extract reused login into a domain service or another type of service/class and use it both from app service and background job.

  • User Avatar
    0
    chauey created

    Thanks for your answer. The appService doesn't have authorization and validation.As per your documentation, if we use the proper naming conventions with post-fix of "AppService" and inherit IApplicationService then we don't need to register the AppService, So i think i don't need to register my service for DI.

    As you told me, I tried with the IocManager.Instance.Resolve<IPaymentProcessAppService>().

    but i am failed to initialize the implementation class of IPaymentProcessAppService

    <ins>following is my code</ins>

    public void Execute(IJobExecutionContext context)
        {
               var obj = IocManager.Instance.Resolve<IPaymentProcessAppService>();
        }
    

    <ins>Exception:</ins>

    Message: Castle.MicroKernel.ComponentNotFoundException: No component for supporting the service Ncg.Payment.IPaymentProcessAppService was found

    Inner Exception: null

    Stack Trace: at Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(Type service, IDictionary arguments, IReleasePolicy policy) at Castle.MicroKernel.DefaultKernel.Resolve(Type service, IDictionary arguments) at Castle.Windsor.WindsorContainer.ResolveT at Abp.Dependency.IocManager.ResolveT at Ncg.NcgQuartz.NcgJob.Execute(IJobExecutionContext context) at Quartz.Core.JobRunShell.Run()

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Have you added PaymentCardDetail entity to DbContext ? I think your original problem might be related to that. Can you also share definitions of IPaymentProcessAppService and PaymentProcessAppService ?

    Also do you use this IPaymentProcessAppService in a different module or is it in your WebModule or AppModule ?

    Thanks.

  • User Avatar
    0
    chauey created

    Below is the Interface of my AppService along with class that is implementing the interface

    public interface IPaymentProcessAppService : IApplicationService
        {
            Task<CommonTypeResult> SomeFunc();
       }
    
     public class PaymentProcessAppService : NcgAppServiceBase, IPaymentProcessAppService
     {
            private readonly IRepository<PaymentCardDetail> _paymentCardDetailRepository;
            private readonly IRepository<StripeTransaction> _stripeTransactionRepository;
            private readonly IStripper stripper;
            private int _tenantID, _userID;
            private CommonTypeResult _commonTypeResult = null;
            public PaymentProcessAppService(IRepository<PaymentCardDetail> paymentCardDetailRepository, 
            IRepository<StripeTransaction> stripeTransactionRepository)
            {
                stripper = new Stripper();
                _paymentCardDetailRepository = paymentCardDetailRepository;
                _stripeTransactionRepository = stripeTransactionRepository;
            }
    
            public async Task<CommonTypeResult> SomeFunc()
            {
                // some code
            }
    }
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you answer these questions as well ?

    Have you added PaymentCardDetail entity to DbContext ? I think your original problem might be related to that.

    Also do you use this IPaymentProcessAppService in a different module or is it in your WebModule or AppModule ?

    Thanks.

  • User Avatar
    0
    chauey created

    Hello,

    <ins>1)"Have you added PaymentCardDetail entity to DbContext ? I think your original problem might be related to that."</ins>

    Yes ,

    public virtual IDbSet<PaymentCardDetail> PaymentCardDetails { get; set; }
    

    <ins>2) "Also do you use this IPaymentProcessAppService in a different module or is it in your WebModule or AppModule ?</ins>

    No, PaymentProcessAppService is working fine when call it from my angular app, there is no problem with the service. I just want to resolve the dependency to call it directly in quartz Job Execute method.

    I did my quartz with task by directly accessing to dbset property of entity, but i think that is not the proper way to do that as per your architecture.

    Thank You

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    In which project do you start Quartz jobs ? Normally it is better to use domain services in background jobs but your App Service should be resolved by DI.

    Thanks.

  • User Avatar
    0
    chauey created

    My Job create and trigger in startup method of Web.Host project.

    And Job class with execute method is in Application Project.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    If it is not a problem for you, can you share your project with us ? You can send it to <a href="mailto:[email protected]">[email protected]</a>.

    We will take a look at it.

    Thanks.