Skip to content

Commit

Permalink
[Host.Configuration] Backward compatibility with 2.x
Browse files Browse the repository at this point in the history
Signed-off-by: Tomasz Maruszak <maruszaktomasz@gmail.com>
  • Loading branch information
zarusz committed Sep 18, 2024
1 parent 0e0159c commit dc65eda
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Host.Plugin.Properties.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Import Project="Common.NuGet.Properties.xml" />

<PropertyGroup>
<Version>3.0.0-rc6</Version>
<Version>3.0.0-rc11</Version>
</PropertyGroup>

</Project>
14 changes: 12 additions & 2 deletions src/SlimMessageBus.Host.Configuration/Builders/ConsumerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@ public ConsumerBuilder(MessageBusSettings settings, Type messageType = null)
ConsumerSettings.ConsumerMode = ConsumerMode.Consumer;
}

public ConsumerBuilder<TMessage> Path(string path, Action<ConsumerBuilder<TMessage>> pathConfig = null)
public ConsumerBuilder<TMessage> Path(string path)
{
ConsumerSettings.Path = path;
return this;
}

public ConsumerBuilder<TMessage> Path(string path, Action<ConsumerBuilder<TMessage>> pathConfig)
{
Path(path);
pathConfig?.Invoke(this);
return this;
}

public ConsumerBuilder<TMessage> Topic(string topic, Action<ConsumerBuilder<TMessage>> topicConfig = null) => Path(topic, topicConfig);
public ConsumerBuilder<TMessage> Topic(string topic)
=> Path(topic);

public ConsumerBuilder<TMessage> Topic(string topic, Action<ConsumerBuilder<TMessage>> topicConfig)
=> Path(topic, topicConfig);

private static Task DefaultConsumerOnMethod<T>(object consumer, object message, IConsumerContext consumerContext, CancellationToken cancellationToken)
=> ((IConsumer<T>)consumer).OnHandle((T)message, cancellationToken);
Expand Down
40 changes: 30 additions & 10 deletions src/SlimMessageBus.Host.Configuration/Builders/HandlerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,53 @@ protected AbstractHandlerBuilder(MessageBusSettings settings, Type messageType,
}

protected THandlerBuilder TypedThis => (THandlerBuilder)this;

/// <summary>
/// Configure topic name (or queue name) that incoming requests (<see cref="TRequest"/>) are expected on.
/// </summary>
/// <param name="path">Topic name</param>
/// <returns></returns>
public THandlerBuilder Path(string path)
{
var consumerSettingsExist = Settings.Consumers.Any(x => x.Path == path && x.ConsumerMode == ConsumerMode.RequestResponse && x != ConsumerSettings);
if (consumerSettingsExist)
{
throw new ConfigurationMessageBusException($"Attempted to configure request handler for path '{path}' when one was already configured. There can only be one request handler for a given path.");
}

ConsumerSettings.Path = path;
return TypedThis;
}

/// <summary>
/// Configure topic name (or queue name) that incoming requests (<see cref="TRequest"/>) are expected on.
/// </summary>
/// <param name="path">Topic name</param>
/// <param name="pathConfig"></param>
/// <returns></returns>
public THandlerBuilder Path(string path, Action<THandlerBuilder> pathConfig = null)
{
var consumerSettingsExist = Settings.Consumers.Any(x => x.Path == path && x.ConsumerMode == ConsumerMode.RequestResponse && x != ConsumerSettings);
if (consumerSettingsExist)
{
throw new ConfigurationMessageBusException($"Attempted to configure request handler for path '{path}' when one was already configured. There can only be one request handler for a given path (topic/queue)");
}

ConsumerSettings.Path = path;
public THandlerBuilder Path(string path, Action<THandlerBuilder> pathConfig)
{
Path(path);
pathConfig?.Invoke(TypedThis);
return TypedThis;
}

/// <summary>
/// Configure topic name (or queue name) that incoming requests (<see cref="TRequest"/>) are expected on.
/// </summary>
/// <param name="topic">Topic name</param>
/// <returns></returns>
public THandlerBuilder Topic(string topic)
=> Path(topic);

/// <summary>
/// Configure topic name (or queue name) that incoming requests (<see cref="TRequest"/>) are expected on.
/// </summary>
/// <param name="topic">Topic name</param>
/// <param name="topicConfig"></param>
/// <returns></returns>
public THandlerBuilder Topic(string topic, Action<THandlerBuilder> topicConfig = null) => Path(topic, topicConfig);
public THandlerBuilder Topic(string topic, Action<THandlerBuilder> topicConfig)
=> Path(topic, topicConfig);

public THandlerBuilder Instances(int numberOfInstances)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Description>Core configuration interfaces of SlimMessageBus</Description>
<PackageTags>SlimMessageBus</PackageTags>
<RootNamespace>SlimMessageBus.Host</RootNamespace>
<Version>3.0.0-rc6</Version>
<Version>3.0.0-rc11</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Import Project="../Common.NuGet.Properties.xml" />

<PropertyGroup>
<Version>3.0.0-rc6</Version>
<Version>3.0.0-rc11</Version>
<Description>Core interceptor interfaces of SlimMessageBus</Description>
<PackageTags>SlimMessageBus</PackageTags>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Import Project="../Common.NuGet.Properties.xml" />

<PropertyGroup>
<Version>3.0.0-rc6</Version>
<Version>3.0.0-rc11</Version>
<Description>Core serialization interfaces of SlimMessageBus</Description>
<PackageTags>SlimMessageBus</PackageTags>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/SlimMessageBus/SlimMessageBus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Import Project="../Common.NuGet.Properties.xml" />

<PropertyGroup>
<Version>3.0.0-rc6</Version>
<Version>3.0.0-rc11</Version>
<Description>
This library provides a lightweight, easy-to-use message bus interface for .NET, offering a simplified facade for working with messaging brokers.
It supports multiple transport providers for popular messaging systems, as well as in-memory (in-process) messaging for efficient local communication.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void When_PathSet_Given_ThePathWasUsedBeforeOnAnotherHandler_Then_Excepti
// assert
act.Should()
.Throw<ConfigurationMessageBusException>()
.WithMessage($"Attempted to configure request handler for path '*' when one was already configured. There can only be one request handler for a given path (topic/queue)");
.WithMessage($"Attempted to configure request handler for path '*' when one was already configured. There can only be one request handler for a given path.");
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public record GenerateCustomerIdCommand(string Firstname, string Lastname) : IRe

public class GenerateCustomerIdCommandHandler : IRequestHandler<GenerateCustomerIdCommand, string>
{
public async Task<string> OnHandle(GenerateCustomerIdCommand request, CancellationToken cancellationToken)
public Task<string> OnHandle(GenerateCustomerIdCommand request, CancellationToken cancellationToken)
{
// Note: This handler will be already wrapped in a transaction: see Program.cs and .UseTransactionScope() / .UseSqlTransaction()

Expand Down

0 comments on commit dc65eda

Please sign in to comment.