diff --git a/PubSub.sln b/PubSub.sln new file mode 100644 index 0000000..5aa3e3e --- /dev/null +++ b/PubSub.sln @@ -0,0 +1,26 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.12.35527.113 d17.12 +MinimumVisualStudioVersion = 15.0.26730.12 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Subscriber", "Subscriber\Subscriber.csproj", "{2FE71442-7F81-428E-B945-D564850D6564}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Publisher", "Publisher\Publisher.csproj", "{11641841-C7E9-4B49-9688-99E54187A7E8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csproj", "{DD438DB2-9C03-4BC0-BA52-BB7A35098458}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {2FE71442-7F81-428E-B945-D564850D6564}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2FE71442-7F81-428E-B945-D564850D6564}.Debug|Any CPU.Build.0 = Debug|Any CPU + {11641841-C7E9-4B49-9688-99E54187A7E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {11641841-C7E9-4B49-9688-99E54187A7E8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DD438DB2-9C03-4BC0-BA52-BB7A35098458}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DD438DB2-9C03-4BC0-BA52-BB7A35098458}.Debug|Any CPU.Build.0 = Debug|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Publisher/Program.cs b/Publisher/Program.cs new file mode 100644 index 0000000..8715210 --- /dev/null +++ b/Publisher/Program.cs @@ -0,0 +1,64 @@ +using System; +using System.Threading.Tasks; +using NServiceBus; + +class Program +{ + static async Task Main() + { + Console.Title = "Publisher"; + + #region config + + var endpointConfiguration = new EndpointConfiguration("Samples.ASBS.PubSub.Publisher"); + endpointConfiguration.EnableInstallers(); + + var connectionString = Environment.GetEnvironmentVariable("AzureServiceBus_ConnectionString"); + if (string.IsNullOrWhiteSpace(connectionString)) + { + throw new Exception("Could not read the 'AzureServiceBus_ConnectionString' environment variable. Check the sample prerequisites."); + } + + var storageConnectionString = Environment.GetEnvironmentVariable("AzureStorage_ConnectionString"); + if (string.IsNullOrWhiteSpace(storageConnectionString)) + { + throw new Exception("Could not read the 'AzureStorage_ConnectionString' environment variable. Check the sample prerequisites."); + } + + var transport = new AzureServiceBusTransport(connectionString); + endpointConfiguration.UseTransport(transport); + endpointConfiguration.UseSerialization(); + + endpointConfiguration.UseDataBus(); + + var databus = endpointConfiguration.UseDataBus(); + databus.ConnectionString(storageConnectionString); + + #endregion + + var endpointInstance = await Endpoint.Start(endpointConfiguration); + Console.WriteLine("Press 'enter' to publish an event"); + Console.WriteLine("Press any other key to exit"); + + while (true) + { + var key = Console.ReadKey(); + Console.WriteLine(); + + if (key.Key != ConsoleKey.Enter) + { + break; + } + + var message = new SomeEvent + { + Property = "Hello from Publisher", + DataBusProperty = new DataBusProperty("Large message from publisher") + }; + + await endpointInstance.Publish(message); + Console.WriteLine("Event published"); + } + await endpointInstance.Stop(); + } +} diff --git a/Publisher/Publisher.csproj b/Publisher/Publisher.csproj new file mode 100644 index 0000000..9681980 --- /dev/null +++ b/Publisher/Publisher.csproj @@ -0,0 +1,13 @@ + + + net6.0;net48 + Exe + 10.0 + + + + + + + + \ No newline at end of file diff --git a/Shared/Shared.csproj b/Shared/Shared.csproj new file mode 100644 index 0000000..b97945f --- /dev/null +++ b/Shared/Shared.csproj @@ -0,0 +1,11 @@ + + + net6.0;net48 + 10.0 + + + + + + + \ No newline at end of file diff --git a/Shared/SomeEvent.cs b/Shared/SomeEvent.cs new file mode 100644 index 0000000..ea31c05 --- /dev/null +++ b/Shared/SomeEvent.cs @@ -0,0 +1,7 @@ +using NServiceBus; + +public class SomeEvent : IEvent +{ + public string Property { get; set; } + public DataBusProperty DataBusProperty { get; set; } +} \ No newline at end of file diff --git a/Subscriber/Program.cs b/Subscriber/Program.cs new file mode 100644 index 0000000..83baded --- /dev/null +++ b/Subscriber/Program.cs @@ -0,0 +1,48 @@ +using System; +using System.Threading.Tasks; +using NServiceBus; +using NServiceBus.DataBus; + +class Program +{ + static async Task Main() + { + Console.Title = "Subscriber"; + + #region Endpoint Configuration + + var endpointConfiguration = new EndpointConfiguration("Samples.ASBS.PubSub.Subscriber"); + endpointConfiguration.EnableInstallers(); + + var transportConnectionString = Environment.GetEnvironmentVariable("AzureServiceBus_ConnectionString"); + if (string.IsNullOrWhiteSpace(transportConnectionString)) + { + throw new Exception("Could not read the 'AzureServiceBus_ConnectionString' environment variable. Check the sample prerequisites."); + } + + var storageConnectionString = Environment.GetEnvironmentVariable("AzureStorage_ConnectionString"); + if(string.IsNullOrWhiteSpace(storageConnectionString)) + { + throw new Exception("Could not read the 'AzureStorage_ConnectionString' environment variable. Check the sample prerequisites."); + } + + var transport = new AzureServiceBusTransport(transportConnectionString); + endpointConfiguration.UseTransport(transport); + endpointConfiguration.UseSerialization(); + + #endregion + +#pragma warning disable CS0618 // Type or member is obsolete + var databus = endpointConfiguration.UseDataBus(); +#pragma warning restore CS0618 // Type or member is obsolete + databus.AddDeserializer(); + databus.ConnectionString(storageConnectionString); + + var endpointInstance = await Endpoint.Start(endpointConfiguration); + + Console.WriteLine("Press any key to exit"); + Console.ReadKey(); + + await endpointInstance.Stop(); + } +} \ No newline at end of file diff --git a/Subscriber/SomeEventHandler.cs b/Subscriber/SomeEventHandler.cs new file mode 100644 index 0000000..885985d --- /dev/null +++ b/Subscriber/SomeEventHandler.cs @@ -0,0 +1,17 @@ +using System.Threading.Tasks; +using NServiceBus; +using NServiceBus.Logging; + +public class SomeEventHandler : + IHandleMessages +{ + static ILog log = LogManager.GetLogger(); + + public Task Handle(SomeEvent message, IMessageHandlerContext context) + { + log.Info($"Received SomeEvent: {message.Property}"); + log.Info($"DataBus content: {message.DataBusProperty.Value }"); + + return Task.CompletedTask; + } +} \ No newline at end of file diff --git a/Subscriber/Subscriber.csproj b/Subscriber/Subscriber.csproj new file mode 100644 index 0000000..13794f0 --- /dev/null +++ b/Subscriber/Subscriber.csproj @@ -0,0 +1,13 @@ + + + net6.0;net48 + Exe + 10.0 + + + + + + + + \ No newline at end of file