Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for MassTransit #92

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
828 changes: 447 additions & 381 deletions Nybus.sln

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions samples/MassTransit/RabbitMQ/PureSender/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Threading.Tasks;
using MassTransit;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Nybus;
using Nybus.Configuration;
using Nybus.MassTransit;
using Types;

namespace PureSender
{
class Program
{
static async Task Main(string[] args)
{
var services = new ServiceCollection();

services.AddLogging(logging => logging.AddConsole().SetMinimumLevel(LogLevel.Trace));

services.AddNybus(nybus =>
{
nybus.UseMassTransitWithRabbitMq(c =>
{
c.ConfigureMassTransit(mt =>
{
mt.Host(new Uri("rabbitmq://localhost"), h =>
{
h.Username("guest");
h.Password("guest");
});
});
});

nybus.SubscribeToEvent<SomethingDoneEvent>(async (dispatcher, context) =>
{
await Console.Out.WriteLineAsync($"Something was done: {context.Event.WhatWasDone}");
});
});

var serviceProvider = services.BuildServiceProvider();

var host = serviceProvider.GetRequiredService<IBusHost>();

await host.StartAsync();

await host.Bus.InvokeCommandAsync(new DoSomethingCommand
{
WhatToDo = "Whatever you want"
});

Console.WriteLine("Press ENTER to exit...");
Console.ReadLine();

await host.StopAsync();
}
}
}
19 changes: 19 additions & 0 deletions samples/MassTransit/RabbitMQ/PureSender/PureSender.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\Types\Types.csproj" />
<ProjectReference Include="..\..\..\..\src\Nybus\Nybus.csproj" />
<ProjectReference Include="..\..\..\..\src\engines\Nybus.Engine.MassTransit.RabbitMq\Nybus.Engine.MassTransit.RabbitMq.csproj" />
</ItemGroup>

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
</ItemGroup>

</Project>
56 changes: 56 additions & 0 deletions samples/MassTransit/RabbitMQ/Receiver/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Threading.Tasks;
using MassTransit;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Nybus;
using Types;

namespace Receiver
{
class Program
{
static async Task Main(string[] args)
{
var services = new ServiceCollection();

services.AddLogging(logging => logging.AddConsole().SetMinimumLevel(LogLevel.Trace));

services.AddNybus(nybus =>
{
nybus.UseMassTransitWithRabbitMq(c =>
{
c.ConfigureMassTransit(mt =>
{
mt.Host(new Uri("rabbitmq://localhost"), h =>
{
h.Username("guest");
h.Password("guest");
});
});
});

nybus.SubscribeToCommand<DoSomethingCommand>(async (dispatcher, context) =>
{
await Console.Out.WriteLineAsync($"Doing something: {context.Command.WhatToDo}");

await dispatcher.RaiseEventAsync<SomethingDoneEvent>(new SomethingDoneEvent
{
WhatWasDone = context.Command.WhatToDo
});
});
});

var serviceProvider = services.BuildServiceProvider();

var host = serviceProvider.GetRequiredService<IBusHost>();

await host.StartAsync();

Console.WriteLine("Press ENTER to exit...");
Console.ReadLine();

await host.StopAsync();
}
}
}
19 changes: 19 additions & 0 deletions samples/MassTransit/RabbitMQ/Receiver/Receiver.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\Types\Types.csproj" />
<ProjectReference Include="..\..\..\..\src\Nybus\Nybus.csproj" />
<ProjectReference Include="..\..\..\..\src\engines\Nybus.Engine.MassTransit.RabbitMq\Nybus.Engine.MassTransit.RabbitMq.csproj" />
</ItemGroup>

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
</ItemGroup>

</Project>
15 changes: 15 additions & 0 deletions samples/MassTransit/RabbitMQ/Types/Types.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using Nybus;

namespace Types
{
public class DoSomethingCommand : ICommand
{
public string WhatToDo { get; set; }
}

public class SomethingDoneEvent : IEvent
{
public string WhatWasDone { get; set; }
}
}
11 changes: 11 additions & 0 deletions samples/MassTransit/RabbitMQ/Types/Types.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\..\..\..\src\Nybus.Abstractions\Nybus.Abstractions.csproj" />
</ItemGroup>

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections;
using System.Collections.Generic;
using MassTransit.RabbitMqTransport;
using Microsoft.Extensions.DependencyInjection;
using Nybus.MassTransit.RabbitMq;

namespace Nybus.Configuration
{
public interface IMassTransitRabbitMqConfigurator
{
void ConfigureMassTransit(Action<IRabbitMqBusFactoryConfigurator> configureMassTransit);

//void UseConfiguration(string sectionName = "MassTransit");


}

public class MassTransitRabbitMqConfigurator : IMassTransitRabbitMqConfigurator
{
private readonly IList<Action<IRabbitMqBusFactoryConfigurator>> _configurationActions = new List<Action<IRabbitMqBusFactoryConfigurator>>();

public void ConfigureMassTransit(Action<IRabbitMqBusFactoryConfigurator> configureMassTransit)
{
if (configureMassTransit == null)
{
throw new ArgumentNullException(nameof(configureMassTransit));
}

_configurationActions.Add(configureMassTransit);
}

private string _configurationSectionName;

public void UseConfiguration(string sectionName = "MassTransit")
{
_configurationSectionName = sectionName ?? throw new ArgumentNullException(nameof(sectionName));
}

public void Apply(INybusConfigurator nybus)
{
var busBuilder = new MassTransitRabbitMqBusBuilder();

foreach (var action in _configurationActions)
{
busBuilder.AddConfiguration(action);
}

nybus.AddServiceConfiguration(svc => svc.AddSingleton<IMassTransitRabbitMqBusBuilder>(busBuilder));
}
}
}
Loading