コード例 #1
0
        /// <summary>
        /// Registers with the <see cref="ServiceBusService"/> to produce or consume <see cref="Message"/>s.
        /// </summary>
        /// <param name="request">An <see cref="RegistrationRequest"/> containing registration data.</param>
        public virtual void Register(RegistrationRequest request)
        {
            // Initialize if uninitialized.
            Initialize();

            // Save client information if not already present.
            ClientInfo client = null;
            if (OperationContext.Current != null)
            {
                m_clientsLock.EnterUpgradeableReadLock();
                try
                {
                    if (!m_clients.TryGetValue(OperationContext.Current.SessionId, out client))
                    {
                        m_clientsLock.EnterWriteLock();
                        try
                        {
                            client = new ClientInfo(OperationContext.Current);
                            m_clients.Add(client.SessionId, client);
                            client.OperationContext.Channel.Faulted += OnChannelFaulted;
                            client.OperationContext.Channel.Closing += OnChannelClosing;
                        }
                        finally
                        {
                            m_clientsLock.ExitWriteLock();
                        }
                    }
                }
                finally
                {
                    m_clientsLock.ExitUpgradeableReadLock();
                }
            }

            // Retrieve registration information.
            RegistrationInfo registration = null;
            if (request.MessageType == MessageType.Queue)
            {
                // Queue
                m_queuesLock.EnterUpgradeableReadLock();
                try
                {
                    if (!m_queues.TryGetValue(request.MessageName, out registration))
                    {
                        m_queuesLock.EnterWriteLock();
                        try
                        {
                            registration = new RegistrationInfo(request);
                            m_queues.Add(request.MessageName, registration);
                        }
                        finally
                        {
                            m_queuesLock.ExitWriteLock();
                        }
                    }
                }
                finally
                {
                    m_queuesLock.ExitUpgradeableReadLock();
                }
            }
            else if (request.MessageType == MessageType.Topic)
            {
                // Topic
                m_topicsLock.EnterUpgradeableReadLock();
                try
                {
                    if (!m_topics.TryGetValue(request.MessageName, out registration))
                    {
                        m_topicsLock.EnterWriteLock();
                        try
                        {
                            registration = new RegistrationInfo(request);
                            m_topics.Add(request.MessageName, registration);
                        }
                        finally
                        {
                            m_topicsLock.ExitWriteLock();
                        }
                    }
                }
                finally
                {
                    m_topicsLock.ExitUpgradeableReadLock();
                }
            }
            else
            {
                // Unsupported
                throw new NotSupportedException(string.Format("Message type '{0}' is not supported by this operation", request.MessageType));
            }

            // Update registration information.
            if (registration != null && client != null)
            {
                List<ClientInfo> clients = (request.RegistrationType == RegistrationType.Produce ? registration.Producers : registration.Consumers);
                lock (clients)
                {
                    if (!clients.Contains(client))
                        clients.Add(client);
                }
            }
        }
コード例 #2
0
 public PublishContext(Message message, RegistrationInfo registration)
 {
     Message = message;
     Registration = registration;
 }