インスタンス モードが Single で同時実行モードが Multiple の WCF サービスがあります。次のコードがあります。
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;
}
}
}