一个对象根据他所对应的类型(如System.MarshalByRefObject通过传递引用的方式)或者属性(比如对于定义了System. ", RemotingServices.IsTransparentProxy(marshalByRefObject) ? ", RemotingServices.IsTransparentProxy(marshalByRefObject) ? 而对于MarshalByRefType对象marshalByRefObject则返回True。这就充分证明了,标记了System. 由于marshalByRefObject,依赖于远程对象,所以当我们卸载掉Host远程对象的Application Domain,对marshalByRefObject的任何调用将变得无效。
也正是其来截机制本身的局限,当我们才用PIAB的方式进行对象的创建的时候,要求本创建对象的类型要么实现某一个接口,要么继承MarshalByRefObject类型。 但是当我们让抽象基类继承自MarshalByRefObject就不行了,我个人觉得这是微软需要改进的地方。 你也可以让Foo直接继承自MarshalByRefObject。 三、让FooBase继承MarshalByRefObject 上面我们说过,能被PIAB进行拦截的类型要么实现一个接口,要么继承MarshalByReObject类。 四、抽象类可以这样用 经过我的实验,抽象类可以这样用:将继承自MarshalByRefObject的具体类作为抽象类的基类。
(Inherited from MarshalByRefObject) Dispose() 释放 WebResponse 对象使用的非托管资源。 (Inherited from MarshalByRefObject) GetObjectData(SerializationInfo, StreamingContext) 使用序列化目标对象所需的数据填充 (Inherited from MarshalByRefObject) MemberwiseClone() 创建当前 Object 的浅表副本。 (Inherited from Object) MemberwiseClone(Boolean) 创建当前 MarshalByRefObject 对象的浅表副本。 (Inherited from MarshalByRefObject) ToString() 返回表示当前对象的字符串。
ProxyAttribute和RealProxy两个类,先来看下这两个类有什么作用 ProxyAttribute这个类用来截获对象的代理,我们只要能够替换代理,就能够在对象的初始化,方法调用的过程中加入自定义的操作,重写MarshalByRefObject 方法,进行代替替换 public override MarshalByRefObject CreateInstance(Type serverType) { realProxy = new SetMethodAopProxy(serverType); return realProxy.GetTransparentProxy() as MarshalByRefObject 只觉得自己通过路由器就能访问Google,具体请看下面的代码 class SetMethodAop : ProxyAttribute { public override MarshalByRefObject realProxy = new SetMethodAopProxy(serverType); return realProxy.GetTransparentProxy() as MarshalByRefObject
(Inherited from MarshalByRefObject) Dispose() 释放 WebResponse 对象使用的非托管资源。 (Inherited from MarshalByRefObject) GetObjectData(SerializationInfo, StreamingContext) 使用将目标对象序列化所需的数据填充 (Inherited from MarshalByRefObject) MemberwiseClone() 创建当前 Object 的浅表副本。 (Inherited from Object) MemberwiseClone(Boolean) 创建当前 MarshalByRefObject 对象的浅表副本。 (Inherited from MarshalByRefObject) ToString() 返回表示当前对象的字符串。 (Inherited from Object)
Invoke(this, "cnblogs"); } } public class F1 : MarshalByRefObject { public 建议新建的两个类是写在一个文件,而且需要让两个类继承 MarshalByRefObject 和接口 IRemoteEventHandle ,并且只允许本地的NativeEventHandle在构造传入远程的类 [Remote] [Serializable] public class RemoteEventHandle : MarshalByRefObject, IRemoteEventHandle 如下面的代码 [Serializable] public class RemoteEventHandle : MarshalByRefObject, IRemoteEventHandle 这个问题有最简单的例子,请看下面代码,保持远程的代码不变 public class NativeEventHandle : MarshalByRefObject, IRemoteEventHandle
按引用编组的类必须派生自 MarshalByRefObject。MarshalByRefObjects 称为应用程序域绑 定对象。 MarshalByRefObject 的一个专业化版本是 ContextBoundObject :抽象类 ContextBoundObject 派生自 MarshalByRefObject。 ● 不能用于远程通信的类——这种类不能序列化,也不派生自 MarshalByRefObject 的。这些 类型的类不能在远程对象的公共方法中用作参数。它们只能用于创建它们的应用程序域中。
CreateObjRef(从 MarshalByRefObject 继承) 创建一个对象,该对象包含生成用于与远程对象进行通讯的代理所需的全部相关信息。 GetLifetimeService(从 MarshalByRefObject 继承) 检索控制此实例的生存期策略的当前生存期服务对象。 InitializeLifetimeService(从 MarshalByRefObject 继承) 获取控制此实例的生存期策略的生存期服务对象。
MarshalByRefObject.GetLifetimeService() MarshalByRefObject.InitializeLifetimeService() 解决方法 请考虑使用 WCF
Invoke(this, "cnblogs"); } } public class F1 : MarshalByRefObject { public 建议新建的两个类是写在一个文件,而且需要让两个类继承 MarshalByRefObject 和接口 IRemoteEventHandle ,并且只允许本地的NativeEventHandle在构造传入远程的类 [Remote] [Serializable] public class RemoteEventHandle : MarshalByRefObject, IRemoteEventHandle 如下面的代码 [Serializable] public class RemoteEventHandle : MarshalByRefObject, IRemoteEventHandle 这个问题有最简单的例子,请看下面代码,保持远程的代码不变 public class NativeEventHandle : MarshalByRefObject, IRemoteEventHandle
OpenDoorEventHandler(string door, string direction); [Serializable] class MachineControlService : MarshalByRefObject OpenDoorEvent(door ,direction); } } } } 在这里首先实现类需要实现MarshalByRefObject
如果要把 DLL 加载在 AppDomain 需要先写一个类,继承MarshalByRefObject internal class ApplicationProxy : MarshalByRefObject
namespace MyClassLibrary
{
///
如果我们定义如下几个类型,Foo继承于MarshalByRefObject,里面具有一个类型为IBar的Bar属性,上面应用了DependencyAttribute特性使之成为一个“注入属性”。 1: public class Foo : MarshalByRefObject 2: { 3: [Dependency] 4: public IBar Bar { get 1: public class Foo : MarshalByRefObject 2: { 3: [Dependency] 4: public IBar Bar { get 1: public class Foo : MarshalByRefObject 2: { 3: public int Count { get; set; } 4: [
Invoke(this, "cnblogs"); } } public class F1 : MarshalByRefObject { public 建议新建的两个类是写在一个文件,而且需要让两个类继承 MarshalByRefObject 和接口 IRemoteEventHandle ,并且只允许本地的NativeEventHandle在构造传入远程的类 [Remote] [Serializable] public class RemoteEventHandle : MarshalByRefObject, IRemoteEventHandle 如下面的代码 [Serializable] public class RemoteEventHandle : MarshalByRefObject, IRemoteEventHandle 这个问题有最简单的例子,请看下面代码,保持远程的代码不变 public class NativeEventHandle : MarshalByRefObject, IRemoteEventHandle
如果我们定义如下几个类型,Foo继承于MarshalByRefObject,里面具有一个类型为IBar的Bar属性,上面应用了DependencyAttribute特性使之成为一个“注入属性”。 1: public class Foo : MarshalByRefObject 2: { 3: [Dependency] 4: public IBar Bar { get 1: public class Foo : MarshalByRefObject 2: { 3: [Dependency] 4: public IBar Bar { get 1: public class Foo : MarshalByRefObject 2: { 3: public int Count { get; set; } 4: [
instance.SomeMethod(); // 卸载 AppDomain AppDomain.Unload(newDomain); } } public class SomeType : MarshalByRefObject 2.4 边界访问对象 跨 AppDomain 边界访问对象的机制 MarshalByRefObject: 直接访问跨 AppDomain 边界的对象需要继承自 MarshalByRefObject 类。 使用 MarshalByRefObject 进行跨 AppDomain 边界访问 下面是一个示例,展示了如何使用 MarshalByRefObject 进行跨 AppDomain 边界访问对象和调用方法 SomeType 类继承自 MarshalByRefObject,使得跨 AppDomain 调用方法成为可能。 ); // 卸载新的 AppDomain AppDomain.Unload(newDomain); } } public class SomeType : MarshalByRefObject
obj = null; Console.ReadLine(); } } class ProxyObject : MarshalByRefObject 要想让一个对象能够穿过AppDomain边界,必须要继承MarshalByRefObject类,否则无法被其他AppDomain使用。 2.
Proxy到达Server端所在的Application Domain后,Remoting Infrastructure在Server 端激活(Activate)相应的远程对象(一个继承子System.MarshalByRefObject result); 4: } 步骤三、定义远程对象类型 DuplexCalculatorRemoting 1: public class DuplexCalculatorRemoting : MarshalByRefObject 之所以要把typeFilterLevel为Full,是因为我们的远程调用里包含一Callback对象,它实际上是一个继承System.MarshalByRefObject类对象(这个的对象将在Artech.DuplexRemoting.Client 步骤五、定义Callback对象类型和调用远程对象 CalculatorCallbackHandler 1: public class CalculatorCallbackHandler : MarshalByRefObject Domain远程地调用运行Client Application Domain中的Callback对象(Callback的执行实际是在Client而不在Server),所以Callback对象应该是一个MarshalByRefObject
projectMessage = value; } 这里构造的类和结构体需要加上serializable序列化特性,用于数据传输 处理逻辑 public class Processing : MarshalByRefObject string ReturnResult() { return GlobalVariables.functionString; } } 类需要继承MarshalByRefObject