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();
}
}
直接到内存益出错误,另外怎么设置客户端的反序列化个数
scope没释放,scope必须要释放
对5000的循环进行不同的代码屏蔽试试,看看是哪个引起的,估计就可以定位到问题
AccountSrv.GetList();
调一次增加一次
var scope = new OperationContextScope(((IClientChannel)proxy));
scope 没释放引起的
试试 using (var AccountSrv = factory.CreateChannel())