3

以下代码使用 Castle Windsor 3.0 的WCF 集成工具来注册 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 组件的正确方法是什么?

4

1 回答 1

2

所以我错过了一些东西:

ServiceContract 需要设置 SessionMode 属性

[ServiceContract(SessionMode = SessionMode.Required)]

同样 ServiceBehavior 需要设置 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();
        }
    }
}
于 2012-06-14T00:32:43.657 回答