Ejemplo n.º 1
0
        /// <summary>显式关闭和销毁当前的WCF通道</summary>
        public static void Close()
        {
            lock (_ans)
            {
                IClientChannel channel = _backupsClass as IClientChannel;

                try
                {
                    if (null == channel)
                    {
                        return;
                    }

                    ClearAllEvents(channel);

                    if (channel.State == CommunicationState.Opened ||
                        channel.State == CommunicationState.Opening ||
                        channel.State == CommunicationState.Created)
                    {
                        channel.Close();
                    }
                }
                catch { }
                finally { _backupsClass = null; _channelState = false; }
            }
        }
Ejemplo n.º 2
0
        /// <summary>新建一个WCF的通道,返回通道是否有效</summary>
        private static bool CreatNewChannel()
        {
            lock (_ans)
            {
                try
                {
                    WcfClientHelper.SetWCFParas(_ipAdress, _portNum, _binding);

                    _backupsClass = WcfClientHelper.CreateService <IRemoteDispatch>();

                    IClientChannel channel = _backupsClass as IClientChannel;

                    if (channel == null)
                    {
                        return(false);
                    }

                    channel.Faulted += ChannelFaulted;

                    if (channel.State != CommunicationState.Opened)
                    {
                        channel.Open();
                    }

                    if (!_channelState)
                    {
                        _channelState = true;
                    }

                    return(true);
                }
                catch { if (_channelState)
                        {
                            _channelState = false;
                        }
                        return(false); }
            }
        }
