/// <summary> /// Sets up azure storage command auditing for output to a queue. This is best suited for scenarios /// where there are multiple auditors or storage mechanisms in the audit pipeline as it enables /// execution of the command dispatch pipeline to rapidly continue but still with a guarantee /// that the command will be audited. /// /// Generally when configuring this auditor no other auditors are configured - but you can. /// </summary> /// <param name="dependencyResolver">The dependency resolver</param> /// <param name="queue">The queue to audit via</param> /// <param name="blobContainer">The blob container to store the payload to. If this is set then the /// payload is stored before the item is queued, if left null then the payload will be serialized /// into the queue item. The default setting of null is the more performant and common case, setting /// the container here is only useful for very large command payloads that won't fit inside a queue /// item. If the payload is stored in the blob container specified here then there will be no way /// for downstream auditors to access it from the AuditItem model - it will be null. /// </param> /// <param name="storageStrategy"></param> /// <returns></returns> public static ICommandingDependencyResolverAdapter AddAzureStorageCommandAuditing(this ICommandingDependencyResolverAdapter dependencyResolver, CloudQueue queue, CloudBlobContainer blobContainer = null, IStorageStrategy storageStrategy = null, AzureStorageAuditorOptions options = null) { options = options ?? new AzureStorageAuditorOptions(); ICloudAuditQueueProvider cloudAuditQueueProvider = new CloudAuditQueueProvider(queue, null); ICloudAuditQueueBlobContainerProvider cloudAuditQueueBlobContainerProvider = new CloudAuditQueueBlobContainerProvider(blobContainer); dependencyResolver.RegisterInstance(cloudAuditQueueProvider); dependencyResolver.RegisterInstance(cloudAuditQueueBlobContainerProvider); dependencyResolver.TypeMapping <IAzureStorageQueueSerializer, AzureStorageQueueSerializer>(); if (options.UsePreDispatchAuditor) { EnsureCommandingRuntime(dependencyResolver); dependencyResolver.AssociatedCommandingRuntime.AddPreDispatchCommandingAuditor <AzureStorageQueueCommandAuditor>(dependencyResolver, options.AuditPreDispatchRootOnly); } if (options.UsePostDispatchAuditor) { EnsureCommandingRuntime(dependencyResolver); dependencyResolver.AssociatedCommandingRuntime.AddPostDispatchCommandingAuditor <AzureStorageQueueCommandAuditor>(dependencyResolver, options.AuditPostDispatchRootOnly); } if (options.UseExecutionAuditor) { EnsureCommandingRuntime(dependencyResolver); dependencyResolver.AssociatedCommandingRuntime.AddExecutionCommandingAuditor <AzureStorageQueueCommandAuditor>(dependencyResolver, options.AuditExecuteDispatchRootOnly); } return(dependencyResolver); }
/// <summary> /// Registers a command auditor that writes to an event hub /// </summary> /// <param name="resolver">Dependency resolver</param> /// <param name="eventHubClient">The event hub client</param> /// <param name="partitionKeyProvider">An optional partition key provider, if unspecified events will be sent unpartitioned</param> /// <param name="options">Options for the event hub auditor configuration</param> /// <returns>Dependency resolver</returns> public static ICommandingDependencyResolverAdapter AddEventHubCommandAuditing(this ICommandingDependencyResolverAdapter resolver, Microsoft.Azure.EventHubs.EventHubClient eventHubClient, IPartitionKeyProvider partitionKeyProvider = null, AzureEventHubAuditorOptions options = null) { options = options ?? new AzureEventHubAuditorOptions(); IEventHubClient client = new EventHubClient(eventHubClient); if (partitionKeyProvider == null) { partitionKeyProvider = new NullPartitionKeyProvider(); } resolver.RegisterInstance(client); resolver.RegisterInstance(partitionKeyProvider); resolver.TypeMapping <IAuditItemMapper, AuditItemMapper>(); resolver.TypeMapping <IEventHubSerializer, EventHubSerializer>(); if (options.UsePreDispatchAuditor) { EnsureRuntimeIsAssociated(resolver); resolver.AssociatedCommandingRuntime.AddPreDispatchCommandingAuditor <AzureEventHubCommandAuditor>(resolver, options.AuditPreDispatchRootOnly); } if (options.UsePostDispatchAuditor) { EnsureRuntimeIsAssociated(resolver); resolver.AssociatedCommandingRuntime.AddPostDispatchCommandingAuditor <AzureEventHubCommandAuditor>(resolver, options.AuditPostDispatchRootOnly); } if (options.UseExecutionAuditor) { EnsureRuntimeIsAssociated(resolver); resolver.AssociatedCommandingRuntime.AddExecutionCommandingAuditor <AzureEventHubCommandAuditor>(resolver, options.AuditExecuteDispatchRootOnly); } return(resolver); }
public static ICommandingDependencyResolverAdapter AddMicrosoftLoggingExtensionsAuditor(this ICommandingDependencyResolverAdapter resolver, LogLevel normalLogLevel = LogLevel.Trace, LogLevel executionFailureLogLevel = LogLevel.Warning, MicrosoftLoggingExtensionsAuditorOptions options = null) { options = options ?? new MicrosoftLoggingExtensionsAuditorOptions(); ILogLevelProvider logLevelProvider = new LogLevelProvider(normalLogLevel, executionFailureLogLevel); resolver.RegisterInstance(logLevelProvider); if (options.UsePreDispatchAuditor) { EnsureCommandingRuntime(resolver); resolver.AssociatedCommandingRuntime.AddPreDispatchCommandingAuditor <LoggerCommandAuditor>(resolver, options.AuditPreDispatchRootOnly); } if (options.UsePostDispatchAuditor) { EnsureCommandingRuntime(resolver); resolver.AssociatedCommandingRuntime.AddPostDispatchCommandingAuditor <LoggerCommandAuditor>(resolver, options.AuditPostDispatchRootOnly); } if (options.UsePreDispatchAuditor) { EnsureCommandingRuntime(resolver); resolver.AssociatedCommandingRuntime.AddExecutionCommandingAuditor <LoggerCommandAuditor>(resolver, options.AuditExecuteDispatchRootOnly); } return(resolver); }
public static ICommandingDependencyResolverAdapter AddCommandRedisCache(this ICommandingDependencyResolverAdapter resolver, Lazy <ConnectionMultiplexer> multiplexer) { ICacheAdapter adapter = new RedisCacheAdapter(multiplexer); resolver.RegisterInstance(adapter); return(resolver); }
public static ICommandingDependencyResolverAdapter AddCommandRedisCache(this ICommandingDependencyResolverAdapter resolver, string connectionString) { ICacheAdapter adapter = new RedisCacheAdapter(new Lazy <ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(connectionString))); resolver.RegisterInstance(adapter); return(resolver); }
/// <summary> /// Sets up azure storage command auditing for direct output to tables /// </summary> /// <param name="dependencyResolver">The dependency resolver</param> /// <param name="cloudStorageAccount">The cloud storage account to use for storage</param> /// <param name="commandPayloadContainer">(Optional) The blob container that </param> /// <param name="storageStrategy"></param> /// <param name="options">Auditor options</param> /// <returns></returns> public static ICommandingDependencyResolverAdapter AddAzureStorageCommandAuditing(this ICommandingDependencyResolverAdapter dependencyResolver, CloudStorageAccount cloudStorageAccount, CloudBlobContainer commandPayloadContainer = null, IStorageStrategy storageStrategy = null, AzureStorageAuditorOptions options = null) { options = options ?? new AzureStorageAuditorOptions(); if (!options.UseExecutionAuditor && !options.UsePostDispatchAuditor && !options.UsePreDispatchAuditor) { throw new AzureStorageConfigurationException("At least one auditor type must be configured"); } CloudTableClient cloudTableClient = cloudStorageAccount.CreateCloudTableClient(); if (commandPayloadContainer == null) { commandPayloadContainer = cloudStorageAccount.CreateCloudBlobClient().GetContainerReference("commandauditpayload"); } if (storageStrategy == null) { storageStrategy = new SingleTableStrategy(); } ICloudStorageProvider cloudStorageProvider = new CloudStorageProvider(cloudTableClient, commandPayloadContainer); dependencyResolver.RegisterInstance(cloudStorageProvider); dependencyResolver.RegisterInstance(storageStrategy); if (options.UsePreDispatchAuditor) { EnsureCommandingRuntime(dependencyResolver); dependencyResolver.AssociatedCommandingRuntime.AddPreDispatchCommandingAuditor <AzureStorageTableCommandAuditor>(dependencyResolver, options.AuditPreDispatchRootOnly); } if (options.UsePostDispatchAuditor) { EnsureCommandingRuntime(dependencyResolver); dependencyResolver.AssociatedCommandingRuntime.AddPostDispatchCommandingAuditor <AzureStorageTableCommandAuditor>(dependencyResolver, options.AuditPostDispatchRootOnly); } if (options.UseExecutionAuditor) { EnsureCommandingRuntime(dependencyResolver); dependencyResolver.AssociatedCommandingRuntime.AddExecutionCommandingAuditor <AzureStorageTableCommandAuditor>(dependencyResolver, options.AuditExecuteDispatchRootOnly); } return(dependencyResolver); }
private static ICommandingDependencyResolverAdapter Register <TSerializer>(ICommandingDependencyResolverAdapter dependencyResolver, HttpClient client) where TSerializer : IHttpCommandSerializer { HttpClientProvider = client == null ? new HttpClientProvider() : new HttpClientProvider(client); dependencyResolver.RegisterInstance(HttpClientProvider); dependencyResolver.TypeMapping <IHttpCommandSerializer, TSerializer>(); dependencyResolver.TypeMapping <IUriCommandQueryBuilder, UriCommandQueryBuilder>(); dependencyResolver.TypeMapping <IHttpCommandDispatcherFactory, HttpCommandDispatcherFactoryImpl>(); return(dependencyResolver); }
/// <summary> /// Registers the IAzureStorageCommandQueueProcessorFactory interface through which a audit queue processor task can be /// started /// </summary> /// <param name="dependencyResolver">The dependency resolver</param> /// <param name="queue">The queue to dequeue from</param> /// <param name="deadLetterQueue">An optional dead letter queue to place items in if errors repeatedly occur in item processing</param> /// <returns>The dependency resovler</returns> public static ICommandingDependencyResolverAdapter AddAzureStorageAuditQueueProcessor(this ICommandingDependencyResolverAdapter dependencyResolver, CloudQueue queue, CloudQueue deadLetterQueue = null) { ICloudAuditQueueProvider cloudAuditQueueProvider = new CloudAuditQueueProvider(queue, deadLetterQueue); dependencyResolver.RegisterInstance(cloudAuditQueueProvider); dependencyResolver.TypeMapping <IAzureStorageQueueSerializer, AzureStorageQueueSerializer>(); dependencyResolver.TypeMapping <IAzureStorageAuditQueueProcessorFactory, AzureStorageAuditQueueProcessorFactory>(); return(dependencyResolver); }
public static ICommandingDependencyResolverAdapter AddQueues(this ICommandingDependencyResolverAdapter dependencyResolver, Action <string, ICommand, Exception> logError = null, Action <string, ICommand, Exception> logWarning = null, Action <string, ICommand, Exception> logInfo = null) { ICommandQueueProcessorLogger logger = new CommandQueueProcessorLogger(logWarning, logError, logInfo); dependencyResolver.RegisterInstance(logger); dependencyResolver.TypeMapping <IAsynchronousBackoffPolicyFactory, AsynchronousBackoffPolicyFactory>(); dependencyResolver.TypeMapping <ICommandQueueProcessor, CommandQueueProcessor>(); return(dependencyResolver); }
/// <summary> /// Sets up the cache with the specified cache key provider /// </summary> /// <param name="resolver">The dependency resolver</param> /// <param name="cacheKeyProvider">Instance of a cache key provider</param> /// <param name="replaceDefaultCommandDispatcher">If true then the default ICommandDispatcher will be replaced with the caching variant</param> /// <param name="options">Cache options</param> /// <returns>The dependency resolver</returns> public static ICommandingDependencyResolverAdapter AddCommandCache( this ICommandingDependencyResolverAdapter resolver, ICacheKeyProvider cacheKeyProvider, bool replaceDefaultCommandDispatcher, params CacheOptions[] options) { ICacheOptionsProvider cacheOptionsProvider = new CacheOptionsProvider(options); resolver.RegisterInstance(cacheOptionsProvider); if (replaceDefaultCommandDispatcher) { resolver.TypeMapping <ICommandDispatcher, CachedCommandDispatcher>(); } else { resolver.TypeMapping <ICachedCommandDispatcher, CachedCommandDispatcher>(); } resolver.RegisterInstance(cacheKeyProvider); return(resolver); }
/// <summary> /// Registers the commanding system in an ioc container. /// If the container is not able to resolve unregistered types (for example the NetStandard Microsoft container) then /// the commandHandlerContainerRegistration should be used to perform the type registration for the handler /// </summary> /// <param name="dependencyResolver">The dependency resolver to register inside</param> /// <param name="options">Configuration options for the commanding system</param> /// <returns>The dependency resolver</returns> public ICommandRegistry AddCommanding(ICommandingDependencyResolverAdapter dependencyResolver, IOptions options = null) { options = options ?? new Options(); dependencyResolver.AssociatedCommandingRuntime = this; ICommandHandlerExecuter commandHandlerExecuter = new CommandHandlerExecuter(); dependencyResolver.RegisterInstance(commandHandlerExecuter); IOptionsProvider optionsProvider = new OptionsProvider(options); dependencyResolver.RegisterInstance(optionsProvider); // the registry is always shared, but vagaries of different IoC containers mean its dangerous to rely // on dependecy resolver checks for an existing registration lock (_registryLockObject) { if (_registry == null || options.Reset) { Action <Type> resolverContainerRegistration = type => dependencyResolver.TypeMapping(type, type); _registry = new CommandRegistry(commandHandlerExecuter, options.CommandHandlerContainerRegistration ?? resolverContainerRegistration); } dependencyResolver.RegisterInstance(_registry); } // the enricher is always shared, but vagaries of different IoC containers mean its dangerous to rely // on dependecy resolver checks for an existing registration lock (_enrichmentLockObject) { if (_dispatchContextEnrichment == null || options.Reset) { _dispatchContextEnrichment = new CommandDispatchContextEnrichment(options.Enrichers ?? new List <ICommandDispatchContextEnricher>()); } else if (options.Enrichers != null) { _dispatchContextEnrichment.AddEnrichers(options.Enrichers); } dependencyResolver.RegisterInstance(_dispatchContextEnrichment); } lock (_auditItemEnricherPipelineLockObject) { if (_auditItemEnricherPipeline == null || options.Reset) { _auditItemEnricherPipeline = new AuditItemEnricherPipeline( options.AuditItemEnricherFactoryFunc ?? (type => (IAuditItemEnricher)dependencyResolver.Resolve(type))); } dependencyResolver.RegisterInstance(_auditItemEnricherPipeline); } lock (_auditorPipelineLockObject) { if (_auditorPipeline == null || options.Reset) { _auditorPipeline = new CommandAuditPipeline(t => (ICommandAuditor)dependencyResolver.Resolve(t), dependencyResolver.Resolve <ICommandAuditSerializer>, _auditItemEnricherPipeline); } dependencyResolver.RegisterInstance(_auditorPipeline); } ICommandHandlerFactory commandHandlerFactory = new CommandHandlerFactory(options.CommandHandlerFactoryFunc ?? dependencyResolver.Resolve); IPipelineAwareCommandHandlerExecuter pipelineAwareCommandHandlerExecuter = new PipelineAwareCommandHandlerExecuter(); dependencyResolver.RegisterInstance(commandHandlerFactory); dependencyResolver.RegisterInstance(pipelineAwareCommandHandlerExecuter); dependencyResolver.TypeMapping <ICommandAuditorFactory, NullCommandAuditorFactory>(); dependencyResolver.TypeMapping <ICommandScopeManager, AsyncLocalCommandScopeManager>(); dependencyResolver.TypeMapping <IFrameworkCommandDispatcher, CommandDispatcher>(); dependencyResolver.TypeMapping <ICommandDispatcher, CommandDispatcher>(); dependencyResolver.TypeMapping <IFrameworkCommandExecuter, CommandExecuter>(); dependencyResolver.TypeMapping <ICommandExecuter, CommandExecuter>(); dependencyResolver.TypeMapping <IDirectCommandExecuter, DirectCommandExecuter>(); if (options.DisableCorrelationIds) { dependencyResolver.TypeMapping <ICommandCorrelationIdProvider, DisabledCorrelationIdProvider>(); } else { if (options.UseLocallyUniqueCorrelationIds) { dependencyResolver .TypeMapping <ICommandCorrelationIdProvider, LocallyUniqueCommandCorrelationIdProvider>(); } else { dependencyResolver.TypeMapping <ICommandCorrelationIdProvider, CommandCorrelationIdProvider>(); } } dependencyResolver.TypeMapping <ICommandAuditSerializer, CommandAuditSerializer>(); dependencyResolver.TypeMapping(typeof(ICommandExecutionExceptionHandler), options.CommandExecutionExceptionHandler ?? typeof(DefaultCommandExecutionExceptionHandler)); return(_registry); }