Example #1
0
        ///<summary>
        /// checks, whether <paramref name="target"/> supports the methods of <paramref name="requiredType"/>.
        /// Supports testing transparent proxies.
        ///</summary>
        ///<param name="target">the target instance or <c>null</c></param>
        ///<param name="targetName">the name of the target to be used in error messages</param>
        ///<param name="requiredType">the type to test for</param>
        /// <exception cref="ArgumentNullException">
        /// if <paramref name="requiredType"/> is <c>null</c>
        /// </exception>
        /// <exception cref="NotSupportedException">
        /// if it is not possible to invoke methods of
        /// type <paramref name="requiredType"/> on <paramref name="target"/>
        /// </exception>
        public static void Understands(object target, string targetName, Type requiredType)
        {
            ArgumentNotNull(requiredType, "requiredType");

            if (target == null)
            {
                throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "Target '{0}' is null.", targetName));
            }

            Type targetType;

            if (RemotingServices.IsTransparentProxy(target))
            {
                RealProxy         rp  = RemotingServices.GetRealProxy(target);
                IRemotingTypeInfo rti = rp as IRemotingTypeInfo;
                if (rti != null)
                {
                    if (rti.CanCastTo(requiredType, target))
                    {
                        return;
                    }
                    throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "Target '{0}' is a transparent proxy that does not support methods of '{1}'.", targetName, requiredType.FullName));
                }
                targetType = rp.GetProxiedType();
            }
            else
            {
                targetType = target.GetType();
            }

            if (!requiredType.IsAssignableFrom(targetType))
            {
                throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "Target '{0}' of type '{1}' does not support methods of '{2}'.", targetName, targetType, requiredType.FullName));
            }
        }
        // Check whether we can cast the transparent proxy to the given type
        public bool CanCastTo(Type castType, Object o)
        {
            bool fCastOK = false;

            // The identity should be non-null
            BCLDebug.Assert(null != IdentityObject, "null != IdentityObject");

            Message.DebugOut("CheckCast for identity " + IdentityObject.GetType());

            if ((castType == s_typeofObject) ||
                (castType == s_typeofMarshalByRefObject))
            {
                return(true);
            }

            // Get the objref of the proxy
            ObjRef oRef = IdentityObject.ObjectRef;

            // If the object ref is non-null then check against the type info
            // stored in the it
            if (null != oRef)
            {
                Object oTP = GetTransparentProxy();

                // Check that there is a matching type in the server object
                // hierarchy represented in the objref
                Message.DebugOut("Calling CanCastTo for type " + castType);
                IRemotingTypeInfo typeInfo = oRef.TypeInfo;
                if (null != typeInfo)
                {
                    fCastOK = typeInfo.CanCastTo(castType, oTP);
                    if (!fCastOK && typeInfo.GetType() == typeof(TypeInfo) && oRef.IsWellKnown())
                    {
                        fCastOK = CanCastToWK(castType);
                    }
                }
                else
                {
                    if (oRef.IsObjRefLite())
                    {
                        // we should do a dynamic cast across the network
                        fCastOK = MarshalByRefObject.CanCastToXmlTypeHelper(castType, (MarshalByRefObject)o);
                    }
                }
            }
            // This is a well known object which does not have a backing ObjRef
            else
            {
                fCastOK = CanCastToWK(castType);
            }
            return(fCastOK);
        }
        public override IMessage Invoke(IMessage message)
        {
            RealProxy          delegatingProxy = null;
            IMethodCallMessage msg             = message as IMethodCallMessage;

            try
            {
                delegatingProxy = serviceChannelCreator.CreateChannel();
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }

                return(new ReturnMessage(DiagnosticUtility.ExceptionUtility.ThrowHelperError(new COMException(e.GetBaseException().Message, Marshal.GetHRForException(e.GetBaseException()))), msg));
            }

            MethodBase        typeMethod = msg.MethodBase;
            IRemotingTypeInfo typeInfo   = delegatingProxy as IRemotingTypeInfo;

            if (typeInfo == null)
            {
                throw Fx.AssertAndThrow("Type Info cannot be null");
            }
            if (typeInfo.CanCastTo(typeMethod.DeclaringType, null))
            {
                IMessage      msgReturned = delegatingProxy.Invoke(message);
                ReturnMessage returnMsg   = msgReturned as ReturnMessage;
                if ((returnMsg == null) || (returnMsg.Exception == null))
                {
                    return(msgReturned);
                }
                else
                {
                    return(new ReturnMessage(DiagnosticUtility.ExceptionUtility.ThrowHelperError(new COMException(returnMsg.Exception.GetBaseException().Message, Marshal.GetHRForException(returnMsg.Exception.GetBaseException()))), msg));
                }
            }
            else
            {
                return(new ReturnMessage(DiagnosticUtility.ExceptionUtility.ThrowHelperError(new COMException(SR.GetString(SR.OperationNotFound, typeMethod.Name), HR.DISP_E_UNKNOWNNAME)), msg));
            }
        }
