我有一个 WCF 服务,它的实例模式为 Single,并发模式为 Multiple。我有以下代码:
public static ICmsDataServiceWcf data
{
get
{
if (HttpContext.Current != null && HttpContext.Current.Session != null && HttpContext.Current.Session["DataService"] == null)
{
HttpContext.Current.Session.Add("DataService", GetDataService());
}
if (HttpContext.Current != null && HttpContext.Current.Session != null && HttpContext.Current.Session["DataService"] != null)
{
return (ICmsDataServiceWcf)HttpContext.Current.Session["DataService"];
}
return GetDataService();
}
}
private static ICmsDataServiceWcf GetDataService()
{
string url = ConfigurationManager.AppSettings["service_url"];
EndpointAddress endPoint = new EndpointAddress(url);
var binding = new WSHttpBinding(SecurityMode.None);
CachedWebServiceChannelFactory<ICmsDataServiceWcf> cf = new CachedWebServiceChannelFactory<ICmsDataServiceWcf>(binding, endPoint);
var channel = cf.CreateChannel();
return channel;
}
这个想法是每个客户端都有自己的 WCF 客户端,它只会阻止他们的请求,我不必承受多次创建/销毁客户端的开销。
最近我收到了一些“服务太忙”的异常。这些客户中的大多数大部分时间都处于闲置状态。空闲的客户端是否仍然消耗服务器上的资源?它的实例是否以某种方式在服务器端持续存在?
谁能看到这可能导致问题的原因?(除了让大量客户坐在那里直到会话被放弃的内存浪费 - 我正在考虑使用一个客户池并定期剔除不活动/错误的客户。)
谢谢,
乔
编辑:我忘了提到我已经实现了自己的代理 - 不确定这是否会产生任何影响:
public class CachedWebServiceProxy<T> : RealProxy
{
private Type _typeOfProxy;
public object _channel;
public CachedWebServiceProxy(Type typeOfProxy, object channel)
: base(typeOfProxy)
{
_typeOfProxy = typeOfProxy;
_channel = channel;
}
public override System.Runtime.Remoting.Messaging.IMessage Invoke(System.Runtime.Remoting.Messaging.IMessage msg)
{
try
{
var methodCall = msg as IMethodCallMessage;
var methodInfo = methodCall.MethodBase as MethodInfo;
object result = null;
// Caching code removed
result = methodInfo.Invoke(_channel, methodCall.Args);
return new ReturnMessage(result, null, 0, methodCall.LogicalCallContext, methodCall);
}
catch (Exception ex)
{
// Throw the actual error fro mthe webservice
if (ex is TargetInvocationException && ex.InnerException != null)
{
throw ex.InnerException;
}
throw ex;
}
}
}