当前位置:开发者网络 >> 技术教程 >> .NET教程 >> 远程及网络应用 >> 内容
精彩推荐
分类最新教程
分类热点教程
  
Using the Remoting Callbacks in .Net Applications
作者:未知
日期:2003-07-12
人气:
投稿:Andy.m(转贴)
来源:未知
字体:
收藏:加入浏览器收藏
以下正文:
Remoting Interface

Interface is a contract between the object and its consumer. The interfaces allow to publish an object based on their abstract definitions. This encapsulation is giving a more flexibility in the design implementation. The major advantage using interfaces (or abstract class) is their loosely design pattern, which it may play an important role in the remoting issue. It is a good design technique to put all common abstract definitions  into one assembly. In my sample I created separate project called a ContractObject for that issue.

The following is an abstract definition of the remote object contract:

namespace RKiss.RemoteObject
{
   // stateless delegate (it can be any generic signature)
   public delegate bool RemoteCallback(string sender, object e);

    
   // interface
   [Guid("B19A2AD2-31F2-4c6e-B5A6-24495670BE02")]
   public interface IRmObject
   {
      string GiveMeCallback(int timeinsec, string ticketId, object wire);
      string Echo(string msg);
   }

    
   // Callback EventArgs class
   [Serializable]
   public class CallbackEventArgs : EventArgs
   {
      private string _sender;
      private string _ticketId;
      private object _state;
      public string Sender
      {
         get { return _sender; }
         set { _sender = value;  }
      }
      public string TicketId
      {
         get { return _ticketId; }
         set { _ticketId = value;  }
      }
      public object State
      {
         get { return _state;  }
         set { _state = value; }
      }
   }
}


There are three parts of the metadata in its assembly: delegator, interface and callback's EventArg class. They can be modified based on needs of the application. Note that interface has been attributed by Guid value to keep its ID the same (The .Net Service will be accepted this Guid each time when object is going to be re-registered into the COM+ catalog)



Callback Custom Attribute

For setup a config of the Remote Callback object on the client side is suitable to use a custom attribute technique. It will allow to hide and reuse all implementation for this private remoting object. I created a separate project - RemoteCallbackLib to handle this solution.  There is a RemoteCallbackAttribute  to config any attributed object (of course derived from the MarshalByRefObject class) for the Remoting purpose.

The following code snippet shows its usage:


[RemoteCallback("tcp", desc="Callbacks Test")]
public CallbackClass cb = null;
// callback Admin private RemoteCallbackSubscriber sub = null;  
The RemoteCallback attribute is activated by its subscriber - RemoteCallbackSubscriber, which has to be constructed during the client's initialization:

sub = new RemoteCallbackSubscriber(this);
cb.Parent = this;    // we need an access to the parent properties




Remoting Method
The method signature of the Remoting object which wants to use the Remoting Callback mechanism includes less two additional arguments such as the  ticketId and objwire as it is shown in the following snippet:  


public string GiveMeCallback(int timeinsec, string ticketId, object objwire)


The ticketId represents a cookie value to handle multiple callbacks on the client side. The objwire is a delegate object of the client's callback proxy.



相关文章: