public override void BindToName(Type serializedType, out string assemblyName, out string typeName) { var mappedType = _eventMapper.GetMappedTypeFor(serializedType); assemblyName = mappedType.Assembly.FullName; typeName = mappedType.FullName; }
public override void BindToName(Type serializedType, out string assemblyName, out string typeName) { var mappedType = _eventMapper.GetMappedTypeFor(serializedType) ?? serializedType; assemblyName = null; typeName = mappedType.AssemblyQualifiedName; }
public void Handle(IState state, IEvent @event) { var eventType = @event.GetType(); if (!eventType.IsInterface) { eventType = _mapper.GetMappedTypeFor(eventType); } // Todo: can suport "named" events with an attribute here so instead of routing based on object type // route based on event name. Action <TState, object> eventMutator; if (!_mutators.TryGetValue($"Handle.{eventType.Name}", out eventMutator)) { throw new NoRouteException($"State {typeof(TState).Name} does not have handler for event {eventType.Name}"); } eventMutator((TState)state, @event); }
public Task <long> WriteEvents(string stream, IFullEvent[] events, IDictionary <string, string> commitHeaders, long?expectedVersion = null) { _cache.Evict(stream); var translatedEvents = events.Select(e => { var descriptor = new EventDescriptor { EventId = e.EventId ?? Guid.NewGuid(), CommitHeaders = (commitHeaders ?? new Dictionary <string, string>()).Merge(new Dictionary <string, string> { [Defaults.InstanceHeader] = Defaults.Instance.ToString(), [Defaults.EndpointHeader] = Configuration.Settings.Endpoint, [Defaults.EndpointVersionHeader] = Configuration.Settings.EndpointVersion.ToString(), [Defaults.AggregatesVersionHeader] = Configuration.Settings.AggregatesVersion.ToString(), [Defaults.MachineHeader] = Environment.MachineName }), Compressed = _compress.HasFlag(Compression.Events), EntityType = e.Descriptor.EntityType, StreamType = e.Descriptor.StreamType, Bucket = e.Descriptor.Bucket, StreamId = e.Descriptor.StreamId, Parents = e.Descriptor.Parents, Version = e.Descriptor.Version, Timestamp = e.Descriptor.Timestamp, Headers = e.Descriptor.Headers, }; var mappedType = e.Event.GetType(); if (!mappedType.IsInterface) { mappedType = _mapper.GetMappedTypeFor(mappedType) ?? mappedType; } var @event = _serializer.Serialize(e.Event); if (_compress.HasFlag(Compression.Events)) { descriptor.Compressed = true; @event = @event.Compress(); } var metadata = _serializer.Serialize(descriptor); return(new EventData( descriptor.EventId, mappedType.AssemblyQualifiedName, !descriptor.Compressed, @event, metadata )); }).ToArray(); return(DoWrite(stream, translatedEvents, expectedVersion)); }
Type GetMappedType(Type messageType) { if (messageType.IsInterface) { var mappedTypeFor = messageMapper.GetMappedTypeFor(messageType); if (mappedTypeFor != null) { return(mappedTypeFor); } } return(messageType); }
public override void BindToName(Type serializedType, out string assemblyName, out string typeName) { // Todo: this is where I would substitute the type info with a unique string to represent the object without namespaces var mappedType = serializedType; if (!serializedType.IsInterface) { mappedType = _mapper.GetMappedTypeFor(serializedType) ?? serializedType; } assemblyName = null; typeName = mappedType.AssemblyQualifiedName; }
protected override JsonObjectContract CreateObjectContract(Type objectType) { if (objectType.IsInterface) { var mappedType = _eventMapper.GetMappedTypeFor(objectType); var objectContract = base.CreateObjectContract(mappedType); objectContract.DefaultCreator = () => _eventFactory.Create(mappedType); return(objectContract); } return(base.CreateObjectContract(objectType)); }
protected override JsonObjectContract CreateObjectContract(Type objectType) { var mappedType = _eventMapper.GetMappedTypeFor(objectType); if (mappedType == null) { return(base.CreateObjectContract(objectType)); } var jsonObjectContract = base.CreateObjectContract(mappedType); jsonObjectContract.DefaultCreator = () => _eventFactory.Create(mappedType); return(jsonObjectContract); }
public Task <long> WriteEvents(string stream, IFullEvent[] events, IDictionary <string, string> commitHeaders, long?expectedVersion = null) { Logger.Write(LogLevel.Debug, () => $"Writing {events.Count()} events to stream id [{stream}]. Expected version: {expectedVersion}"); var translatedEvents = events.Select(e => { var descriptor = new EventDescriptor { EventId = e.EventId ?? Guid.NewGuid(), CommitHeaders = commitHeaders ?? new Dictionary <string, string>(), Compressed = _compress.HasFlag(Compression.Events), EntityType = e.Descriptor.EntityType, StreamType = e.Descriptor.StreamType, Bucket = e.Descriptor.Bucket, StreamId = e.Descriptor.StreamId, Parents = e.Descriptor.Parents, Version = e.Descriptor.Version, Timestamp = e.Descriptor.Timestamp, Headers = e.Descriptor.Headers }; var mappedType = e.Event.GetType(); if (!mappedType.IsInterface) { mappedType = _mapper.GetMappedTypeFor(mappedType) ?? mappedType; } var @event = _serializer.Serialize(e.Event); if (_compress.HasFlag(Compression.Events)) { descriptor.Compressed = true; @event = @event.Compress(); } var metadata = _serializer.Serialize(descriptor); return(new EventData( descriptor.EventId, mappedType.AssemblyQualifiedName, !descriptor.Compressed, @event, metadata )); }).ToArray(); return(DoWrite(stream, translatedEvents, expectedVersion)); }
protected override JsonObjectContract CreateObjectContract(Type objectType) { var mappedTypeFor = objectType; _mapper.Initialize(objectType); mappedTypeFor = _mapper.GetMappedTypeFor(objectType); if (mappedTypeFor == null) { return(base.CreateObjectContract(objectType)); } var objectContract = base.CreateObjectContract(objectType); objectContract.DefaultCreator = () => _factory.Create(objectType); return(objectContract); }
public override Task Invoke(IOutgoingPhysicalMessageContext context, Func <Task> next) { var messageTypeKey = "NServiceBus.EnclosedMessageTypes"; //var headers = context.Headers; if (!context.Headers.TryGetValue(messageTypeKey, out var messageType)) { return(next()); } if (messageType.IndexOf(';') != -1) { messageType = messageType.Substring(0, messageType.IndexOf(';')); } // Don't use context.Message.Instance because it will be IEvent_impl var type = Type.GetType(messageType, false); if (type == null) { Logger.WarnEvent("UnknownType", "{MessageType} sent - but could not load type?", messageType); return(next()); } if (!type.IsInterface) { type = _mapper.GetMappedTypeFor(type) ?? type; } var definition = _registrar.GetVersionedName(type); if (definition == null) { Logger.WarnEvent("UnknownMessage", "{MessageType} has no known definition", type.FullName); return(next()); } context.Headers[messageTypeKey] = definition; return(next()); }
public async Task SendLocal(IFullMessage message, IDictionary <string, string> headers = null) { while (!Bus.BusOnline) { await Task.Delay(100).ConfigureAwait(false); } headers = headers ?? new Dictionary <string, string>(); var contextBag = new ContextBag(); // Hack to get all the events to invoker without NSB deserializing contextBag.Set(Defaults.LocalHeader, message.Message); var processed = false; var numberOfDeliveryAttempts = 0; var messageType = message.Message.GetType(); if (!messageType.IsInterface) { messageType = _mapper.GetMappedTypeFor(messageType) ?? messageType; } var finalHeaders = message.Headers.Merge(headers); finalHeaders[Headers.EnclosedMessageTypes] = _registrar.GetVersionedName(messageType); finalHeaders[Headers.MessageIntent] = MessageIntentEnum.Send.ToString(); var messageId = Guid.NewGuid().ToString(); var corrId = ""; if (finalHeaders.ContainsKey($"{Defaults.PrefixHeader}.{Defaults.MessageIdHeader}")) { messageId = finalHeaders[$"{Defaults.PrefixHeader}.{Defaults.MessageIdHeader}"]; } if (finalHeaders.ContainsKey($"{Defaults.PrefixHeader}.{Defaults.CorrelationIdHeader}")) { corrId = finalHeaders[$"{Defaults.PrefixHeader}.{Defaults.CorrelationIdHeader}"]; } Logger.DebugEvent("SendLocal", "Starting local message [{MessageId:l}] Corr [{CorrelationId:l}]", messageId, corrId); finalHeaders[Headers.MessageId] = messageId; finalHeaders[Headers.CorrelationId] = corrId; while (!processed) { var transportTransaction = new TransportTransaction(); var tokenSource = new CancellationTokenSource(); try { var messageContext = new MessageContext(messageId, finalHeaders, Marker, transportTransaction, tokenSource, contextBag); await Bus.OnMessage(messageContext).ConfigureAwait(false); _metrics.Mark("Dispatched Messages", Unit.Message); processed = true; } catch (ObjectDisposedException) { // NSB transport has been disconnected throw new OperationCanceledException(); } catch (Exception ex) { _metrics.Mark("Dispatched Errors", Unit.Errors); Logger.DebugEvent("SendLocalException", ex, "Local message [{MessageId:l}] Corr [{CorrelationId:l}] exception", messageId, corrId); ++numberOfDeliveryAttempts; // Don't retry a cancelation if (tokenSource.IsCancellationRequested) { numberOfDeliveryAttempts = Int32.MaxValue; } var messageBytes = _serializer.Serialize(message.Message); var errorContext = new ErrorContext(ex, finalHeaders, messageId, messageBytes, transportTransaction, numberOfDeliveryAttempts); if ((await Bus.OnError(errorContext).ConfigureAwait(false)) == ErrorHandleResult.Handled || tokenSource.IsCancellationRequested) { break; } } } }
public Task <long> WriteEvents(string stream, IFullEvent[] events, IDictionary <string, string> commitHeaders, long?expectedVersion = null) { var mutators = MutationManager.Registered.ToList(); var translatedEvents = events.Select(e => { IMutating mutated = new Mutating(e.Event, e.Descriptor.Headers ?? new Dictionary <string, string>()); // use async local container first if one exists // (is set by unit of work - it creates a child container which mutators might need) IContainer container = Configuration.Settings.LocalContainer.Value; if (container == null) { container = Configuration.Settings.Container; } foreach (var type in mutators) { var mutator = (IMutate)container.TryResolve(type); if (mutator == null) { Logger.WarnEvent("MutateFailure", "Failed to construct mutator {Mutator}", type.FullName); continue; } mutated = mutator.MutateOutgoing(mutated); } var mappedType = e.Event.GetType(); if (!mappedType.IsInterface) { mappedType = _mapper.GetMappedTypeFor(mappedType) ?? mappedType; } var descriptor = new EventDescriptor { EventId = e.EventId ?? Guid.NewGuid(), CommitHeaders = (commitHeaders ?? new Dictionary <string, string>()).Merge(new Dictionary <string, string> { [Defaults.InstanceHeader] = Defaults.Instance.ToString(), [Defaults.EndpointHeader] = Configuration.Settings.Endpoint, [Defaults.EndpointVersionHeader] = Configuration.Settings.EndpointVersion.ToString(), [Defaults.AggregatesVersionHeader] = Configuration.Settings.AggregatesVersion.ToString(), [Defaults.MachineHeader] = Environment.MachineName, }), Compressed = _compress.HasFlag(Compression.Events), EntityType = e.Descriptor.EntityType, StreamType = e.Descriptor.StreamType, Bucket = e.Descriptor.Bucket, StreamId = e.Descriptor.StreamId, Parents = e.Descriptor.Parents, Version = e.Descriptor.Version, Timestamp = e.Descriptor.Timestamp, Headers = e.Descriptor.Headers, }; var eventType = _registrar.GetVersionedName(mappedType); foreach (var header in mutated.Headers) { e.Descriptor.Headers[header.Key] = header.Value; } var @event = _serializer.Serialize(mutated.Message); if (_compress.HasFlag(Compression.Events)) { descriptor.Compressed = true; @event = @event.Compress(); } var metadata = _serializer.Serialize(descriptor); return(new EventData( descriptor.EventId, eventType, !descriptor.Compressed, @event, metadata )); }).ToArray(); return(DoWrite(stream, translatedEvents, expectedVersion)); }