Example #4
0
        public bool CanCastTo(Type castType, object o)
        {
            if (castType == (Type)null)
            {
                throw new ArgumentNullException("castType");
            }
            RuntimeType castType1 = castType as RuntimeType;

            if (castType1 == (RuntimeType)null)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"));
            }
            bool flag = false;

            if (castType1 == RemotingProxy.s_typeofObject || castType1 == RemotingProxy.s_typeofMarshalByRefObject)
            {
                return(true);
            }
            ObjRef objectRef = this.IdentityObject.ObjectRef;

            if (objectRef != null)
            {
                object            transparentProxy = this.GetTransparentProxy();
                IRemotingTypeInfo typeInfo         = objectRef.TypeInfo;
                if (typeInfo != null)
                {
                    flag = typeInfo.CanCastTo((Type)castType1, transparentProxy);
                    if (!flag && typeInfo.GetType() == typeof(System.Runtime.Remoting.TypeInfo) && objectRef.IsWellKnown())
                    {
                        flag = this.CanCastToWK((Type)castType1);
                    }
                }
                else if (objectRef.IsObjRefLite())
                {
                    flag = MarshalByRefObject.CanCastToXmlTypeHelper(castType1, (MarshalByRefObject)o);
                }
            }
            else
            {
                flag = this.CanCastToWK((Type)castType1);
            }
            return(flag);
        }
Example #5
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.RemotingServices.GetObjRefForProxy

        // <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));
        // </Snippet1>
    }
Example #6
0
        public bool CanCastTo(Type castType, object o)
        {
            if (castType == null)
            {
                throw new ArgumentNullException("castType");
            }
            RuntimeType fromType = castType as RuntimeType;

            if (fromType == null)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"));
            }
            bool flag = false;

            if ((fromType == s_typeofObject) || (fromType == s_typeofMarshalByRefObject))
            {
                return(true);
            }
            ObjRef objectRef = this.IdentityObject.ObjectRef;

            if (objectRef != null)
            {
                object            transparentProxy = this.GetTransparentProxy();
                IRemotingTypeInfo typeInfo         = objectRef.TypeInfo;
                if (typeInfo != null)
                {
                    flag = typeInfo.CanCastTo(fromType, transparentProxy);
                    if ((!flag && (typeInfo.GetType() == typeof(TypeInfo))) && objectRef.IsWellKnown())
                    {
                        flag = this.CanCastToWK(fromType);
                    }
                    return(flag);
                }
                if (objectRef.IsObjRefLite())
                {
                    flag = MarshalByRefObject.CanCastToXmlTypeHelper(fromType, (MarshalByRefObject)o);
                }
                return(flag);
            }
            return(this.CanCastToWK(fromType));
        }
Example #7
0
        ///<summary>
        /// checks, whether <paramref name="target"/> supports the methods of <paramref name="requiredType"/>.
        /// Supports testing transparent proxies.
        ///</summary>
        ///<param name="target">the target instance or <c>null</c></param>
        ///<param name="targetName">the name of the target to be used in error messages</param>
        ///<param name="requiredType">the type to test for</param>
        /// <exception cref="ArgumentNullException">
        /// if <paramref name="requiredType"/> is <c>null</c>
        /// </exception>
        /// <exception cref="NotSupportedException">
        /// if it is not possible to invoke methods of
        /// type <paramref name="requiredType"/> on <paramref name="target"/>
        /// </exception>
        public static void Understands(object target, string targetName, Type requiredType)
        {
            ArgumentNotNull(requiredType, "requiredType");

            if (target == null)
            {
                ThrowNotSupportedException($"Target '{targetName}' is null.");
            }

            Type targetType = null;

            if (RemotingServices.IsTransparentProxy(target))
            {
#if !NETSTANDARD
                System.Runtime.Remoting.Proxies.RealProxy rp = RemotingServices.GetRealProxy(target);
                IRemotingTypeInfo rti = rp as IRemotingTypeInfo;
                if (rti != null)
                {
                    if (rti.CanCastTo(requiredType, target))
                    {
                        return;
                    }
                    ThrowNotSupportedException(
                        $"Target '{targetName}' is a transparent proxy that does not support methods of '{requiredType.FullName}'.");
                }
                targetType = rp.GetProxiedType();
#endif
            }
            else
            {
                targetType = target.GetType();
            }

            if (!requiredType.IsAssignableFrom(targetType))
            {
                ThrowNotSupportedException($"Target '{targetName}' of type '{targetType}' does not support methods of '{requiredType.FullName}'.");
            }
        }
