1
我試圖創建RealProxy以實現一些緩存和一些其他功能。我檢查了GetTransparentProxy返回的類型,它看起來是正確的,但是如果我在它上調用InvokeMember,那麼代理最終會自行調用,並且會發生堆溢出。請有人指出我做錯了什麼?RealProxy的實現以無限循環結束
public class CachedWebServiceProxy<T> : RealProxy
{
private Type _typeOfProxy;
public CachedWebServiceProxy(Type typeOfProxy) : base(typeOfProxy)
{
_typeOfProxy = typeOfProxy;
}
public override System.Runtime.Remoting.Messaging.IMessage Invoke(System.Runtime.Remoting.Messaging.IMessage msg)
{
var methodCall = msg as IMethodCallMessage;
var methodInfo = methodCall.MethodBase as MethodInfo;
var proxy = GetTransparentProxy();
var result = _typeOfProxy.InvokeMember(methodCall.MethodName, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, proxy, methodCall.Args);
return new ReturnMessage(result, null, 0, methodCall.LogicalCallContext, methodCall);
}
}
class CachedWebServiceChannelFactory<T> : ChannelFactory<T>
{
public CachedWebServiceChannelFactory(Binding binding, EndpointAddress endpoint) : base(binding, endpoint)
{ }
public CachedWebServiceChannelFactory(string endpointConfigurationName)
: base(endpointConfigurationName)
{ }
public override T CreateChannel(EndpointAddress address, Uri via)
{
var extendedProxy = new CachedWebServiceProxy<T>(typeof(T));
return (T)extendedProxy.GetTransparentProxy();
}
}
如果你解決它自己,你可以(也應該)發佈您的解決方案作爲一個答案,而不是編輯的問題。 – Flexo