Globals.NET.RabbitMQ
1.0.7
dotnet add package Globals.NET.RabbitMQ --version 1.0.7
NuGet\Install-Package Globals.NET.RabbitMQ -Version 1.0.7
<PackageReference Include="Globals.NET.RabbitMQ" Version="1.0.7" />
paket add Globals.NET.RabbitMQ --version 1.0.7
#r "nuget: Globals.NET.RabbitMQ, 1.0.7"
// Install Globals.NET.RabbitMQ as a Cake Addin #addin nuget:?package=Globals.NET.RabbitMQ&version=1.0.7 // Install Globals.NET.RabbitMQ as a Cake Tool #tool nuget:?package=Globals.NET.RabbitMQ&version=1.0.7
The Globals Design Pattern
The Globals Communication Pattern describes a way to make communication between systems death simple, hiding away all technical details.
This package contains a .NET & RabbitMQ implementation of this pattern.
For the examples, click here
Working (C#): On the sending side, you declare a Global of a certain type and name.
using (Global<string> HelloWorld = new Global<string>("HelloWorld")) // a declaration...
{
HelloWorld.Value = "Hello, World!"; // assign a value, and we are done!
...
}
On the receiving side, you declare the same Global (same name, same type) and add a DataChanged handler.
// a declaration plus event handler
using (Global<string> HelloWorld = new Global<string>("HelloWorld", handler: HelloWorld_DataChanged))
{
...
}
private static void HelloWorld_DataChanged(object sender, GlobalEventData<string> e)
{
Console.WriteLine(e.Data); // Output: Hello, world!
}
On the moment a value is assigned by the Sending side, a DataChanged event is raised on the receiving side.
With this mechanism, you can send a stream of data as well:
using (Global<string> HelloWorld = new Global<string>("HelloWorld")) // a declaration...
{
for (int i = 0; i < 100; i++)
{
HelloWorld.Value = $"Hello, World! This is greeting number {i}!";
}
...
}
On the receiving side, 100 DataChanged events are raised, in the same order as it was sent.
Below the complete example to send and receive a single greeting over the network:
The Sender:
using System;
using Globals.NET.RabbitMQ; // the package
namespace Sender
{
class Program
{
static void Main()
{
using (Global<string> HelloWorld = new Global<string>("HelloWorld")) // a declaration...
{
HelloWorld.Value = "Hello, World!"; // assign a value, and we are done!
Console.ReadLine();
Console.WriteLine("Stopping...");
};
}
}
}
The Receiver:
using System;
using Globals.NET.RabbitMQ; // the package
namespace Receiver
{
class Program
{
static void Main()
{
using Global<string> HelloWorld = new Global<string>("HelloWorld", handler: HelloWorld_DataChanged); // a declaration plus event handler
Console.ReadLine();
Console.WriteLine("Stopping...");
}
private static void HelloWorld_DataChanged(object sender, GlobalEventData<string> e)
{
Console.WriteLine(e.Data); // Output: Hello, world!
}
}
}
The Rule:
Globals with the same name have the same value.
Getting Started
Prerequisites
- This package uses RabbitMQ as the communication layer, this should be installed first.
- This package is created to be used within the .NET Framework (.NET Standard 2.0), a version of Visual Studio can be downloaded here.
Installing
Once the Prerequisites section is done, create you first Hello World application following the steps below. In this description it is assumed you work with some version of Visual Studio. We will start with:
The Sender:
1. Start Visual Studio and create an empty Console Application:
- Create a new project
- Select as project type: Console App, C#, either .NET Core or .NET Framework.
- Click Next
- Give your project a nice name, like "HelloWorld_Sender"
- Click on 'Create'
Visual Studio opens the editor with a small program:
using System;
namespace HelloWorld_Sender
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
2. Install the Globals.NET.RabbitMQ NuGet package:
- Select Tools - NuGet Package Manager - Manage NuGet Packages for Solution...
- Click on the Browse tab
- In the Search bar, type Globals.NET.RabbitMQ.
- Select the Globals.NET.RabbitMQ package in the window below the search bar.
- Check the HellowWorld_Sender checkbox and click on install.
After installation is complete:
- Close the window
3. Copy & Paste the Sender Code in program.cs:
using System;
using Globals.NET.RabbitMQ; // the package
namespace Sender
{
class Program
{
static void Main()
{
using (var HelloWorld = new Global<string>("HelloWorld")) // a declaration...
{
HelloWorld.Value = "Hello, World!"; // assign a value, and we are done!
Console.ReadLine();
Console.WriteLine("Stopping...");
};
}
}
}
4. Add an App.Config file to your project and enter 5 configuration parameters:
- Right-click on the project HelloWorld_Sender, and click Add - New Item.
- In the search bar, type 'config'.
- Select the ' Application Configuration File' and click 'Add' .
Your App.Config file is added and opened.
- Add your 5 RabbitMQ parameters:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="HostName" value="localhost"/>
<add key="Port" value="5672"/>
<add key="UserName" value="guest"/>
<add key="Password" value="guest"/>
<add key="VirtualHost" value="/"/>
</appSettings>
</configuration>
To get your parameters right, please see the RabbitMQ documentation
The Receiver:
Follow the same steps as with creating the sender, but now give the project the name HelloWorld_Receiver, and replace your program.cs code with:
using System;
using Globals.NET.RabbitMQ; // the package
namespace Receiver
{
class Program
{
static void Main()
{
using var HelloWorld = new Global<string>("HelloWorld", handler: HelloWorld_DataChanged); // a declaration plus event handler
Console.ReadLine();
Console.WriteLine("Stopping...");
}
private static void HelloWorld_DataChanged(object sender, GlobalEventData<string> e)
{
Console.WriteLine(e.Data); // The data is received here!
}
}
}
Built With / Tools Used
- Microsoft Visual Studio 2019 - The Development Environment
- RabbitMQ - The Message Broker
- Newtonsoft - Essential package by the King
- Word 365 - Used to write the article
- Notepad++ - Used to write this Readme
- Heavy Duty Calculator - If you want to get Numeric
Resources
Author
License
This project is licensed under the MIT License
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- RabbitMQ.Client (>= 6.8.0)
- System.Configuration.ConfigurationManager (>= 8.0.0)
- System.Text.Json (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
An implementation of the Globals Design Pattern