castle-windsorwcffacility

WcfFacility: controlling service lifestyle


Registration example:

        container.Register(Component.For<IFooService>().ImplementedBy<FooService>().AsWcfService<IFooService>(new DefaultServiceModel().Hosted()).LifestyleTransient());

.svc:

<%@ServiceHost language="C#" Debug="true" Service="Service.FooService" 
Factory="Castle.Facilities.WcfIntegration.DefaultServiceHostFactory, Castle.Facilities.WcfIntegration" %>

The WCF is hosted in IIS and when I hit the endpoint using WcfStorm the ctor is not called upon every request. If I regenerate the client-side proxy the ctor is called once again. How can the service lifecycle be coupled to the client side proxy?


Solution

  • Transient will behave like Transient should, meaning that Castle.Windsor will create a new service instance upon request. The confusion came from the fact that this did not happen every time the service was hit. The reason for this is that the WCF stack has its own idea of service lifetime, defined by the "InstanceContextMode" which default to "PerSession". If I changed the InstanceContextMode to "PerCall", Castle will comission/decomission the services as (I) expected.

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class FooService : IFooService { .. }