使用PerWcfSession生活方式与Castle WCF集成工具

时间:2012-06-13 21:21:31

标签: c# wcf castle-windsor wcffacility

以下代码使用Castle Windsor 3.0的WCF Integration Facility注册WCF自托管服务:

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();
        }
    }
}

尝试使用WcfTestClient.exe调用SayHello方法会导致以下错误:

  

无法获取组件SelfHost.PerSession的范围。这是最多的   可能是自定义IScopeAccessor中的错误,或者您正在尝试   访问范围之外的作用域组件(如每个Web请求   Web请求之外的组件等)

使用PerWcfSession组件的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

所以我错过了一些事情:

ServiceContract需要设置SessionMode属性

[ServiceContract(SessionMode = SessionMode.Required)]

同样,ServiceBehavior需要设置InstanceContextMode

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

最后,服务注册需要从默认(Singleton)更改Lifestyle,以便为每个请求重新创建(并重新评估依赖项) - Transient或PerWcfSession可以工作。

另外,因为我们需要一个会话,所以绑定需要从basicHttpBinding更改为支持会话的内容:

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

这使得最终代码看起来像这样:

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();
        }
    }
}