Search code examples
c#asp.netdependency-injectioncastle-windsoraspnetboilerplate

How to achieve Property Injection like IAbpSession?


I am trying to implement my own custom IAbpSession, but I do not know how to achieve the same property injection as IAbpSession. My AppServiceBase is like below:

public abstract class EdwardAppServiceBase : ApplicationService
{
    public TenantManager TenantManager { get; set; }

    public UserManager UserManager { get; set; }

    protected EdwardActiveUnitOfWork EdwardActiveUnitOfWork => (EdwardActiveUnitOfWork)UnitOfWorkManager.Current;

    protected IEdwardSession EdwardSession { get; set; } 
    // protected IEdwardSession EdwardSession => AbpSession as IEdwardSession;

    protected SensingStoreCloudAppServiceBase()
    {
        // EdwardSession = NullEdwardSession.Instance;
        LocalizationSourceName = SensingStoreCloudConsts.LocalizationSourceName;
    }
}

If I uncomment EdwardSession = NullEdwardSession.Instance;, I always get NullEdwardSession instead of IEdwardSession implementation: EdwardSession.

I could only do AbpSession as IEdwardSession.

How does ABP inject IAbpSession and set its value in ApplicationService?


Solution

  • ABP uses Castle Windsor for dependency and property injection:

    How properties are injected

    Property injection of dependencies is designed to be done during component activation when a component is created. The responsibility of determining which properties are used for injection is fulfilled by default through PropertiesDependenciesModelInspector - a IContributeComponentModelConstruction implementation which uses all the following criteria to determine if a property represents a dependency:

    • Has 'public' accessible setter
    • Is an instance property
    • If ComponentModel.InspectionBehavior is set to PropertiesInspectionBehavior.DeclaredOnly, is not inherited
    • Does not have parameters
    • Is not annotated with the Castle.Core.DoNotWireAttribute attribute

    So make it public:

    // protected IEdwardSession EdwardSession { get; set; }
    public IEdwardSession EdwardSession { get; set; }