Search code examples
c#wcfcastle-windsorwcffacility

Using PerWcfSession lifestyle with Castle WCF Integration Facility


The following code uses the Castle Windsor 3.0's WCF Integration Facility to register a WCF self-hosted service:

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;

using Castle.Facilities.WcfIntegration;
using Castle.MicroKernel.Registration;
using Castle.Windsor;

namespace SelfHost
{
    [ServiceContract]
    public interface IHelloWorldService
    {
        [OperationContract]
        string SayHello(string name);
    }

    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class HelloWorldService : IHelloWorldService
    {
        private readonly PerSession _perSession;

        public HelloWorldService(PerSession perSession)
        {
            _perSession = perSession;
        }

        public string SayHello(string name)
        {
            return string.Format("Hello, {0} {1}", name, _perSession.Info());
        }
    }

    public class PerSession
    {
        private readonly string _now;

        public PerSession()
        {
            _now = DateTime.Now.ToString();
        }

        public string Info()
        {
            return _now;
        }
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            Uri baseAddress = new Uri("http://localhost:8080/hello");

            var container = new WindsorContainer();

            container.AddFacility<WcfFacility>();

            container.Register(
                Component.For<PerSession>().LifeStyle.PerWcfSession(),
                Component.For<IHelloWorldService>()
                    .ImplementedBy<HelloWorldService>()
                    .AsWcfService(
                        new DefaultServiceModel()
                            .AddBaseAddresses(baseAddress)
                            .AddEndpoints(WcfEndpoint.BoundTo(new BasicHttpBinding()).At("basic"))
                            .PublishMetadata(o => o.EnableHttpGet())
                    )
                );

            Console.WriteLine("The service is ready at {0}", baseAddress);
            Console.WriteLine("Press <Enter> to stop the service.");
            Console.ReadLine();
        }
    }
}

Trying to invoke the SayHello method using WcfTestClient.exe results in the following error:

Could not obtain scope for component SelfHost.PerSession. This is most likely either a bug in custom IScopeAccessor or you're trying to access scoped component outside of the scope (like a per-web-request component outside of web request etc)

What is the correct way to use PerWcfSession components?


Solution

  • So I was missing a few things:

    The ServiceContract needs to set the SessionMode property

    [ServiceContract(SessionMode = SessionMode.Required)]
    

    Likewise the ServiceBehavior needs to set the InstanceContextMode

    [ServiceBehavior(IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.PerSession)]
    

    Finally, the service registration needs to change the Lifestyle from the default (Singleton) so that it gets recreated for each request (and the dependencies are re-evaluated) - Transient or PerWcfSession would work.

    Also, because we require a session, the binding needs to change from the basicHttpBinding to something that that supports sessions:

    Component.For<IHelloWorldService>()
        .ImplementedBy<HelloWorldService>()
        .LifestyleTransient()
        .AsWcfService(
            new DefaultServiceModel()
                .AddBaseAddresses(baseAddress)
                .AddEndpoints(WcfEndpoint.BoundTo(new WSHttpBinding()).At("myBinding"))
                .PublishMetadata(o => o.EnableHttpGet())
        )
    

    Which makes the final code look like this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.ServiceModel;
    
    using Castle.Facilities.WcfIntegration;
    using Castle.MicroKernel.Registration;
    using Castle.Windsor;
    
    namespace SelfHost
    {
        [ServiceContract(SessionMode = SessionMode.Required)]
        public interface IHelloWorldService
        {
            [OperationContract]
            string SayHello(string name);
        }
    
        [ServiceBehavior(IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.PerSession)]
        public class HelloWorldService : IHelloWorldService
        {
            private readonly PerSession _perSession;
    
            public HelloWorldService(PerSession perSession)
            {
                _perSession = perSession;
            }
    
            public string SayHello(string name)
            {
                return string.Format("Hello, {0} {1}", name, _perSession.Info());
            }
        }
    
            public class PerSession
            {
                private readonly string _now;
    
                public PerSession()
                {
                    _now = DateTime.Now.ToString();
                }
    
                public string Info()
                {
                    return _now;
                }
            }
    
        internal class Program
        {
            private static void Main(string[] args)
            {
                Uri baseAddress = new Uri("http://localhost:8080/hello");
    
                var container = new WindsorContainer();
    
                container.AddFacility<WcfFacility>();
    
                container.Register(
                    Component.For<PerSession>().LifeStyle.PerWebRequest,
                    Component.For<IHelloWorldService>()
                        .ImplementedBy<HelloWorldService>()
                        .LifeStyle.PerWebRequest
                        .AsWcfService(
                            new DefaultServiceModel()
                                .AddBaseAddresses(baseAddress)
                                .AddEndpoints(WcfEndpoint.BoundTo(new WSHttpBinding()).At("myBinding"))
                                .PublishMetadata(o => o.EnableHttpGet())
                        )
                    );
    
                Console.WriteLine("The service is ready at {0}", baseAddress);
                Console.WriteLine("Press <Enter> to stop the service.");
                Console.ReadLine();
            }
        }
    }