diff --git a/Microsoft.Azure.ServiceBus.Plugins.sln b/Microsoft.Azure.ServiceBus.Plugins.sln index 2855cc2..a9d2b65 100644 --- a/Microsoft.Azure.ServiceBus.Plugins.sln +++ b/Microsoft.Azure.ServiceBus.Plugins.sln @@ -1,15 +1,28 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26430.6 +VisualStudioVersion = 15.0.26430.12 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{7D8B793C-F4AC-432A-8EE7-2EE3C2B16EC1}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Azure.ServiceBus.KeyVault", "src\Microsoft.Azure.ServiceBus.KeyVault\Microsoft.Azure.ServiceBus.KeyVault.csproj", "{F942A66E-A1B7-41C3-85E8-1A108D18F890}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.ServiceBus.KeyVault", "src\Microsoft.Azure.ServiceBus.KeyVault\Microsoft.Azure.ServiceBus.KeyVault.csproj", "{F942A66E-A1B7-41C3-85E8-1A108D18F890}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{B18143EB-5FF4-4AFC-B086-709DC9B92546}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Azure.ServiceBus.KeyVault.Test", "test\Microsoft.Azure.ServiceBus.KeyVault.Test\Microsoft.Azure.ServiceBus.KeyVault.Test.csproj", "{DC8DC685-DCB5-47C7-A2B7-41D31EDDCAED}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.ServiceBus.KeyVault.Test", "test\Microsoft.Azure.ServiceBus.KeyVault.Test\Microsoft.Azure.ServiceBus.KeyVault.Test.csproj", "{DC8DC685-DCB5-47C7-A2B7-41D31EDDCAED}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Azure.ServiceBus.MessageId", "src\Microsoft.Azure.ServiceBus.MessageId\Microsoft.Azure.ServiceBus.MessageId.csproj", "{7EAE339B-16CC-418C-BB17-070E10BE8283}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Azure.ServiceBus.MessageId.Test", "test\Microsoft.Azure.ServiceBus.MessageId.Test\Microsoft.Azure.ServiceBus.MessageId.Test.csproj", "{F2FF1744-AE2B-440D-93AB-92C5B832FF25}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Azure.ServiceBus.Test.Shared", "test\Microsoft.Azure.ServiceBus.Test.Shared\Microsoft.Azure.ServiceBus.Test.Shared.csproj", "{63F75042-620C-4B08-BE41-D7EB64479A80}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Documentation", "Documentation", "{F7BC1BBC-D5D5-4E0A-9332-C9C841450F55}" + ProjectSection(SolutionItems) = preProject + docs\keyvault.md = docs\keyvault.md + docs\messageid.md = docs\messageid.md + README.md = README.md + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -25,6 +38,18 @@ Global {DC8DC685-DCB5-47C7-A2B7-41D31EDDCAED}.Debug|Any CPU.Build.0 = Debug|Any CPU {DC8DC685-DCB5-47C7-A2B7-41D31EDDCAED}.Release|Any CPU.ActiveCfg = Release|Any CPU {DC8DC685-DCB5-47C7-A2B7-41D31EDDCAED}.Release|Any CPU.Build.0 = Release|Any CPU + {7EAE339B-16CC-418C-BB17-070E10BE8283}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7EAE339B-16CC-418C-BB17-070E10BE8283}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7EAE339B-16CC-418C-BB17-070E10BE8283}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7EAE339B-16CC-418C-BB17-070E10BE8283}.Release|Any CPU.Build.0 = Release|Any CPU + {F2FF1744-AE2B-440D-93AB-92C5B832FF25}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F2FF1744-AE2B-440D-93AB-92C5B832FF25}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F2FF1744-AE2B-440D-93AB-92C5B832FF25}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F2FF1744-AE2B-440D-93AB-92C5B832FF25}.Release|Any CPU.Build.0 = Release|Any CPU + {63F75042-620C-4B08-BE41-D7EB64479A80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {63F75042-620C-4B08-BE41-D7EB64479A80}.Debug|Any CPU.Build.0 = Debug|Any CPU + {63F75042-620C-4B08-BE41-D7EB64479A80}.Release|Any CPU.ActiveCfg = Release|Any CPU + {63F75042-620C-4B08-BE41-D7EB64479A80}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -32,5 +57,8 @@ Global GlobalSection(NestedProjects) = preSolution {F942A66E-A1B7-41C3-85E8-1A108D18F890} = {7D8B793C-F4AC-432A-8EE7-2EE3C2B16EC1} {DC8DC685-DCB5-47C7-A2B7-41D31EDDCAED} = {B18143EB-5FF4-4AFC-B086-709DC9B92546} + {7EAE339B-16CC-418C-BB17-070E10BE8283} = {7D8B793C-F4AC-432A-8EE7-2EE3C2B16EC1} + {F2FF1744-AE2B-440D-93AB-92C5B832FF25} = {B18143EB-5FF4-4AFC-B086-709DC9B92546} + {63F75042-620C-4B08-BE41-D7EB64479A80} = {B18143EB-5FF4-4AFC-B086-709DC9B92546} EndGlobalSection EndGlobal diff --git a/README.md b/README.md index b9e1bfe..f91de0b 100644 --- a/README.md +++ b/README.md @@ -18,4 +18,5 @@ See our [Contribution Guidelines](./.github/CONTRIBUTING.md). The folling plugins are included with this repository: -* [Key Vault](./docs/keyvault.md) \ No newline at end of file +* [Key Vault](./docs/keyvault.md) +* [Message ID](./docs/messageid.md) \ No newline at end of file diff --git a/build/build.ps1 b/build/build.ps1 index 992a0d2..1d2e752 100644 --- a/build/build.ps1 +++ b/build/build.ps1 @@ -45,18 +45,22 @@ function Build-Solution function Run-UnitTests { + Write-Host "Running unit tests." + if ($skipCodeCoverage) { - dotnet test $testProject -f $runtime - if (-not $?) + foreach ($test in ls ($projectFolder + '\test\**\*.Test.csproj')) { - throw "Unit tests failed." + Write-Host "Testing $test" + dotnet test $test -f $runtime + if (-not $?) + { + throw "Unit tests failed." + } } return; } - Write-Host "Running unit tests." - if (-Not (Test-Path .\nuget.exe)) { Invoke-WebRequest -Uri "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" -outfile $buildFolder\nuget.exe diff --git a/docs/messageid.md b/docs/messageid.md new file mode 100644 index 0000000..0675a3c --- /dev/null +++ b/docs/messageid.md @@ -0,0 +1,27 @@ +# Message ID plugin for Azure Service Bus + +The Message ID plugin for Azure Service Bus allows for the message ID on outgoing messages to be set using custom logic. + +## How to use + +In order to use this plugin you will need to setup the following: + +1. An Azure subscription +1. A Service Bus namespace + +## Example + +Below is a simple example of how to use the plugin. + +```csharp +var messageIdPlugin = new MessageIdPlugin(() => Guid.NewGuid().ToString("N")); + +var queueClient = new QueueClient("{ServiceBusConnectionString}", "{ServiceBusEntityName}"); +queueClient.RegisterPlugin(messageIdPlugin); + +var message = new Message(Encoding.UTF8.GetBytes("Message with GUID message ID")); + +await queueClient.SendAsync(message); + +// message.MessageId will be assigned a GUID in a 32 digit format w/o hyphens or braces +``` \ No newline at end of file diff --git a/src/Microsoft.Azure.ServiceBus.MessageId/MessageIdPlugin.cs b/src/Microsoft.Azure.ServiceBus.MessageId/MessageIdPlugin.cs new file mode 100644 index 0000000..42ca73f --- /dev/null +++ b/src/Microsoft.Azure.ServiceBus.MessageId/MessageIdPlugin.cs @@ -0,0 +1,52 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.Azure.ServiceBus.MessageId +{ + using System; + using System.Threading.Tasks; + using Microsoft.Azure.ServiceBus.Core; + + /// Generate Azure Service Bus for outgoing messages. + /// + /// var plugin = new MessageIdPlugin(() => Guid.NewGuid().ToString("N")); + /// queueClient.RegisterPlugin(plugin); + /// + /// + /// If a message ID is assigned, the value will not be replaced by the plugin. + public class MessageIdPlugin : ServiceBusPlugin + { + private Func messageIdGenerator; + + /// + /// + /// + public override string Name => "Microsoft.Azure.ServiceBus.MessageId"; + + /// + /// Create a new instance of + /// + /// Message ID generator to use. + public MessageIdPlugin(Func messageIdGenerator) + { + this.messageIdGenerator = messageIdGenerator; + } + + /// + /// Assign message id if it's not already assigned. + /// The to assign id to. + /// + /// with id. + public override Task BeforeMessageSend(Message message) + { + if (!string.IsNullOrEmpty(message.MessageId)) + { + return base.BeforeMessageSend(message); + } + + message.MessageId = messageIdGenerator(); + + return Task.FromResult(message); + } + } +} diff --git a/src/Microsoft.Azure.ServiceBus.MessageId/Microsoft.Azure.ServiceBus.MessageId.csproj b/src/Microsoft.Azure.ServiceBus.MessageId/Microsoft.Azure.ServiceBus.MessageId.csproj new file mode 100644 index 0000000..cede70b --- /dev/null +++ b/src/Microsoft.Azure.ServiceBus.MessageId/Microsoft.Azure.ServiceBus.MessageId.csproj @@ -0,0 +1,35 @@ + + + + Microsoft.Azure.ServiceBus MessageId extension + Microsoft.Azure.ServiceBus.MessageId + 0.0.1-preview + Microsoft; Sean Feldman + net451;netstandard1.3 + true + Microsoft.Azure.ServiceBus.MessageId + ../../build/keyfile.snk + true + true + Microsoft.Azure.ServiceBus.MessageId + Azure;Service Bus;ServiceBus;.NET;AMQP;IoT;Queue;Topic;KeyVault;Encryption;Plugin + https://github.com/Azure/azure-service-bus-dotnet-plugins/releases + https://raw.githubusercontent.com/Azure/azure-service-bus-dotnet-plugins/master/service-bus.png + https://github.com/Azure/azure-service-bus-dotnet-plugins + https://raw.githubusercontent.com/Azure/azure-service-bus-dotnet-plugins/master/LICENSE + true + $(PackageTargetFallback);dnxcore50 + 1.6.1 + 1.6.1 + false + false + false + bin\$(Configuration)\$(TargetFramework)\Microsoft.Azure.ServiceBus.MessageId.xml + full + + + + + + + \ No newline at end of file diff --git a/test/Microsoft.Azure.ServiceBus.KeyVault.Test/EncryptionTests.cs b/test/Microsoft.Azure.ServiceBus.KeyVault.Test/EncryptionTests.cs index 696a5e9..ae48cfb 100644 --- a/test/Microsoft.Azure.ServiceBus.KeyVault.Test/EncryptionTests.cs +++ b/test/Microsoft.Azure.ServiceBus.KeyVault.Test/EncryptionTests.cs @@ -7,6 +7,7 @@ namespace Microsoft.Azure.ServiceBus.KeyVault.Test using System.Text; using System.Threading.Tasks; using Microsoft.Azure.ServiceBus.KeyVault; + using Microsoft.Azure.ServiceBus.Test.Shared; using Xunit; public class EncryptionTests diff --git a/test/Microsoft.Azure.ServiceBus.KeyVault.Test/Microsoft.Azure.ServiceBus.KeyVault.Test.csproj b/test/Microsoft.Azure.ServiceBus.KeyVault.Test/Microsoft.Azure.ServiceBus.KeyVault.Test.csproj index 815745b..a56c883 100644 --- a/test/Microsoft.Azure.ServiceBus.KeyVault.Test/Microsoft.Azure.ServiceBus.KeyVault.Test.csproj +++ b/test/Microsoft.Azure.ServiceBus.KeyVault.Test/Microsoft.Azure.ServiceBus.KeyVault.Test.csproj @@ -21,6 +21,7 @@ + diff --git a/test/Microsoft.Azure.ServiceBus.MessageId.Test/Microsoft.Azure.ServiceBus.MessageId.Test.csproj b/test/Microsoft.Azure.ServiceBus.MessageId.Test/Microsoft.Azure.ServiceBus.MessageId.Test.csproj new file mode 100644 index 0000000..9119bf5 --- /dev/null +++ b/test/Microsoft.Azure.ServiceBus.MessageId.Test/Microsoft.Azure.ServiceBus.MessageId.Test.csproj @@ -0,0 +1,39 @@ + + + + Microsoft.Azure.ServiceBus.MessageId.Test + netcoreapp1.0;net46 + true + Microsoft.Azure.ServiceBus.MessageId.Test + ../../build/keyfile.snk + true + true + Microsoft.Azure.ServiceBus.MessageId.Test + true + $(PackageTargetFallback);dnxcore50;portable-net46+win8 + 1.0.4 + false + false + false + + False + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test/Microsoft.Azure.ServiceBus.MessageId.Test/When_MessageIdPlugin_is_used.cs b/test/Microsoft.Azure.ServiceBus.MessageId.Test/When_MessageIdPlugin_is_used.cs new file mode 100644 index 0000000..8c7a305 --- /dev/null +++ b/test/Microsoft.Azure.ServiceBus.MessageId.Test/When_MessageIdPlugin_is_used.cs @@ -0,0 +1,56 @@ +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.Azure.ServiceBus.MessageId.Test +{ + using System; + using System.Threading.Tasks; + using Microsoft.Azure.ServiceBus; + using Xunit; + using Microsoft.Azure.ServiceBus.Test.Shared; + + public class When_MessageIdPlugin_is_used + { + [Fact] + [DisplayTestMethodName] + public async Task Should_assign_message_id() + { + var message = new Message(); + var generatedMessageId = Guid.Empty; + var plugin = new MessageIdPlugin(() => + { + generatedMessageId = Guid.NewGuid(); + return generatedMessageId.ToString("N"); + }); + + var result = await plugin.BeforeMessageSend(message); + + Assert.Equal(generatedMessageId, Guid.Parse(result.MessageId)); + } + + [Fact] + [DisplayTestMethodName] + public async Task Should_not_assign_message_id_if_it_already_exists() + { + var originalMessageId = Guid.NewGuid().ToString(); + var message = new Message + { + MessageId = originalMessageId + }; + var plugin = new MessageIdPlugin(() => "this id should never be assigned"); + + var result = await plugin.BeforeMessageSend(message); + + Assert.Equal(originalMessageId, result.MessageId); + } + + [Fact] + [DisplayTestMethodName] + public void Should_return_correct_plugin_name() + { + var plugin = new MessageIdPlugin(() => ""); + + Assert.Equal("Microsoft.Azure.ServiceBus.MessageId", plugin.Name); + } + + } +} diff --git a/test/Microsoft.Azure.ServiceBus.KeyVault.Test/DisplayTestMethodNameAttribute.cs b/test/Microsoft.Azure.ServiceBus.Test.Shared/DisplayTestMethodNameAttribute.cs similarity index 94% rename from test/Microsoft.Azure.ServiceBus.KeyVault.Test/DisplayTestMethodNameAttribute.cs rename to test/Microsoft.Azure.ServiceBus.Test.Shared/DisplayTestMethodNameAttribute.cs index ba1baa1..2f6ce64 100644 --- a/test/Microsoft.Azure.ServiceBus.KeyVault.Test/DisplayTestMethodNameAttribute.cs +++ b/test/Microsoft.Azure.ServiceBus.Test.Shared/DisplayTestMethodNameAttribute.cs @@ -1,7 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -namespace Microsoft.Azure.ServiceBus.KeyVault.Test +namespace Microsoft.Azure.ServiceBus.Test.Shared { using System.Reflection; using Microsoft.Extensions.PlatformAbstractions; diff --git a/test/Microsoft.Azure.ServiceBus.Test.Shared/Microsoft.Azure.ServiceBus.Test.Shared.csproj b/test/Microsoft.Azure.ServiceBus.Test.Shared/Microsoft.Azure.ServiceBus.Test.Shared.csproj new file mode 100644 index 0000000..d1bc5e9 --- /dev/null +++ b/test/Microsoft.Azure.ServiceBus.Test.Shared/Microsoft.Azure.ServiceBus.Test.Shared.csproj @@ -0,0 +1,31 @@ + + + + Microsoft.Azure.ServiceBus.KeyVault.Test + netcoreapp1.0;net46 + true + Microsoft.Azure.ServiceBus.Test.Shared + ../../build/keyfile.snk + true + true + Microsoft.Azure.ServiceBus.Test.Shared + true + $(PackageTargetFallback);dnxcore50;portable-net46+win8 + 1.0.4 + false + false + false + + False + Microsoft.Azure.ServiceBus.Test.Shared + + + + + + + + + + + \ No newline at end of file diff --git a/test/Microsoft.Azure.ServiceBus.KeyVault.Test/TestUtility.cs b/test/Microsoft.Azure.ServiceBus.Test.Shared/TestUtility.cs similarity index 90% rename from test/Microsoft.Azure.ServiceBus.KeyVault.Test/TestUtility.cs rename to test/Microsoft.Azure.ServiceBus.Test.Shared/TestUtility.cs index af66de0..f37043e 100644 --- a/test/Microsoft.Azure.ServiceBus.KeyVault.Test/TestUtility.cs +++ b/test/Microsoft.Azure.ServiceBus.Test.Shared/TestUtility.cs @@ -1,7 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -namespace Microsoft.Azure.ServiceBus.KeyVault.Test +namespace Microsoft.Azure.ServiceBus.Test.Shared { using System; using System.Diagnostics;