ISagaScopeContext <T> ISagaScopeProvider <TSaga> .GetScope <T>(ConsumeContext <T> context) { if (context.TryGetPayload <IServiceScope>(out var existingServiceScope)) { return(new ExistingSagaScopeContext <T>(context)); } var scopeFactory = _serviceProvider.GetRequiredService <IServiceScopeFactory>(); var scope = scopeFactory.CreateScope(); try { var proxy = new ConsumeContextProxy <T>(context, new PayloadCacheScope(context)); var sagaScope = scope; proxy.GetOrAddPayload(() => sagaScope); proxy.GetOrAddPayload(() => sagaScope.ServiceProvider); foreach (Action <ConsumeContext> scopeAction in _scopeActions) { scopeAction(proxy); } return(new CreatedSagaScopeContext <IServiceScope, T>(sagaScope, proxy)); } catch { scope.Dispose(); throw; } }
ISagaScopeContext <T> ISagaScopeProvider <TSaga> .GetScope <T>(ConsumeContext <T> context) { if (context.TryGetPayload <IUnityContainer>(out var existingScope)) { return(new ExistingSagaScopeContext <T>(context)); } var scope = _container.CreateChildContainer(); try { var proxy = new ConsumeContextProxy <T>(context, new PayloadCacheScope(context)); var consumerScope = scope; proxy.GetOrAddPayload(() => consumerScope); return(new CreatedSagaScopeContext <IUnityContainer, T>(consumerScope, proxy)); } catch { scope.Dispose(); throw; } }
public ISagaScopeContext <T> GetScope <T>(ConsumeContext <T> context) where T : class { if (context.TryGetPayload <Scope>(out var existingScope)) { existingScope.UpdateScope(context); return(new ExistingSagaScopeContext <T>(context)); } var scope = AsyncScopedLifestyle.BeginScope(_container); try { scope.UpdateScope(context); var proxy = new ConsumeContextProxy <T>(context, new PayloadCacheScope(context)); proxy.UpdatePayload(scope); foreach (Action <ConsumeContext> scopeAction in _scopeActions) { scopeAction(proxy); } return(new CreatedSagaScopeContext <Scope, T>(scope, proxy)); } catch { scope.Dispose(); throw; } }
ISagaScopeContext <T> ISagaScopeProvider <TSaga> .GetScope <T>(ConsumeContext <T> context) { if (context.TryGetPayload <ILifetimeScope>(out _)) { return(new ExistingSagaScopeContext <T>(context)); } var parentLifetimeScope = _scopeProvider.GetLifetimeScope(context); var lifetimeScope = parentLifetimeScope.BeginLifetimeScope(_name, builder => { builder.ConfigureScope(context); _configureScope?.Invoke(builder, context); }); try { var proxy = new ConsumeContextProxy <T>(context, new PayloadCacheScope(context)); proxy.UpdatePayload(lifetimeScope); foreach (Action <ConsumeContext> scopeAction in _scopeActions) { scopeAction(proxy); } return(new CreatedSagaScopeContext <ILifetimeScope, T>(lifetimeScope, proxy)); } catch { lifetimeScope.Dispose(); throw; } }
public ISagaScopeContext <T> GetScope <T>(ConsumeContext <T> context) where T : class { if (context.TryGetPayload <Scope>(out var existingScope)) { return(new ExistingSagaScopeContext <T>(context)); } var scope = AsyncScopedLifestyle.BeginScope(_container); try { var proxy = new ConsumeContextProxy <T>(context, new PayloadCacheScope(context)); var consumerScope = scope; proxy.GetOrAddPayload(() => consumerScope); return(new CreatedSagaScopeContext <Scope, T>(consumerScope, proxy)); } catch { scope.Dispose(); throw; } }
public ISagaScopeContext <T> GetScope <T>(ConsumeContext <T> context) where T : class { if (context.TryGetPayload <IContainer>(out var existingContainer)) { return(new ExistingSagaScopeContext <T>(context)); } var container = _container?.CreateNestedContainer(context) ?? _context?.CreateNestedContainer(context); try { var proxy = new ConsumeContextProxy <T>(context, new PayloadCacheScope(context)); var consumerContainer = container; proxy.GetOrAddPayload(() => consumerContainer); foreach (Action <ConsumeContext> scopeAction in _scopeActions) { scopeAction(proxy); } return(new CreatedSagaScopeContext <IContainer, T>(consumerContainer, proxy)); } catch { container.Dispose(); throw; } }
public ISagaScopeContext <T> GetScope <T>(ConsumeContext <T> context) where T : class { if (context.TryGetPayload <IKernel>(out var kernel)) { kernel.UpdateScope(context); return(new ExistingSagaScopeContext <T>(context)); } var scope = _kernel.CreateNewOrUseExistingMessageScope(); try { _kernel.UpdateScope(context); var proxy = new ConsumeContextProxy <T>(context, new PayloadCacheScope(context)); proxy.UpdatePayload(_kernel); foreach (Action <ConsumeContext> scopeAction in _scopeActions) { scopeAction(proxy); } return(new CreatedSagaScopeContext <IDisposable, T>(scope, proxy)); } catch { scope.Dispose(); throw; } }
public ISagaScopeContext <T> GetScope <T>(ConsumeContext <T> context) where T : class { if (context.TryGetPayload <INestedContainer>(out var existingNestedContainer)) { existingNestedContainer.Inject <ConsumeContext>(context); return(new ExistingSagaScopeContext <T>(context)); } var nestedContainer = _container.GetNestedContainer(context); try { var proxy = new ConsumeContextProxy <T>(context, new PayloadCacheScope(context)); proxy.UpdatePayload(nestedContainer); foreach (Action <ConsumeContext> scopeAction in _scopeActions) { scopeAction(proxy); } return(new CreatedSagaScopeContext <INestedContainer, T>(nestedContainer, proxy)); } catch { nestedContainer.Dispose(); throw; } }
public ISagaScopeContext <T> GetScope <T>(ConsumeContext <T> context) where T : class { if (context.TryGetPayload <IKernel>(out _)) { return(new ExistingSagaScopeContext <T>(context)); } var scope = _container.BeginScope(); _container.Register(Component.For <ConsumeContext, ConsumeContext <T> >().Instance(context)); try { var proxy = new ConsumeContextProxy <T>(context, new PayloadCacheScope(context)); var consumerContainer = _container; proxy.GetOrAddPayload(() => consumerContainer); foreach (Action <ConsumeContext> scopeAction in _scopeActions) { scopeAction(proxy); } return(new CreatedSagaScopeContext <IDisposable, T>(scope, proxy)); } catch { scope.Dispose(); throw; } }
public static ConsumeContext <T> CreateScope <T, TScope>(this ConsumeContext <T> context, TScope scope) where T : class where TScope : class { var proxy = new ConsumeContextProxy <T>(context, new PayloadCacheScope(context)); proxy.GetOrAddPayload(() => scope); return(proxy); }
public async Task Should_add_the_expected_properties_to_the_dependency_telemetry() { // Arrange. var messageId = Guid.NewGuid(); var conversationId = Guid.NewGuid(); var correlationId = Guid.NewGuid(); var requestId = Guid.NewGuid(); var inputAddress = new Uri("http://masstransit-project.com/"); var destinationAddress = new Uri("sb://my-organization.servicebus.windows.net/MyNamespace/MyMessage"); var mockReceiveContext = new Mock <ReceiveContext>(); mockReceiveContext.Setup(c => c.InputAddress).Returns(inputAddress); var mockConsumeContext = new Mock <ConsumeContext <PingMessage> >(); mockConsumeContext.Setup(c => c.ReceiveContext).Returns(mockReceiveContext.Object); mockConsumeContext.Setup(c => c.Headers).Returns(_mockHeaders.Object); mockConsumeContext.Setup(c => c.MessageId).Returns(messageId); mockConsumeContext.Setup(c => c.ConversationId).Returns(conversationId); mockConsumeContext.Setup(c => c.CorrelationId).Returns(correlationId); mockConsumeContext.Setup(c => c.RequestId).Returns(requestId); mockConsumeContext.Setup(c => c.DestinationAddress).Returns(destinationAddress); var consumeContextProxy = new ConsumeContextProxy <PingMessage>(mockConsumeContext.Object, new Mock <IPayloadCache>().Object); var capturedRequestTelemetry = default(RequestTelemetry); var filter = new ApplicationInsightsConsumeFilter <PingMessage>(new TelemetryClient(), "", "", (holder, context) => capturedRequestTelemetry = holder.Telemetry); // Act. await filter.Send(consumeContextProxy, new Mock <IPipe <ConsumeContext <PingMessage> > >().Object); // Assert. Assert.IsNotNull(capturedRequestTelemetry); Assert.AreEqual(capturedRequestTelemetry.Properties["MessageId"], messageId.ToString()); Assert.AreEqual(capturedRequestTelemetry.Properties["MessageType"], TypeMetadataCache <PingMessage> .ShortName); Assert.AreEqual(capturedRequestTelemetry.Properties["ConversationId"], conversationId.ToString()); Assert.AreEqual(capturedRequestTelemetry.Properties["CorrelationId"], correlationId.ToString()); Assert.AreEqual(capturedRequestTelemetry.Properties["DestinationAddress"], destinationAddress.ToString()); Assert.AreEqual(capturedRequestTelemetry.Properties["InputAddress"], inputAddress.ToString()); Assert.AreEqual(capturedRequestTelemetry.Properties["RequestId"], requestId.ToString()); }
public async Task Should_not_fail_if_properties_are_already_defined() { var messageId = Guid.NewGuid(); var conversationId = Guid.NewGuid(); var correlationId = Guid.NewGuid(); var requestId = Guid.NewGuid(); var inputAddress = new Uri("http://masstransit-project.com/"); var destinationAddress = new Uri("sb://my-organization.servicebus.windows.net/MyNamespace/MyMessage"); var mockReceiveContext = new Mock <ReceiveContext>(); mockReceiveContext.Setup(c => c.InputAddress).Returns(inputAddress); var mockConsumeContext = new Mock <ConsumeContext <PingMessage> >(); mockConsumeContext.Setup(c => c.ReceiveContext).Returns(mockReceiveContext.Object); mockConsumeContext.Setup(c => c.Headers).Returns(_mockHeaders.Object); mockConsumeContext.Setup(c => c.MessageId).Returns(messageId); mockConsumeContext.Setup(c => c.ConversationId).Returns(conversationId); mockConsumeContext.Setup(c => c.CorrelationId).Returns(correlationId); mockConsumeContext.Setup(c => c.RequestId).Returns(requestId); mockConsumeContext.Setup(c => c.DestinationAddress).Returns(destinationAddress); var consumeContextProxy = new ConsumeContextProxy <PingMessage>(mockConsumeContext.Object, new Mock <IPayloadCache>().Object); var capturedTelemetry = default(RequestTelemetry); var filter = new ApplicationInsightsConsumeFilter <PingMessage>(new TelemetryClient(), "", "", (holder, context) => capturedTelemetry = holder.Telemetry); await filter.Send(consumeContextProxy, new Mock <IPipe <ConsumeContext <PingMessage> > >().Object); var configuration = TelemetryConfiguration.CreateDefault(); configuration.TelemetryInitializers.Add(new CopyPropertiesTelemetryInitializer(capturedTelemetry)); var telemetryClient = new TelemetryClient(configuration); filter = new ApplicationInsightsConsumeFilter <PingMessage>(telemetryClient, "", "", (h, c) => { }); // Act, Assert Assert.That(async() => await filter.Send(consumeContextProxy, new Mock <IPipe <ConsumeContext <PingMessage> > >().Object), Throws.Nothing); }