Пример #1
0
    public static void Main()
    {
        ChannelServices.RegisterChannel(new HttpChannel(0));
        RemotingConfiguration.RegisterActivatedClientType(typeof(SampleService), "http://localhost:9000/MySampleService");
        SampleService myRemoteObject = new SampleService();
        bool          result         = myRemoteObject.SampleMethod();
        // System.Runtime.Remoting.ObjRef
        // <Snippet1>
        ObjRef objRefSample = RemotingServices.GetObjRefForProxy(myRemoteObject);

        Console.WriteLine("***ObjRef Details***");
        Console.WriteLine("URI:\t{0}", objRefSample.URI);
        object[] channelData = objRefSample.ChannelInfo.ChannelData;
        Console.WriteLine("Channel Info:");
        foreach (object o in channelData)
        {
            Console.WriteLine("\t{0}", o.ToString());
        }
        IEnvoyInfo envoyInfo = objRefSample.EnvoyInfo;

        if (envoyInfo == null)
        {
            Console.WriteLine("This ObjRef does not have envoy information.");
        }
        else
        {
            IMessageSink envoySinks = envoyInfo.EnvoySinks;
            Console.WriteLine("Envoy Sink Class: {0}", envoySinks);
        }
        IRemotingTypeInfo typeInfo = objRefSample.TypeInfo;

        Console.WriteLine("Remote type name: {0}", typeInfo.TypeName);
        Console.WriteLine("Can my object cast to a Bitmap? {0}",
                          typeInfo.CanCastTo(typeof(System.Drawing.Bitmap), objRefSample));
        Console.WriteLine("Is this object from this AppDomain? {0}", objRefSample.IsFromThisAppDomain());
        Console.WriteLine("Is this object from this process? {0}", objRefSample.IsFromThisProcess());
        // </Snippet1>
    }
Пример #2
0
        /// <summary>
        /// Registers the receiver and associate the provided object with it.
        /// Returns false if the receiver has already been registered.
        /// WARNING: does not check whether the receiver supports the required interface (via Reflection)
        /// because this check requires client's dll.
        /// </summary>
        /// <param name="obj">The receiver being registered.</param>
        /// <param name="tag">The object associated with the receiver. This object is accessible when receiver is being unregistered or during filtering.</param>
        /// <param name="remoteGenuineUri">The uri of the remote host provided by any of Genuine Channels.</param>
        /// <param name="transportContext">The transport context of the remote host.</param>
        /// <returns>False if the receiver has been already registered.</returns>
        public bool Add(MarshalByRefObject obj, object tag, string remoteGenuineUri, ITransportContext transportContext)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            BinaryLogWriter binaryLogWriter = GenuineLoggingServices.BinaryLogWriter;

            // check if it is in the list
            string uri = RemotingServices.GetObjectUri(obj);

            if (uri == null && !RemotingServices.IsObjectOutOfAppDomain(obj))
            {
                // it was not marshalled
                RemotingServices.Marshal(obj);
                uri = RemotingServices.GetObjectUri(obj);
            }

            using (ReaderAutoLocker reader = new ReaderAutoLocker(this._readerWriterLock))
            {
                if (this._receivers.ContainsKey(uri))
                {
                    return(false);
                }
            }

            // this check can not be performed because client's dll is required
//			// check on the interface
//			bool supportInterface = false;
//			foreach(Type interfaceType in obj.GetType().GetInterfaces())
//				if (interfaceType == this._interfaceToSupport)
//				{
//					supportInterface = true;
//					break;
//				}
//			if (! supportInterface)
//				throw GenuineExceptions.Get_Broadcast_ObjectDoesNotSupportDestinationInterface();

            // adds the object to the receiver list
            ReceiverInfo receiverInfo = new ReceiverInfo();

            receiverInfo.MbrObject = obj;
            receiverInfo.MbrUri    = uri;
            receiverInfo.Tag       = tag;

            if (binaryLogWriter != null)
            {
                try
                {
                    if (receiverInfo.MbrObject != null)
                    {
                        receiverInfo.DbgRemoteHost = GenuineUtility.FetchHostInformationFromMbr(receiverInfo.MbrObject);
                    }
                }
                catch (Exception ex)
                {
                    binaryLogWriter.WriteImplementationWarningEvent("Dispatcher.Add",
                                                                    LogMessageType.Error, ex, GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                                                    "Can't get HostInformation from MbrObject.");
                }
            }

            ObjRef objRef = receiverInfo.MbrObject.CreateObjRef(typeof(MarshalByRefObject));

            receiverInfo.Local = objRef.IsFromThisAppDomain();

            // cache object's info to speed up sending thru Genuine Channels
            if (!receiverInfo.Local)
            {
                if (remoteGenuineUri != null)
                {
                    receiverInfo.IClientChannelSink = new GenuineTcpClientTransportSink(remoteGenuineUri, transportContext);
                }
                else
                {
                    // check whether the client sink has registered itself on this MBR
                    receiverInfo.IClientChannelSink = ChannelServices.GetChannelSinkProperties(obj)["GC_TS"] as IClientChannelSink;
                    if (receiverInfo.IClientChannelSink == null)
                    {
                        throw GenuineExceptions.Get_Broadcast_ClientSinkIsUnknown();
                    }
                }

                // object uri
                receiverInfo.SerializedObjRef = objRef;

//				// and shell's uri
//				string shellUri;
//				ITransportContext iTransportContext;
//				GenuineUtility.FetchChannelUriFromMbr(obj, out shellUri, out iTransportContext);
//				if (shellUri == null)
//					throw GenuineExceptions.Get_Send_NoSender(objRef.URI);
//
//				receiverInfo.ReceiverUri = shellUri;
            }

            // LOG:
            if (binaryLogWriter != null && binaryLogWriter[LogCategory.BroadcastEngine] > 0)
            {
                binaryLogWriter.WriteBroadcastEngineEvent(LogCategory.BroadcastEngine, "Dispatcher.Add",
                                                          LogMessageType.BroadcastRecipientAdded, null, null, receiverInfo.DbgRemoteHost, null,
                                                          GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                                          null, null, false, this, null, true, receiverInfo,
                                                          null, null,
                                                          "The broadcast recipient is added.");
            }

            // register the sponsor to prevent unexpected reclaiming
            ILease lease = (ILease)RemotingServices.GetLifetimeService(obj);

            if (lease != null)
            {
                lease.Register(this.GlobalSponsor);
            }

            // and register it
            using (WriterAutoLocker writer = new WriterAutoLocker(this._readerWriterLock))
            {
                this._cachedReceiversInfoArray = null;
                this._receivers[uri]           = receiverInfo;
            }
            return(true);
        }
Пример #3
0
 public void UnmarshaledObject(object obj, ObjRef or)
 {
     Console.WriteLine("Unmarshaled " + obj.GetType().Name + " local:" + or.IsFromThisAppDomain());
 }
 public void UnmarshaledObject(object obj, ObjRef or)
 {
     Console.WriteLine(); Console.WriteLine("对象" + obj.ToString() + " is unmarshaled at " + DateTime.Now.ToShortTimeString() + ",IsFromThisAppDomain=" + or.IsFromThisAppDomain());
 }