private ISimpleQueueListener AttachService(string storageAccountConnectionString, object service)
        {
            // Search interfaces for 'QueuedServiceAttribute' def and get name
            string name = (from interfaceType in service.GetType().GetInterfaces()
                           let attr = interfaceType.GetCustomAttributes(typeof (QueuedServiceAttribute), false)
                           where attr.Any()
                           select ((QueuedServiceAttribute) attr[0]).Name ?? interfaceType.Name).FirstOrDefault();

            if(name == null)
            {
                throw new InvalidOperationException("Service type '" + service.GetType().Name + "' must inherit from an interface marked with the [QueuedService] attribute");
            }

            var location = new CloudQueueDescription { QueueName = name, StorageAccount = storageAccountConnectionString };
            var listener = new CloudQueueListenerExtension<QueueMessage>(location, _component);
            return new AzureSimpleQueueListener(listener, service);
        }
Пример #2
0
        /// <summary>
        /// Extends the OnStart phase that is called by Windows Azure runtime to initialize the role instance.
        /// </summary>
        /// <returns>True if initialization succeeds, otherwise false.</returns>
        protected override bool OnRoleStart()
        {
            this.EnsureExists <WorkItemProcessorConfigurationExtension>();
            this.EnsureExists <WorkQueueLocationResolverExtension>();
            this.EnsureExists <ScalableTransformServiceClientExtension>();
            this.EnsureExists <EndpointConfigurationDiscoveryExtension>();
            this.EnsureExists <RulesEngineServiceClientExtension>();
            this.EnsureExists <InterRoleEventSubscriberExtension>();

            IWorkItemProcessorConfigurationExtension configSettingsExtension = Extensions.Find <IWorkItemProcessorConfigurationExtension>();
            IEndpointConfigurationDiscoveryExtension discoveryExtension      = Extensions.Find <IEndpointConfigurationDiscoveryExtension>();
            IInterRoleCommunicationExtension         interCommExtension      = Extensions.Find <IInterRoleCommunicationExtension>();
            IInterRoleEventSubscriberExtension       interCommSubscriber     = Extensions.Find <IInterRoleEventSubscriberExtension>();

            CloudQueueLocation inputQueueLocation = new CloudQueueLocation()
            {
                StorageAccount    = configSettingsExtension.Settings.CloudStorageAccount,
                QueueName         = configSettingsExtension.Settings.WorkItemQueue,
                VisibilityTimeout = configSettingsExtension.Settings.WorkItemQueueVisibilityTimeout
            };

            CloudQueueLocation outputQueueLocation = new CloudQueueLocation()
            {
                StorageAccount    = configSettingsExtension.Settings.CloudStorageAccount,
                QueueName         = configSettingsExtension.Settings.OutputQueue,
                VisibilityTimeout = configSettingsExtension.Settings.OutputQueueVisibilityTimeout
            };

            // Instantiate queue listeners.
            var inputQueueListener  = new CloudQueueListenerExtension <XDocument>(inputQueueLocation);
            var outputQueueListener = new CloudQueueListenerExtension <XDocument>(outputQueueLocation);

            // Configure the input queue listener.
            inputQueueListener.QueueEmpty      += HandleQueueEmptyEvent;
            inputQueueListener.DequeueBatchSize = configSettingsExtension.Settings.DequeueBatchSize;
            inputQueueListener.DequeueInterval  = configSettingsExtension.Settings.MinimumIdleInterval;

            // Configure the output queue listener.
            outputQueueListener.QueueEmpty      += HandleQueueEmptyEvent;
            outputQueueListener.DequeueBatchSize = configSettingsExtension.Settings.DequeueBatchSize;
            outputQueueListener.DequeueInterval  = configSettingsExtension.Settings.MinimumIdleInterval;

            // Instantiate queue subscribers.
            InputQueueSubscriberExtension  inputQueueSubscriber  = new InputQueueSubscriberExtension();
            OutputQueueSubscriberExtension outputQueueSubscriber = new OutputQueueSubscriberExtension();

            // Register subscribers with queue listeners.
            inputQueueListener.Subscribe(inputQueueSubscriber);
            outputQueueListener.Subscribe(outputQueueSubscriber);

            // Register custom extensions for this worker role.
            Extensions.Add(inputQueueSubscriber);
            Extensions.Add(outputQueueSubscriber);
            Extensions.Add(outputQueueListener);
            Extensions.Add(inputQueueListener);

            var contractTypeMatchCondition = ServiceEndpointDiscoveryCondition.ContractTypeExactMatch(typeof(IScalableTransformationServiceContract));
            var bindingTypeMatchCondition  = ServiceEndpointDiscoveryCondition.BindingTypeExactMatch(typeof(NetTcpRelayBinding));

            discoveryExtension.RegisterDiscoveryAction(new[] { contractTypeMatchCondition, bindingTypeMatchCondition }, (endpoint) =>
            {
                NetTcpRelayBinding relayBinding = endpoint.Binding as NetTcpRelayBinding;
                if (relayBinding != null)
                {
                    relayBinding.TransferMode = TransferMode.Streamed;
                }
            });

            // Register a subscriber for all inter-role communication events.
            if (interCommExtension != null && interCommSubscriber != null)
            {
                this.interCommSubscription = interCommExtension.Subscribe(interCommSubscriber);
            }

            return(true);
        }