Example #8
0
        public override IMessage Invoke(IMessage message)
        {
            RealProxy          proxy = null;
            IMethodCallMessage mcm   = message as IMethodCallMessage;

            try
            {
                proxy = this.serviceChannelCreator.CreateChannel();
            }
            catch (Exception exception)
            {
                if (Fx.IsFatal(exception))
                {
                    throw;
                }
                return(new ReturnMessage(DiagnosticUtility.ExceptionUtility.ThrowHelperError(new COMException(exception.GetBaseException().Message, Marshal.GetHRForException(exception.GetBaseException()))), mcm));
            }
            MethodBase        methodBase = mcm.MethodBase;
            IRemotingTypeInfo info       = proxy as IRemotingTypeInfo;

            if (info == null)
            {
                throw Fx.AssertAndThrow("Type Info cannot be null");
            }
            if (info.CanCastTo(methodBase.DeclaringType, null))
            {
                IMessage      message3 = proxy.Invoke(message);
                ReturnMessage message4 = message3 as ReturnMessage;
                if ((message4 != null) && (message4.Exception != null))
                {
                    return(new ReturnMessage(DiagnosticUtility.ExceptionUtility.ThrowHelperError(new COMException(message4.Exception.GetBaseException().Message, Marshal.GetHRForException(message4.Exception.GetBaseException()))), mcm));
                }
                return(message3);
            }
            return(new ReturnMessage(DiagnosticUtility.ExceptionUtility.ThrowHelperError(new COMException(System.ServiceModel.SR.GetString("OperationNotFound", new object[] { methodBase.Name }), HR.DISP_E_UNKNOWNNAME)), mcm));
        }
Example #9
0
        // Check whether we can cast the transparent proxy to the given type
        public bool CanCastTo(Type castType, Object o)
        {
            bool fCastOK = false;

            // The identity should be non-null
            BCLDebug.Assert(null != IdentityObject, "null != IdentityObject");

            Message.DebugOut("CheckCast for identity " + IdentityObject.GetType());

            if ((castType == s_typeofObject) ||
                (castType == s_typeofMarshalByRefObject))
            {
                return(true);
            }

            // Get the objref of the proxy
            ObjRef oRef = IdentityObject.ObjectRef;

            // If the object ref is non-null then check against the type info
            // stored in the it
            if (null != oRef)
            {
                Object oTP = GetTransparentProxy();

                // Check that there is a matching type in the server object
                // hierarchy represented in the objref
                Message.DebugOut("Calling CanCastTo for type " + castType);
                IRemotingTypeInfo typeInfo = oRef.TypeInfo;
                if (null != typeInfo)
                {
                    fCastOK = typeInfo.CanCastTo(castType, oTP);
                }
                else
                {
                    if (oRef.IsObjRefLite())
                    {
                        // we should do a dynamic cast across the network
                        fCastOK = MarshalByRefObject.CanCastToXmlTypeHelper(castType, (MarshalByRefObject)o);
                    }
                }
            }
            // This is a well known object which does not have a backing ObjRef
            else
            {
                Message.DebugOut("CheckCast for well known objects and type " + castType);

                // Check whether the type to which we want to cast is
                // compatible with the current type
                if (castType.IsClass)
                {
                    fCastOK = GetProxiedType().IsAssignableFrom(castType);
                }
                else
                {
                    // NOTE: we are coming here also for x-context proxies
                    // when unmanaged code cannot determine if the cast is not
                    // okay
                    if (!(IdentityObject is ServerIdentity))
                    {
                        BCLDebug.Assert(
                            IdentityObject.URI != null,
                            "Bad WellKnown ID");
                        // Always allow interface casts to succeed. If the
                        // interface is not supported by the well known object
                        // then we will throw an exception when the interface
                        // is invoked.
                        fCastOK = true;
                    }
                }
            }

            return(fCastOK);
        }