选择WCF实例管理

时间:2013-04-21 14:43:31

标签: wcf

我们正在IIS上设计一个具有以下特征的WCF Web服务:

  • 对象创建相对较重(大约需要500毫秒),因为它涉及编写文件
  • 创建对象后不需要保留任何状态
  • 来自客户端的每次呼叫平均需要150 - 200 ms。此调用涉及向另一台服务器发送UDP请求并接收响应。
  • 我们预计会有大约30个客户。它可能会增长到50个客户。在最坏的情况下(50个客户端),我们需要一个10个对象实例的池来处理这个负载。

您会推荐3个实例管理上下文中的哪一个(PerCall,PerSession,Single),为什么?哪个实例使我们能够管理可以完成工作的可用对象池?

1 个答案:

答案 0 :(得分:3)

开箱即用,WCF不支持服务对象池。您需要自定义IInstanceProvider。在这种情况下,WCF上下文模式将定义WCF何时从IInstanceProvider请求新对象,而IInstanceProvider行为将管理池。根据使用情况,将服务设置为PerInstance或PerSession可能有意义。

如果您在实施中使用依赖注入容器,例如Castle WindsorStructureMap或MS Enterprise Library的Unity,那么您可以使用容器的现有IInstanceProvider与池化生活方式。所有这些容器都是合理的(尽管我个人对管理对象池没有多少经验)。

我个人选择的容器是Castle Windsor。在这种情况下,您将使用Windsor的WCF Integration Facilitypooled lifestyle

这是一个使用Castle.Facilites.WcfIntegraion NuGet包的快速测试控制台程序。

using Castle.Facilities.WcfIntegration;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Threading.Tasks;

namespace WCFWindsorPooledService
{
    [ServiceContract]
    public interface ISimple
    {
        [OperationContract]
        void Default(string s);
    }

    public class SimpleService : ISimple
    {
        private static int CurrentIndex = 0;
        private int myIndex;

        public SimpleService()
        {
            // output which instance handled the call.
            myIndex = System.Threading.Interlocked.Increment(ref CurrentIndex);
        }

        public void Default(string s)
        {
            Console.WriteLine("Called #" + myIndex);
            System.Threading.Thread.Sleep(5);
        }
    }

    class PooledService
    {
        static void Main(string[] args)
        {
            Console.WriteLine("\n\n" + System.Reflection.MethodInfo.GetCurrentMethod().DeclaringType.Name);

            // define mapping of interfaces to implementation types in Windsor container.
            IWindsorContainer container = new WindsorContainer();
            container.AddFacility<WcfFacility>()
                     .Register(Component.For<SimpleService>().LifeStyle.PooledWithSize(2, 5));

            var host = new Castle.Facilities.WcfIntegration.DefaultServiceHostFactory()
                                                           .CreateServiceHost(typeof(SimpleService).AssemblyQualifiedName,
                                                                              new Uri[] { new Uri("http://localhost/Simple") });
            host.Open();

            ChannelFactory<ISimple> factory = new ChannelFactory<ISimple>(host.Description.Endpoints[0]);

            List<Task> tasks = new List<Task>();
            for (int i = 0; i < 20; i++)
            {
                tasks.Add(Task.Run(() =>
                {
                    ISimple proxy = factory.CreateChannel();
                    proxy.Default("Hello");

                    ((ICommunicationObject)proxy).Shutdown();
                }));
            }

            Task.WaitAll(tasks.ToArray());

            ((ICommunicationObject)host).Shutdown();
            container.Dispose();
        }
    }

    public static class Extensions
    {
        static public void Shutdown(this ICommunicationObject obj)
        {
            try
            {
                obj.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Shutdown exception: {0}", ex.Message);
                obj.Abort();
            }
        }
    }
}

我不会假装了解Castle如何管理游泳池的所有规则,但显然正在使用游泳池。输出是:

PooledService
Called #1
Called #5
Called #2
Called #3
Called #4
Called #6
Called #7
Called #8
Called #7
Called #4
Called #2
Called #5
Called #1
Called #10
Called #6
Called #9
Called #4
Called #7
Called #1
Called #9