Ejemplo n.º 3
0
        // Implement Invoke
        public override IMessage Invoke(IMessage reqMsg)
        {
            AssertValid();
            IMessage retMsg = null;

            // CHECK FOR CTOR
            if (reqMsg is IConstructionCallMessage)
            {
                DBG.Info(DBG.SC, "RSCP: Short circuiting constructor call.");

                if (((IConstructionCallMessage)reqMsg).ArgCount > 0)
                {
                    throw new ServicedComponentException(Resource.FormatString("ServicedComponentException_ConstructorArguments"));
                }

                // WE don't need dispatch the constructor message, the default .ctor is called
                // by CoCI
                // Create the return message
                MarshalByRefObject retObj = (MarshalByRefObject)GetTransparentProxy();
                return(EnterpriseServicesHelper.CreateConstructionReturnMessage((IConstructionCallMessage)reqMsg, retObj));
            }

            // NON CTOR MESSAGE
            MethodBase mb = ((IMethodMessage)reqMsg).MethodBase;
            MemberInfo m  = (MemberInfo)mb;

            DBG.Assert(m != null, "member info should not be null");

            if (mb == _getTypeMethod)
            {
                DBG.Info(DBG.SC, "RSCP: Hijacking call to GetType");
                IMethodCallMessage mcm = (IMethodCallMessage)reqMsg;
                return(new ReturnMessage(ProxiedType, null, 0, mcm.LogicalCallContext, mcm));
            }

            if (mb == _getHashCodeMethod)
            {
                int hashCode = this.GetHashCode();
                DBG.Info(DBG.SC, "RSCP: Hijacking call to GetHashCode, returning " + hashCode);
                IMethodCallMessage mcm = (IMethodCallMessage)reqMsg;
                return(new ReturnMessage(hashCode, null, 0, mcm.LogicalCallContext, mcm));
            }

            DBG.Info(DBG.SC, "RSCP: Delivering call to " + mb.Name);

            // convert to class member info
            MemberInfo clsMemInfo = ReflectionCache.ConvertToClassMI(ProxiedType, m);

            DBG.Assert(!clsMemInfo.ReflectedType.IsInterface,
                       "Failed to convert interface member info to class member info");

            // dispatch the call
#if _DEBUG
            if (clsMemInfo != m)
            {
                DBG.Info(DBG.SC, "RSCP: converted interface to class call: " + clsMemInfo.Name);
            }
#endif

            try
            {
                int iMethodInfo;
                // check for rolebased security
                if (_fUseIntfDispatch ||
                    (((iMethodInfo = ServicedComponentInfo.MICachedLookup(clsMemInfo)) & ServicedComponentInfo.MI_HASSPECIALATTRIBUTES) != 0) ||
                    ((iMethodInfo & ServicedComponentInfo.MI_EXECUTEMESSAGEVALID) != 0)
                    )
                {
                    // role based security implies we dispatch through an interface
                    MemberInfo intfMemInfo = ReflectionCache.ConvertToInterfaceMI(m);
                    if (intfMemInfo == null)
                    {
                        throw new ServicedComponentException(Resource.FormatString("ServicedComponentException_SecurityMapping"));
                    }
                    //retMsg = EnterpriseServicesHelper.DispatchRemoteCall(reqMsg, intfMemInfo, _server);
                    MethodCallMessageWrapperEx msgex = new MethodCallMessageWrapperEx((IMethodCallMessage)reqMsg, (MethodBase)intfMemInfo);
                    retMsg = RemotingServices.ExecuteMessage((MarshalByRefObject)_server, (IMethodCallMessage)msgex);
                }
                else
                {
                    // check for AutoDone
                    bool fAutoDone = (iMethodInfo & ServicedComponentInfo.MI_AUTODONE) != 0;

                    String s = ComponentServices.ConvertToString(reqMsg);

                    IRemoteDispatch iremDisp = (IRemoteDispatch)_server;

                    String sret;
                    if (fAutoDone)
                    {
                        sret = iremDisp.RemoteDispatchAutoDone(s);
                    }
                    else
                    {
                        sret = iremDisp.RemoteDispatchNotAutoDone(s);
                    }
                    retMsg = ComponentServices.ConvertToReturnMessage(sret, reqMsg);
                }
            }
            catch (COMException e)
            {
                if (!(e.ErrorCode == Util.CONTEXT_E_ABORTED || e.ErrorCode == Util.CONTEXT_E_ABORTING))
                {
                    throw;
                }
                if (IsDisposeRequest(reqMsg))
                {
                    IMethodCallMessage mcm = reqMsg as IMethodCallMessage;
                    retMsg = new ReturnMessage(null, null, 0, mcm.LogicalCallContext, mcm);
                }
                else
                {
                    throw;
                }
            }

            // if disposing, we need to release this side of the world.
            if (IsDisposeRequest(reqMsg))
            {
                Dispose(true);
            }

            return(retMsg);
        }
        public override IMessage Invoke(IMessage reqMsg)
        {
            this.AssertValid();
            IMessage message = null;

            if (reqMsg is IConstructionCallMessage)
            {
                if (((IConstructionCallMessage)reqMsg).ArgCount > 0)
                {
                    throw new ServicedComponentException(Resource.FormatString("ServicedComponentException_ConstructorArguments"));
                }
                MarshalByRefObject transparentProxy = (MarshalByRefObject)this.GetTransparentProxy();
                return(EnterpriseServicesHelper.CreateConstructionReturnMessage((IConstructionCallMessage)reqMsg, transparentProxy));
            }
            MethodBase methodBase = ((IMethodMessage)reqMsg).MethodBase;
            MemberInfo mi         = methodBase;

            if (methodBase == _getTypeMethod)
            {
                IMethodCallMessage mcm = (IMethodCallMessage)reqMsg;
                return(new ReturnMessage(this.ProxiedType, null, 0, mcm.LogicalCallContext, mcm));
            }
            if (methodBase == _getHashCodeMethod)
            {
                int hashCode = this.GetHashCode();
                IMethodCallMessage message3 = (IMethodCallMessage)reqMsg;
                return(new ReturnMessage(hashCode, null, 0, message3.LogicalCallContext, message3));
            }
            if (methodBase == _isInstanceOfTypeMethod)
            {
                IMethodCallMessage message4 = (IMethodCallMessage)reqMsg;
                Type inArg = (Type)message4.GetInArg(0);
                return(new ReturnMessage(inArg.IsInstanceOfType(this.ProxiedType), null, 0, message4.LogicalCallContext, message4));
            }
            MemberInfo m = ReflectionCache.ConvertToClassMI(this.ProxiedType, mi);

            try
            {
                int num2;
                if ((this._fUseIntfDispatch || (((num2 = ServicedComponentInfo.MICachedLookup(m)) & 4) != 0)) || ((num2 & 8) != 0))
                {
                    MemberInfo info3 = ReflectionCache.ConvertToInterfaceMI(mi);
                    if (info3 == null)
                    {
                        throw new ServicedComponentException(Resource.FormatString("ServicedComponentException_SecurityMapping"));
                    }
                    MethodCallMessageWrapperEx ex = new MethodCallMessageWrapperEx((IMethodCallMessage)reqMsg, (MethodBase)info3);
                    message = RemotingServices.ExecuteMessage((MarshalByRefObject)this._server, ex);
                }
                else
                {
                    string          str2;
                    bool            flag     = (num2 & 2) != 0;
                    string          s        = ComponentServices.ConvertToString(reqMsg);
                    IRemoteDispatch dispatch = (IRemoteDispatch)this._server;
                    if (flag)
                    {
                        str2 = dispatch.RemoteDispatchAutoDone(s);
                    }
                    else
                    {
                        str2 = dispatch.RemoteDispatchNotAutoDone(s);
                    }
                    message = ComponentServices.ConvertToReturnMessage(str2, reqMsg);
                }
            }
            catch (COMException exception)
            {
                if ((exception.ErrorCode != -2147164158) && (exception.ErrorCode != -2147164157))
                {
                    throw;
                }
                if (!this.IsDisposeRequest(reqMsg))
                {
                    throw;
                }
                IMethodCallMessage message5 = reqMsg as IMethodCallMessage;
                message = new ReturnMessage(null, null, 0, message5.LogicalCallContext, message5);
            }
            if (this.IsDisposeRequest(reqMsg))
            {
                this.Dispose(true);
            }
            return(message);
        }