private void ConfigureServiceHostWorkerRole(IExtensibleCloudServiceComponent ownerRole) { var callToken = TraceManager.WorkerRoleComponent.TraceIn(ownerRole != null ? ownerRole.GetType().FullName : null); var startScopeCreateServiceHosts = TraceManager.WorkerRoleComponent.TraceStartScope(TraceLogMessages.ScopeCreateServiceHosts, callToken); if (ownerRole != null) { IList <ServiceBusHostWorkerRoleAttribute> serviceHostAttributes = FrameworkUtility.GetDeclarativeAttributes <ServiceBusHostWorkerRoleAttribute>(ownerRole.GetType()); IRoleConfigurationSettingsExtension roleConfigExtension = ownerRole.Extensions.Find <IRoleConfigurationSettingsExtension>(); if (serviceHostAttributes != null && serviceHostAttributes.Count > 0 && roleConfigExtension != null) { ServiceBusEndpointInfo endpointInfo = null; ServiceBusListenerRegistration listenerInfo = null; foreach (ServiceBusHostWorkerRoleAttribute serviceHostAttr in serviceHostAttributes) { endpointInfo = roleConfigExtension.GetServiceBusEndpoint(serviceHostAttr.ServiceBusEndpoint); if (endpointInfo != null) { listenerInfo = new ServiceBusListenerRegistration() { ServiceType = serviceHostAttr.ServiceType, EndpointInfo = endpointInfo, AutoStart = serviceHostAttr.AutoStart }; // All services that are enabled for auto-start will have their service hosts pre-initialized but not as yet openned. if (listenerInfo.AutoStart) { TraceManager.WorkerRoleComponent.TraceInfo(TraceLogMessages.AboutToCreateServiceHost, listenerInfo.ServiceType.FullName, endpointInfo.Name, endpointInfo.ServiceNamespace, endpointInfo.ServicePath, endpointInfo.EndpointType); listenerInfo.ServiceHost = new ReliableServiceBusHost <object>(ServiceBusHostFactory.CreateServiceBusHost(endpointInfo, listenerInfo.ServiceType), roleConfigExtension.CommunicationRetryPolicy); } this.serviceEndpoints.Add(endpointInfo, listenerInfo); } else { throw new CloudApplicationException(String.Format(CultureInfo.CurrentCulture, ExceptionMessages.SpecifiedServiceBusEndpointNotFound, serviceHostAttr.ServiceBusEndpoint, ServiceBusConfigurationSettings.SectionName)); } } } } TraceManager.WorkerRoleComponent.TraceEndScope(TraceLogMessages.ScopeCreateServiceHosts, startScopeCreateServiceHosts, callToken); TraceManager.WorkerRoleComponent.TraceOut(callToken); }
/// <summary> /// Notifies this extension component that it has been registered in the owner's collection of extensions. /// </summary> /// <param name="owner">The extensible owner object that aggregates this extension.</param> public void Attach(IExtensibleCloudServiceComponent owner) { owner.Extensions.Demand <IRoleConfigurationSettingsExtension>(); IRoleConfigurationSettingsExtension roleConfigExtension = owner.Extensions.Find <IRoleConfigurationSettingsExtension>(); this.serviceBusEndpoint = roleConfigExtension.GetServiceBusEndpoint(WellKnownEndpointName.InterRoleCommunication); this.retryPolicy = roleConfigExtension.CommunicationRetryPolicy; if (this.serviceBusEndpoint != null) { // Configure Service Bus credentials and entity URI. var credentials = TransportClientCredentialBase.CreateSharedSecretCredential(this.serviceBusEndpoint.IssuerName, this.serviceBusEndpoint.IssuerSecret); var address = ServiceBusEnvironment.CreateServiceUri(WellKnownProtocolScheme.ServiceBus, this.serviceBusEndpoint.ServiceNamespace, String.Empty); // Configure Service Bus messaging factory and namespace client which is required for subscription management. this.messagingFactory = MessagingFactory.Create(address, credentials); this.managementClient = new ServiceBusNamespaceClient(address, credentials); ConfigureTopicClient(); ConfigureSubscriptionClient(this.ircSubscription = ConfigureSubscription(String.Concat(SubscriptionNamePrefix, this.senderInstanceID))); // Configure event receive action. this.receiveAction = (() => { BrokeredMessage msg = null; this.retryPolicy.ExecuteAction(() => { // Make sure we are not told to stop receiving while we are retrying. if (!cts.IsCancellationRequested) { if (EventReceiver.TryReceive(Settings.EventWaitTimeout, out msg)) { try { // Make sure we are not told to stop receiving while we were waiting for a new message. if (!cts.IsCancellationRequested) { // Extract the event data from brokered message. InterRoleCommunicationEvent e = msg.GetBody <InterRoleCommunicationEvent>(); // Notify all registered subscribers. NotifySubscribers(e); // Mark brokered message as complete. msg.Complete(this.retryPolicy); } else { msg.Defer(this.retryPolicy); } } catch (Exception ex) { // Abandons a brokered message and unlocks the message. msg.Abandon(this.retryPolicy); // Log an error. TraceManager.ServiceComponent.TraceError(ex); } } } }); }); // Configure event receive complete action. this.endReceive = ((ar) => { this.receiveAction.EndInvoke(ar); if (!cts.IsCancellationRequested) { this.receiveHandle = this.receiveAction.BeginInvoke(this.endReceive, null); } }); // Configure event send action. this.sendAction = ((e) => { this.retryPolicy.ExecuteAction(() => { EventSender.Send(e); }); }); // Configure event send complete action. this.endSend = ((ar) => { sendAction.EndInvoke(ar); }); } else { throw new CloudApplicationException(String.Format(CultureInfo.CurrentCulture, ExceptionMessages.SpecifiedServiceBusEndpointNotFound, WellKnownEndpointName.InterRoleCommunication, ServiceBusConfigurationSettings.SectionName)); } }
private void SubmitApplication(LoanApplicationData loanApp) { LoanApplicationProcess loanRequestActivity = new LoanApplicationProcess(loanApp.ApplicationID) { ApplicantID = loanApp.Ssn, LoanApplicationSubmitted = DateTime.UtcNow }; // Ensure the availability of required extensions, throw an exception if not available. Global.WebRoleSingleton.Extensions.Demand <IActivityTrackingEventStreamExtension>(); Global.WebRoleSingleton.Extensions.Demand <IRulesEngineServiceClientExtension>(); IActivityTrackingEventStreamExtension trackingEventStream = Global.WebRoleSingleton.Extensions.Find <IActivityTrackingEventStreamExtension>(); IRulesEngineServiceClientExtension ruleEngineClient = Global.WebRoleSingleton.Extensions.Find <IRulesEngineServiceClientExtension>(); IRoleConfigurationSettingsExtension roleConfig = Global.WebRoleSingleton.Extensions.Find <IRoleConfigurationSettingsExtension>(); // Tells the event stream to invoke the BeginActivity operation. trackingEventStream.BeginActivity(loanRequestActivity); // Call BRE rule to obtain a pre-approval. SimpleValueFact result = ruleEngineClient.ExecutePolicy <SimpleValueFact>("Contoso.Cloud.Integration.Demo.LoanPreapproval", loanApp); // Update the LoanPreapprovalReceived milestone. loanRequestActivity.LoanPreapprovalDecision = Convert.ToString(result.Value); loanRequestActivity.LoanPreapprovalReceived = DateTime.UtcNow; // Display pre-approval decision. SetResultDisplay(loanRequestActivity.LoanPreapprovalDecision); // Tells the event stream to invoke the UpdateActivity. trackingEventStream.UpdateActivity(loanRequestActivity); // Submit pre-approved loan for final processing. if (String.Compare(loanRequestActivity.LoanPreapprovalDecision, "Preapproved", true) == 0) { // Submit the loan application for processing (this will update LoanApplicationStarted, LoanFinalDecision and LoanFinalDecisionMade milestones. using (var loanProcessor = new ReliableServiceBusClient <ILoanApplicationProcessingServiceChannel>(roleConfig.GetServiceBusEndpoint("LoanApplication"), roleConfig.CommunicationRetryPolicy)) { loanProcessor.RetryPolicy.ExecuteAction(() => { loanProcessor.Client.Submit(loanApp); }); } } // Update the LoanFinalDecisionReceived milestone. loanRequestActivity.LoanFinalDecisionReceived = DateTime.UtcNow; // Tells the event stream to invoke the UpdateActivity and EndActivity operations. trackingEventStream.CompleteActivity(loanRequestActivity); }