Пример #1
0
        private void RegisterService(string serviceId, string serviceResponseReceiverId)
        {
            using (EneterTrace.Entering())
            {
                bool            anIsNewServiceRegistered = false;
                TServiceContext aServiceContext          = null;
                using (ThreadLock.Lock(myConnectionLock))
                {
                    aServiceContext = myConnectedServices.FirstOrDefault(x => x.ServiceId == serviceId || x.ServiceResponseReceiverId == serviceResponseReceiverId);
                    if (aServiceContext == null)
                    {
                        aServiceContext = new TServiceContext(serviceId, serviceResponseReceiverId);
                        myConnectedServices.Add(aServiceContext);
                        anIsNewServiceRegistered = true;
                    }
                }

                if (anIsNewServiceRegistered)
                {
                    if (ServiceRegistered != null)
                    {
                        try
                        {
                            MessageBusServiceEventArgs anEvent = new MessageBusServiceEventArgs(serviceId, serviceResponseReceiverId);
                            ServiceRegistered(this, anEvent);
                        }
                        catch (Exception err)
                        {
                            EneterTrace.Error(TracedObject + ErrorHandler.DetectedException, err);
                        }
                    }
                }
                else
                {
                    // If this connection has registered the same service then do nothing.
                    if (aServiceContext.ServiceId == serviceId &&
                        aServiceContext.ServiceResponseReceiverId == serviceResponseReceiverId)
                    {
                    }
                    else if (aServiceContext.ServiceId != serviceId &&
                             aServiceContext.ServiceResponseReceiverId == serviceResponseReceiverId)
                    {
                        EneterTrace.Warning("The connection has already registered a different service '" + aServiceContext.ServiceId + "'. Connection will be disconnected.");
                        UnregisterService(serviceResponseReceiverId);
                    }
                    else if (aServiceContext.ServiceId == serviceId &&
                             aServiceContext.ServiceResponseReceiverId != serviceResponseReceiverId)
                    {
                        EneterTrace.Warning("Service '" + serviceId + "' is already registered. Connection will be disconnected.");
                        UnregisterService(serviceResponseReceiverId);
                    }
                }
            }
        }
Пример #2
0
        private void UnregisterService(string serviceResponseReceiverId)
        {
            using (EneterTrace.Entering())
            {
                List <string> aClientsToDisconnect = new List <string>();

                string aServiceId = null;
                using (ThreadLock.Lock(myConnectionLock))
                {
                    // Remove the service.
                    myConnectedServices.RemoveWhere(x =>
                    {
                        if (x.ServiceResponseReceiverId == serviceResponseReceiverId)
                        {
                            aServiceId = x.ServiceId;
                            return(true);
                        }

                        return(false);
                    });

                    // Remove all clients connected to the service.
                    myConnectedClients.RemoveWhere(x =>
                    {
                        if (x.ServiceResponseReceiverId == serviceResponseReceiverId)
                        {
                            aClientsToDisconnect.Add(x.ClientResponseReceiverId);

                            // Indicate the item shall be removed.
                            return(true);
                        }

                        return(false);
                    });
                }

                // Close connections with clients.
                if (myClientConnector.IsDuplexInputChannelAttached)
                {
                    foreach (string aClientResponseReceiverId in aClientsToDisconnect)
                    {
                        IDuplexInputChannel anInputChannel = myClientConnector.AttachedDuplexInputChannel;
                        if (anInputChannel != null)
                        {
                            anInputChannel.DisconnectResponseReceiver(aClientResponseReceiverId);
                        }
                    }
                }

                IDuplexInputChannel anInputChannel2 = myServiceConnector.AttachedDuplexInputChannel;
                if (anInputChannel2 != null)
                {
                    anInputChannel2.DisconnectResponseReceiver(serviceResponseReceiverId);
                }

                if (ServiceUnregistered != null && !string.IsNullOrEmpty(aServiceId))
                {
                    EneterTrace.Debug("SERVICE '" + aServiceId + "' UNREGISTERED");

                    try
                    {
                        MessageBusServiceEventArgs anEvent = new MessageBusServiceEventArgs(aServiceId, serviceResponseReceiverId);
                        ServiceUnregistered(this, anEvent);
                    }
                    catch (Exception err)
                    {
                        EneterTrace.Error(TracedObject + ErrorHandler.DetectedException, err);
                    }
                }
            }
        }