Base solution for your next web application
Open Closed

injection of replacement service in ApplicationModule #8646


User avatar
0
BobIngham created

.NET Core, Angular, ASPNET Framework 4.6.0, Zero 6.8.0 I want to implement my own StripePaymentAppService. My conventions are to leave alone the Zero implementation, copy the code and then implement my own. Create new service: NuagecareStripePaymentAppService, copied from StripePaymentAppService and modified: Create new interface, INuagecareStripePaymentAppService, copied direct from IStripePaymentAppService Then, in [ProjectName].Application.[ProjectName]ApplicationModule inject the new service using the following code:

//Replace StripePaymentAppService with NuagecareStripePaymentAppService
Configuration.ReplaceService(typeof(IStripePaymentAppService), () =>
{
    Configuration.IocManager.IocContainer.Register(
        Component.For<IStripePaymentAppService, INuagecareStripePaymentAppService>()
                    .ImplementedBy<NuagecareStripePaymentAppService>()
                    .LifestyleTransient()
    );
});

However, I get an error in Visual Studio: What am I doing worong and what's best practise here?


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

    The error message is clear.

    To replace the service for IStripePaymentAppService, you have to implement the interface. In your case, make INuagecareStripePaymentAppService inherit IStripePaymentAppService.

  • User Avatar
    0
    BobIngham created

    Hi @Aaron, good to see you in the forum, my friend. Your answer is correct, though the solution is not obvious, basically I have to create a n interface which looks as follows:

    using Abp.Application.Services;
    using Nuagecare.MultiTenancy.Payments.Stripe;
    
    namespace Nuagecare.App.MultiTenancy.Payments.Stripe
    {
        public interface INuagecareStripePaymentAppService : IApplicationService, IStripePaymentAppService
        {
        }
    }
    

    And then inherit from my service like so:

    public class NuagecareStripePaymentAppService : NuagecareAppServiceBase, INuagecareStripePaymentAppService
    

    That may look obvious to guys like your self but it's very odd to me! Thanks for the pointer.

  • User Avatar
    0
    aaron created
    Support Team

    Happy to help!

    By the way, you don't need to create another interface when doing ReplaceService unless you are extending the interface. Also note that IStripePaymentAppService already inherits IApplicationService so you don't have to inherit that again.

  • User Avatar
    0
    BobIngham created

    Hi @Aaron, Nice to learn from those with better knowledge. In this instance I may need to extend the interface because we are not charging by edition but by the number of devices connected to the system. We are using Stripe in Zero only to create a new account (CreateSubscription()) when a trial period ends or when a tenant chooses to upgrade a trial. Our actual charging is done by an Azure function app which reads the db directly. That way we can use variable pricing for each tenant. We may need to extend the interface to pull back info from Stripe to display on the tenant's subscription dashboard. Hope that makes sense, and thanks for the help.