首页 新闻 会员 周边 捐助

wcf 调用内存只增不减

0
悬赏园豆:200 [已解决问题] 解决于 2013-12-17 09:21

xp + vs2010 .net4.0

              Tuple<EndpointAddress, WSHttpBinding> tuple = ServiceLocator.GetIt<IAccountService>();
                for (int i = 0; i < 5000; i++)
                {
                    using (var factory =new  ChannelFactory<IAccountService>(tuple.Item2,tuple.Item1))
                    {
                        var AccountSrv = factory.CreateChannel();
                        using (AccountSrv as IDisposable)
                        {
                            ServiceLocator.AttachHeader(AccountSrv);//添加username到soap头
                            var list = AccountSrv.GetList();
                            Console.WriteLine("i:" + i + "," + list.Count);
                            (AccountSrv as ICommunicationObject).Close();
                           
                        }
                        (factory as ChannelFactory).Close();
                    }
                   
                }

--------------

    public class ServiceLocator
    {


        public static  Tuple<EndpointAddress, WSHttpBinding>  GetIt<T>()
        {
           EndpointAddress serviceAddress;
           WSHttpBinding binding;
            var surl = ConfigurationManager.AppSettings["ServiceURL"];
            var iname = typeof(T).FullName.Replace(".I",".");
            var sname = iname.Replace(".", "-");
            var url = Path.Combine(surl, sname);
            serviceAddress = new EndpointAddress(url);

          
            binding = new WSHttpBinding(SecurityMode.None);
            binding.CloseTimeout = new TimeSpan(10, 10, 10);
            binding.MaxReceivedMessageSize = 999999;
            binding.MaxBufferPoolSize = 99999;
            binding.ReaderQuotas.MaxArrayLength = 999999;
            binding.OpenTimeout = new TimeSpan(10, 10, 10);
            binding.SendTimeout = new TimeSpan(10, 10, 10);
            binding.ReceiveTimeout = new TimeSpan(10,10,10);
            binding.AllowCookies = true;
            return new Tuple<EndpointAddress, WSHttpBinding>( serviceAddress,  binding );
        }

        public static void AttachHeader(object proxy)
        {
            var scope = new OperationContextScope(((IClientChannel)proxy));


            var curId = UserSetting.LoginId;
            MessageHeader<String> mhg = new MessageHeader<String>(curId);
            MessageHeader untyped = mhg.GetUntypedHeader("loginId", "ns");
            OperationContext.Current.OutgoingMessageHeaders.Add(untyped);
        }
        public static IService Get<IService>()
        {
            var type = typeof(IService);
            if (!ServiceDic.ContainsKey(type))
            {
                ServiceDic.Add(type, new ClientFactory<IService>());
            }
            var factory = (ClientFactory<IService>)ServiceDic[type];

            //var cObj =client as ICommunicationObject;
            //if (cObj.State == CommunicationState.Faulted
            //    || cObj.State == CommunicationState.Closed
            //    || cObj.State == CommunicationState.Closing

            //    )
            //{
            //   client= new ClientFactory<IService>().CreateClient();
            //   ServiceDic[type] = client;
            //}


            return factory.CreateClient();
        }
    }

直接到内存益出错误,另外怎么设置客户端的反序列化个数

遗忘海岸的主页 遗忘海岸 | 菜鸟二级 | 园豆:253
提问于:2013-12-16 08:21
< >
分享
最佳答案
0

scope没释放,scope必须要释放

收获园豆:192
winform | 菜鸟二级 |园豆:394 | 2013-12-17 09:20
其他回答(3)
0

对5000的循环进行不同的代码屏蔽试试,看看是哪个引起的,估计就可以定位到问题

收获园豆:5
2012 | 园豆:21445 (高人七级) | 2013-12-16 09:16

 AccountSrv.GetList();

调一次增加一次

支持(0) 反对(0) 遗忘海岸 | 园豆:253 (菜鸟二级) | 2013-12-16 09:50
0

var scope = new OperationContextScope(((IClientChannel)proxy));

scope 没释放引起的

遗忘海岸 | 园豆:253 (菜鸟二级) | 2013-12-16 19:47
0

试试  using (var AccountSrv = factory.CreateChannel())

收获园豆:3
gunsmoke | 园豆:3592 (老鸟四级) | 2013-12-17 03:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册