Search code examples
wcfcastle-windsorwindsor-3.0windsor-facilities

Windsor Castle 3.0 use IContributeComponentModelConstruction to set lifestyle to WcfPerOperation


In Windsor 2.1, I have the following piece of code to change lifestyle of all services to PerWcfOperation when the code is executed in wcf context:

    container.Kernel.ComponentModelBuilder.AddContributor(
            new CustomLifestyleLevelingContributeComponentModelConstruction(typeof (PerWcfOperationLifestyle))

where CustomLifestyleLevelingContributeComponentModelConstruction is:

public class CustomLifestyleLevelingContributeComponentModelConstruction : IContributeComponentModelConstruction
{
    private readonly Type customLifestyleType;
    private readonly List<LifestyleType> ignoredLifetyles;

public CustomLifestyleLevelingContributeComponentModelConstruction(Type customLifestyleType)
    {
        this.customLifestyleType = customLifestyleType;
    }
    public void ProcessModel(IKernel kernel, ComponentModel model)
    {
        model.LifestyleType = LifestyleType.Custom;
        model.CustomLifestyle = customLifestyleType;
    }
}

My problem is that the class PerWcfOperationLifestyle has been removed from Windsor 3.0. Could anyone please show me how I can achieve the same goal with Windsor 3.x?


Solution

  • I solved this problem by rewriting the ProcessModel method as the below:

    public void ProcessModel(IKernel kernel, ComponentModel model)
        {
                model.LifestyleType = LifestyleType.Scoped;
                model.ExtendedProperties[Constants.ScopeAccessorType] = typeof(WcfOperationScopeAccessor);
        }