.NET Remoting: создание своего RealProxy на сервере
От: Аноним  
Дата: 28.08.07 11:42
Оценка:
Я создаю свой класс унаследованный от ProxyAttribute и свой RealProxy. С классом помеченным этим атрибутом возникают следующие проблемы:
    Метод CreateInstance атрибута вызывает на клиенте, а не на сервере.
    Если поставить точку останова в методе Invoke CustomProxy, то можно заметить, что CustomProxy ловит только сообщение от конструктора, когда же на клиенте вызывается метод GetAppName объекта, он выполняется уже не через мой прокси.

Почему так происходит и что сделать, чтобы работало так как мне нужно?

Спасибо.

Исходный код:
Класс CustomProxy:
public class CustomProxy: RealProxy
    {
        private MarshalByRefObject _obj;
        private Type _type;

        #region Constructors

        public CustomProxy()
        {
        }

        public CustomProxy(Type classToProxy)
            : base(classToProxy)
        {
            _type = classToProxy;
        }

        public CustomProxy(Type classToProxy, MarshalByRefObject obj)
            : base(classToProxy)
        {
            _obj = obj;
        }

        #endregion

        public override IMessage Invoke(IMessage msg)
        {
            IMethodReturnMessage retMsg = null;
            if (msg is IConstructionCallMessage)
            {
                retMsg = InitializeServerObject((IConstructionCallMessage) msg);
                _obj = GetUnwrappedServer();
            }
            else if (msg is IMethodCallMessage)
            {
                retMsg = (IMethodReturnMessage) 
                    RemotingServices.GetEnvoyChainForProxy(_obj).SyncProcessMessage(msg);
            }
            return retMsg;
        }
    }

Класс CustomProxyAttribute:
[AttributeUsage(AttributeTargets.Class)]
    public class CustomProxyAttribute: ProxyAttribute
    {
        public override MarshalByRefObject CreateInstance(Type serverType)
        {
            CustomProxy proxy = new CustomProxy(serverType);
            return (MarshalByRefObject)proxy.GetTransparentProxy();
        }

        public override RealProxy CreateProxy(ObjRef objRef, Type serverType, object serverObject, Context serverContext)
        {
            return new CustomProxy(serverType);
        }
    }

Класс Test:
[CustomProxy]
    public class Test : ContextBoundObject
    {
        private Generator _generator;

        public Test()
        {
            _generator = new Generator();
        }

        public string GetAppName()
        {
            return AppDomain.CurrentDomain.FriendlyName;
        }
    }

Клиентский код:
        static void Main(string[] args)
        {
            RemotingConfiguration.Configure("Client.exe.config", false);

            Test test = new Test();
            Console.WriteLine(test.GetAppName());
        }

Client.exe.config:
  <system.runtime.remoting>
    <application name="Client">
      <client>
        <wellknown 
          type="TestObject.Test, TestObject"
          url="tcp://localhost:8000/Server/Test.rem"/>
      </client>
    </application>
  </system.runtime.remoting>
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.