forked from MassTransit/MassTransit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Protocol Buffer serialization support
- Loading branch information
1 parent
312b8cd
commit 11e22d5
Showing
16 changed files
with
853 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/MassTransit.Protobuf/Configuration/ProtobufConfigurationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
namespace MassTransit.Configuration | ||
{ | ||
using MassTransit.Serialization; | ||
|
||
public static class ProtobufConfigurationExtensions | ||
{ | ||
/// <summary> | ||
/// Registers the Protobuf serializer with the bus, using the default Protobuf message contract. | ||
/// </summary> | ||
public static void UseProtobufSerializer(this IBusFactoryConfigurator configurator) | ||
{ | ||
var factory = new ProtobufSerializerFactory<object>(); | ||
|
||
configurator.AddSerializer(factory); | ||
} | ||
|
||
/// <summary> | ||
/// Register the Protobuf deserializer for a specific message type on the receive endpoint. | ||
/// </summary> | ||
public static void UseProtobufDeserializer<TProtoMessage>(this IReceiveEndpointConfigurator configurator, bool isDefault = true) | ||
where TProtoMessage : class | ||
{ | ||
var factory = new ProtobufSerializerFactory<TProtoMessage>(); | ||
|
||
configurator.AddDeserializer(factory, isDefault); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Import Project="../../signing.props" /> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks> | ||
<RootNamespace>MassTransit</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition=" '$(IsWindows)' == 'true' "> | ||
<TargetFrameworks>$(TargetFrameworks);net462</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<PackageId>MassTransit.Protobuf</PackageId> | ||
<Title>MassTransit.Protobuf</Title> | ||
<PackageTags>MassTransit;Protobuf</PackageTags> | ||
<Description>MassTransit Protobuf support; $(Description)</Description> | ||
<Version>1.0.0-alpha.38</Version> | ||
<Copyright>Copyright 2023 - Reuben Sonnenberg</Copyright> | ||
<IncludeSymbols>True</IncludeSymbols> | ||
<SymbolPackageFormat>snupkg</SymbolPackageFormat> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="protobuf-net" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\MassTransit.Abstractions\MassTransit.Abstractions.csproj" /> | ||
<ProjectReference Include="..\MassTransit\MassTransit.csproj" /> | ||
</ItemGroup> | ||
</Project> |
2 changes: 2 additions & 0 deletions
2
src/MassTransit.Protobuf/MassTransit.Protobuf.csproj.DotSettings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> | ||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=configuration/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#if !NET6_0_OR_GREATER && !NETSTANDARD2_1 | ||
namespace System.Diagnostics.CodeAnalysis | ||
{ | ||
using System; | ||
|
||
|
||
[AttributeUsage(AttributeTargets.Parameter)] | ||
sealed class NotNullWhenAttribute : | ||
Attribute | ||
{ | ||
/// <summary>Initializes the attribute with the specified return value condition.</summary> | ||
/// <param name="returnValue"> | ||
/// The return value condition. If the method returns this value, the associated parameter will not be null. | ||
/// </param> | ||
public NotNullWhenAttribute(bool returnValue) | ||
{ | ||
ReturnValue = returnValue; | ||
} | ||
|
||
/// <summary>Gets the return value condition.</summary> | ||
public bool ReturnValue { get; } | ||
} | ||
} | ||
#endif |
49 changes: 49 additions & 0 deletions
49
src/MassTransit.Protobuf/Serialization/ProtoMessageEnvelope.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#nullable enable | ||
namespace MassTransit.Serialization | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using MassTransit; | ||
using Metadata; | ||
using ProtoBuf; | ||
|
||
[Serializable] | ||
public class ProtoMessageEnvelope<TProtoMessage> : MessageEnvelope | ||
where TProtoMessage : class | ||
{ | ||
public string? MessageId { get; set; } | ||
public string? RequestId { get; set; } | ||
public string? CorrelationId { get; set; } | ||
public string? ConversationId { get; set; } | ||
public string? InitiatorId { get; set; } | ||
public string? SourceAddress { get; set; } | ||
public string? DestinationAddress { get; set; } | ||
public string? ResponseAddress { get; set; } | ||
public string? FaultAddress { get; set; } | ||
public string[]? MessageType { get; set; } | ||
public object? Message { get; set; } | ||
public DateTime? ExpirationTime { get; set; } | ||
public DateTime? SentTime { get; set; } | ||
public HostInfo? Host { get; set; } | ||
public Dictionary<string, object?>? Headers { get; } | ||
|
||
public ProtoMessageEnvelope(ProtobufMessageEnvelope<TProtoMessage> envelope) | ||
{ | ||
MessageId = envelope.MessageId; | ||
RequestId = envelope.RequestId; | ||
CorrelationId = envelope.CorrelationId; | ||
ConversationId = envelope.ConversationId; | ||
InitiatorId = envelope.InitiatorId; | ||
SourceAddress = envelope.SourceAddress; | ||
DestinationAddress = envelope.DestinationAddress; | ||
ResponseAddress = envelope.ResponseAddress; | ||
FaultAddress = envelope.FaultAddress; | ||
MessageType = envelope.MessageType; | ||
Message = envelope.Message; | ||
ExpirationTime = envelope.ExpirationTime; | ||
SentTime = envelope.SentTime; | ||
Headers = envelope.Headers; | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/MassTransit.Protobuf/Serialization/ProtobufBodyMessageSerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#nullable enable | ||
namespace MassTransit.Serialization | ||
{ | ||
using System.IO; | ||
using System.Net.Mime; | ||
using ProtoBuf.Meta; | ||
|
||
public class ProtobufBodyMessageSerializer : IMessageSerializer | ||
{ | ||
private readonly ContentType _contentType; | ||
private readonly ProtobufMessageEnvelope<object> _envelope; | ||
private readonly RuntimeTypeModel _typeModel; | ||
|
||
public ProtobufBodyMessageSerializer(MessageEnvelope envelope, ContentType contentType, RuntimeTypeModel typeModel) | ||
{ | ||
_envelope = new ProtobufMessageEnvelope<object>(envelope); | ||
_contentType = contentType; | ||
_typeModel = typeModel; | ||
} | ||
|
||
public ContentType ContentType => _contentType; | ||
|
||
public MessageBody GetMessageBody<T>(SendContext<T> context) | ||
where T : class | ||
{ | ||
_envelope.Update(context); | ||
|
||
return new ProtobufMessageBody<T>(context, _typeModel, _envelope as ProtobufMessageEnvelope<T>); | ||
} | ||
|
||
public void Overlay(object message) | ||
{ | ||
using (var stream = new MemoryStream()) | ||
{ | ||
_typeModel.Serialize(stream, message); | ||
stream.Position = 0; | ||
var overlayMessage = _typeModel.Deserialize(_envelope.Message?.GetType(), stream); | ||
_envelope.Message = overlayMessage; | ||
} | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/MassTransit.Protobuf/Serialization/ProtobufHostInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using ProtoBuf; | ||
|
||
namespace MassTransit.Serialization | ||
{ | ||
#nullable enable | ||
[ProtoContract] | ||
[ProtoInclude(100, typeof(HostInfo))] | ||
public class ProtobufHostInfo : HostInfo | ||
{ | ||
[ProtoMember(1)] | ||
public string? MachineName { set; get; } | ||
|
||
[ProtoMember(2)] | ||
public string? ProcessName { set; get; } | ||
|
||
[ProtoMember(3)] | ||
public int ProcessId { set; get; } | ||
|
||
[ProtoMember(4)] | ||
public string? Assembly { set; get; } | ||
|
||
[ProtoMember(5)] | ||
public string? AssemblyVersion { set; get; } | ||
|
||
[ProtoMember(6)] | ||
public string? FrameworkVersion { set; get; } | ||
|
||
[ProtoMember(7)] | ||
public string? MassTransitVersion { set; get; } | ||
|
||
[ProtoMember(8)] | ||
public string? OperatingSystemVersion { set; get; } | ||
} | ||
} |
Oops, something went wrong.