示例#1
0
        private void Channel_Faulted(object sender, EventArgs e)
        {
            IRemoteSideCommunicationContract sc = sender as IRemoteSideCommunicationContract;
            bool lockTaken = false;

            try
            {
                syncRoot.Enter(ref lockTaken);
                RemoteSideIDType remoteSideID;

                if (remoteSideIDsByCommunicationContractDict.TryGetValue(sc, out remoteSideID))
                {
                    RemoteSideFaulted(remoteSideID);
                }
                else
                {
                    throw new InvalidOperationException(string.Format("Unkwon ID: {0}!", remoteSideID));
                }
            }
            finally
            {
                if (lockTaken)
                {
                    syncRoot.Exit();
                }
            }
        }
示例#2
0
        protected virtual void DisconnectRemoteSide(RemoteSideIDType remoteID, IRemoteSideCommunicationContract serviceContract)
        {
            remoteSideIDsByCommunicationContractDict.Remove(serviceContract);
            communicationContractsByRemoteSideIDDict.Remove(remoteID);

            OnRemoteSideDisconnected(remoteID);
        }
示例#3
0
        protected virtual object ExecuteOnRemoteSideInternal(RemoteSideIDType remoteSideID, RemoteOperationDescriptor rso)
        {
            object ret = null;

            if (remoteSideID == null)
            {
                throw new ArgumentNullException("remoteSideID");
            }

            if (rso == null)
            {
                throw new ArgumentNullException("rso");
            }

            // find appropriate client
            IRemoteSideCommunicationContract contract = null;
            bool lockTaken = false;

            try
            {
                syncRoot.Enter(ref lockTaken);
                communicationContractsByRemoteSideIDDict.TryGetValue(remoteSideID, out contract);
            }
            finally
            {
                if (lockTaken)
                {
                    syncRoot.Exit();
                }
            }

            if (contract != null)
            {
                RemoteRequest req = remoteOperationHandler.CreateRequest(rso);
                req.RemoteID = remoteSideID;

                RemoteResponse resp = null;

                try
                {
                    RequestToRemoteSideStarted(remoteSideID, rso);

                    resp = contract.ExecuteRequest(req);
                }
                finally
                {
                    RequestToRemoteSideFinished(remoteSideID, rso);
                }
                ret = remoteOperationHandler.HandleResponse(resp);
            }
            else
            {
                throw new InvalidOperationException(String.Format("Unknown remote side id: {0}", remoteSideID));
            }

            return(ret);
        }
示例#4
0
        public IRemoteSideCommunicationContract GetCurrentRemoteSideCommunicationContract()
        {
            IRemoteSideCommunicationContract ret = null;

            var cc = OperationContext.Current;

            if (cc != null)
            {
                ret = cc.GetCallbackChannel <IRemoteSideCommunicationContract>();
            }

            return(ret);
        }
示例#5
0
        public WCFServiceHost(IDIContainer diContainer, IRemoteSideCommunicationContract clientServiceContractInstance, Uri clientServiceAddress)
            : base(clientServiceContractInstance, clientServiceAddress)
        {
            this.ID          = RemoteSideIDType.Parse(Guid.NewGuid().ToString());
            this.diContainer = diContainer;
            this.clientServiceContractInstance = clientServiceContractInstance;
            cm = diContainer.GetLazyBoundInstance <IWCFConfigManager>();

            ApplyConfiguration();

            this.AddServiceEndpoint(
                ServiceMetadataBehavior.MexContractName,
                MetadataExchangeBindings.CreateMexTcpBinding(),
                String.Concat(new Uri(cm.Value.ClientServiceAddress).OriginalString, "/mex"));

            this.Closed  += new EventHandler(clientServiceHost_StateChanged);
            this.Closing += new EventHandler(clientServiceHost_StateChanged);
            this.Faulted += new EventHandler(clientServiceHost_StateChanged);
            this.Opened  += new EventHandler(clientServiceHost_StateChanged);
            this.Opening += new EventHandler(clientServiceHost_StateChanged);
        }
示例#6
0
        private void Channel_Closed(object sender, EventArgs e)
        {
            IRemoteSideCommunicationContract sc = sender as IRemoteSideCommunicationContract;
            bool lockTaken = false;

            try
            {
                syncRoot.Enter(ref lockTaken);
                RemoteSideIDType remoteSideID;

                if (remoteSideIDsByCommunicationContractDict.TryGetValue(sc, out remoteSideID))
                {
                    DisconnectRemoteSide(remoteSideID, sc);
                }
            }
            finally
            {
                if (lockTaken)
                {
                    syncRoot.Exit();
                }
            }
        }
示例#7
0
        public virtual RemoteResponse ExecuteRequest(RemoteRequest request)
        {
            ThrowIfNotInitialized();

            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            RemoteResponse ret = null;

            RemoteSideIDType remoteID = request.RemoteID;

            if (remoteID == null)
            {
                throw new InvalidOperationException("Remote call without remote side id!"); //LOCSTR
            }

            var wrapper = remoteSide;
            IRemoteSideCommunicationContract currentContract = wrapper.GetCurrentRemoteSideCommunicationContract();

            try
            {
                bool isNewRemoteSide = false;
                bool lockTaken       = false;
                try
                {
                    syncRoot.Enter(ref lockTaken);

                    RemoteRequestStarted(remoteID, request);

                    IRemoteSideCommunicationContract lastKnownContract = null;
                    if (communicationContractsByRemoteSideIDDict.TryGetValue(remoteID, out lastKnownContract))
                    {
                        // known client comes in again
                        if (lastKnownContract != currentContract)
                        {
                            communicationContractsByRemoteSideIDDict[remoteID] = currentContract;
                            remoteSideIDsByCommunicationContractDict.Remove(lastKnownContract);
                            remoteSideIDsByCommunicationContractDict.Add(currentContract, remoteID);

                            wrapper.Closed  += new EventHandler(Channel_Closed);
                            wrapper.Faulted += new EventHandler(Channel_Faulted);

                            RemoteSideReconnected(remoteID);
                            //Log.Debug("Client {0} reconnected.", remoteID.ToString()); //LOCSTR
                            System.Diagnostics.Debug.WriteLine("Client {0} reconnected.", remoteID.ToString()); //LOCSTR
                        }

                        KnownRemoteSideRequest(remoteID);
                    }
                    else
                    {
                        wrapper.Closed  += new EventHandler(Channel_Closed);
                        wrapper.Faulted += new EventHandler(Channel_Faulted);

                        remoteSideIDsByCommunicationContractDict.Add(currentContract, remoteID);
                        communicationContractsByRemoteSideIDDict.Add(remoteID, currentContract);

                        NewRemoteSideConnected(remoteID);

                        isNewRemoteSide = true;
                    }
                }
                finally
                {
                    if (lockTaken)
                    {
                        syncRoot.Exit();
                    }
                }

                if (isNewRemoteSide)
                {
                    OnRemoteSideConnected(remoteID);
                }

                // process request
                ret = remoteOperationHandler.ExecuteRequest(request, typeof(RemoteCallableTypeAttribute), typeof(RemoteCallableFuncAttribute));
            }
            finally
            {
                RemoteRequestFinished(remoteID, request);
            }

            return(ret);
        }