Skip to content

Commit

Permalink
Added Azure.Identity to Service Bus Plugin and support for custom mes…
Browse files Browse the repository at this point in the history
…sage deserialization.
  • Loading branch information
Brian Greco committed Jan 15, 2024
1 parent 9a662ba commit 9a29977
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion netfusion/netfusion-pack.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PACKAGES_DIR=../../../_packages
VERSION=7.0.23-dev
VERSION=7.0.27-dev

rm $PACKAGES_DIR/*.nupkg
dotnet clean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace NetFusion.Common.Base.Serialization;

/// <summary>
/// Interface implemented by message to provide custom deserialization.
/// </summary>
public interface ICustomDeserialize
{
void Deserialize(byte[] value);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Azure.Identity;
using Azure.Messaging.ServiceBus;
using Azure.Messaging.ServiceBus.Administration;
using NetFusion.Integration.ServiceBus.Plugin.Settings;
Expand Down Expand Up @@ -36,8 +37,8 @@ internal NamespaceConnection(ILogger<NamespaceConnection> logger, NamespaceSetti

NamespaceSettings = namespaceSettings ?? throw new ArgumentNullException(nameof(namespaceSettings));

BusClient = new ServiceBusClient(NamespaceSettings.ConnString, BuildOptions());
AdminClient = new ServiceBusAdministrationClient(NamespaceSettings.ConnString);
BusClient = new ServiceBusClient(NamespaceSettings.FullyQualifiedNamespace, new DefaultAzureCredential(), BuildOptions());
AdminClient = new ServiceBusAdministrationClient(NamespaceSettings.FullyQualifiedNamespace, new DefaultAzureCredential());

_externalSettings = new ExternalEntitySettings(namespaceSettings);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.10.4" />
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.16.1" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class NamespaceSettings : IValidatableType
/// The connection string used to connect to the namespace.
/// </summary>
[Required(AllowEmptyStrings = false, ErrorMessage = "Azure Service Bus Connection required.")]
public string ConnString { get; set; } = null!;
public string FullyQualifiedNamespace { get; set; } = null!;

/// <summary>
/// The type of protocol and transport that will be used for communicating with the Service Bus service.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ private IMessage DeserializeReceivedMessage(ProcessMessageEventArgs args)
{
message.SetReplyTo(args.Message.ReplyTo);
}

foreach (var appProp in args.Message.ApplicationProperties)
{
var appPropValue = appProp.Value.ToString();
if (appPropValue is not null)
{
message.Attributes.SetStringValue(appProp.Key, appPropValue);
}
}

return message;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using NetFusion.Common.Base.Serialization;

Expand Down Expand Up @@ -69,6 +70,8 @@ public byte[] Serialize(object value, string contentType, string? encodingType =
if (valueType == null) throw new ArgumentNullException(nameof(valueType));
if (value == null) throw new ArgumentNullException(nameof(value));

if (TryCustomDeserialization(valueType, value, out object? instance)) return instance;

ISerializer serializer = GetSerializer(contentType, encodingType);
return serializer.Deserialize(value, valueType);
}
Expand All @@ -79,11 +82,28 @@ public byte[] Serialize(object value, string contentType, string? encodingType =
throw new ArgumentException("Content-Type must be specified.", nameof(contentType));

if (value == null) throw new ArgumentNullException(nameof(value));

if (TryCustomDeserialization(typeof(T), value, out object? instance)) return (T)instance;

ISerializer serializer = GetSerializer(contentType, encodingType);
return (T?)serializer.Deserialize(value, typeof(T));
}

private bool TryCustomDeserialization(Type valueType, byte[] value, [NotNullWhen(true)]out object? instance)
{
if (valueType.IsAssignableTo(typeof(ICustomDeserialize)))
{
instance = Activator.CreateInstance(valueType) ??
throw new InvalidOperationException("");

((ICustomDeserialize)instance).Deserialize(value);
return true;
}

instance = null;
return false;
}

public ISerializer GetSerializer(string contentType, string? encodingType = null)
{
var typeProps = GetContentTypeAndEncoding(contentType, encodingType);
Expand Down

0 comments on commit 9a29977

Please sign in to comment.