Skip to content
Draft
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

This file was deleted.

40 changes: 36 additions & 4 deletions samples/aws/lambda-sqs/SQSLambda_3/RegularEndpoint/Program.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,54 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NServiceBus;

Console.Title = "RegularEndpoint";

var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<InputLoopService>();

var endpointConfiguration = new EndpointConfiguration("RegularEndpoint");

endpointConfiguration.UseSerialization<SystemJsonSerializer>();
endpointConfiguration.UseTransport<SqsTransport>();


Console.WriteLine("Press any key, the application is starting");
Console.ReadKey();
Console.WriteLine("Starting...");

builder.UseNServiceBus(endpointConfiguration);
await builder.Build().RunAsync();

var host = builder.Build();
await host.StartAsync();

// Get the required services
var messageSession = host.Services.GetRequiredService<IMessageSession>();
// Register a cancellation token to gracefully handle application shutdown
var ct = host.Services.GetRequiredService<IHostApplicationLifetime>().ApplicationStopping;

Console.WriteLine("Press [ENTER] to send a message to the serverless endpoint queue.");
Console.WriteLine("Press Ctrl+C to shut down.");

// Wait for user input to publish messages
while (!ct.IsCancellationRequested)
{
if (!Console.KeyAvailable)
{
// If no key is pressed, wait for a short time before checking again
await Task.Delay(100, CancellationToken.None);
continue;
}

var key = Console.ReadKey();
Console.WriteLine();

if (key.Key == ConsoleKey.Enter)
{
await messageSession.Send("ServerlessEndpoint", new TriggerMessage());
Console.WriteLine("Message sent to the serverless endpoint queue.");
}
}

// Wait for the host to stop gracefully
await host.StopAsync();
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

public class ResponseMessageHandler(ILogger<ResponseMessageHandler> logger) : IHandleMessages<ResponseMessage>
{

public Task Handle(ResponseMessage message, IMessageHandlerContext context)
{
logger.LogInformation("Handling {MessageType} in RegularEndpoint", nameof(ResponseMessage));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using NServiceBus;
using NServiceBus.Logging;

#region TriggerMessageHandler
public class TriggerMessageHandler(ILogger<TriggerMessageHandler> logger) : IHandleMessages<TriggerMessage>
public class TriggerMessageHandler : IHandleMessages<TriggerMessage>
{
static readonly ILog Log = LogManager.GetLogger<TriggerMessageHandler>();

public async Task Handle(TriggerMessage message, IMessageHandlerContext context)
{
logger.LogInformation("Handling {MessageType} in ServerlessEndpoint.", nameof(TriggerMessage));
Log.Info($"Handling {nameof(TriggerMessage)} in ServerlessEndpoint.");
await context.Send(new ResponseMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RegularEndpoint\RegularEndpoint.csproj
44 changes: 44 additions & 0 deletions samples/aws/lambda-sqs/SQSLambda_4/AWSLambda-SQS.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29728.190
MinimumVisualStudioVersion = 15.0.26730.12
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServerlessEndpoint", "ServerlessEndpoint\ServerlessEndpoint.csproj", "{D9FB3A23-D74F-4754-9A22-ACF829EEEB70}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RegularEndpoint", "RegularEndpoint\RegularEndpoint.csproj", "{06754C55-CA74-4F17-9472-244B8A3FEB1E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Messages", "Messages\Messages.csproj", "{1BEFE981-8E75-4152-B6E3-8D488AE8530F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D9FB3A23-D74F-4754-9A22-ACF829EEEB70}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D9FB3A23-D74F-4754-9A22-ACF829EEEB70}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D9FB3A23-D74F-4754-9A22-ACF829EEEB70}.Debug|x64.ActiveCfg = Debug|Any CPU
{D9FB3A23-D74F-4754-9A22-ACF829EEEB70}.Debug|x64.Build.0 = Debug|Any CPU
{D9FB3A23-D74F-4754-9A22-ACF829EEEB70}.Debug|x86.ActiveCfg = Debug|Any CPU
{D9FB3A23-D74F-4754-9A22-ACF829EEEB70}.Debug|x86.Build.0 = Debug|Any CPU
{06754C55-CA74-4F17-9472-244B8A3FEB1E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{06754C55-CA74-4F17-9472-244B8A3FEB1E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{06754C55-CA74-4F17-9472-244B8A3FEB1E}.Debug|x64.ActiveCfg = Debug|Any CPU
{06754C55-CA74-4F17-9472-244B8A3FEB1E}.Debug|x64.Build.0 = Debug|Any CPU
{06754C55-CA74-4F17-9472-244B8A3FEB1E}.Debug|x86.ActiveCfg = Debug|Any CPU
{06754C55-CA74-4F17-9472-244B8A3FEB1E}.Debug|x86.Build.0 = Debug|Any CPU
{1BEFE981-8E75-4152-B6E3-8D488AE8530F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1BEFE981-8E75-4152-B6E3-8D488AE8530F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1BEFE981-8E75-4152-B6E3-8D488AE8530F}.Debug|x64.ActiveCfg = Debug|Any CPU
{1BEFE981-8E75-4152-B6E3-8D488AE8530F}.Debug|x64.Build.0 = Debug|Any CPU
{1BEFE981-8E75-4152-B6E3-8D488AE8530F}.Debug|x86.ActiveCfg = Debug|Any CPU
{1BEFE981-8E75-4152-B6E3-8D488AE8530F}.Debug|x86.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6C66C18C-5075-4811-AF40-2997E8863F3B}
EndGlobalSection
EndGlobal
13 changes: 13 additions & 0 deletions samples/aws/lambda-sqs/SQSLambda_4/Messages/Messages.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>preview</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public class ResponseMessage : IMessage
{
}
3 changes: 3 additions & 0 deletions samples/aws/lambda-sqs/SQSLambda_4/Messages/TriggerMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public class TriggerMessage : IMessage
{
}
46 changes: 46 additions & 0 deletions samples/aws/lambda-sqs/SQSLambda_4/RegularEndpoint/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

Console.Title = "RegularEndpoint";

var builder = Host.CreateApplicationBuilder(args);

var endpointConfiguration = new EndpointConfiguration("RegularEndpoint");
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
endpointConfiguration.UseTransport<SqsTransport>();

builder.UseNServiceBus(endpointConfiguration);

var host = builder.Build();
await host.StartAsync();

// Get the required services
var messageSession = host.Services.GetRequiredService<IMessageSession>();
// Register a cancellation token to gracefully handle application shutdown
var ct = host.Services.GetRequiredService<IHostApplicationLifetime>().ApplicationStopping;

Console.WriteLine("Press [ENTER] to send a message to the serverless endpoint queue.");
Console.WriteLine("Press Ctrl+C to shut down.");

// Wait for user input to publish messages
while (!ct.IsCancellationRequested)
{
if (!Console.KeyAvailable)
{
// If no key is pressed, wait for a short time before checking again
await Task.Delay(100, CancellationToken.None);
continue;
}

var key = Console.ReadKey();
Console.WriteLine();

if (key.Key == ConsoleKey.Enter)
{
await messageSession.Send("ServerlessEndpoint", new TriggerMessage());
Console.WriteLine("Message sent to the serverless endpoint queue.");
}
}

// Wait for the host to stop gracefully
await host.StopAsync();
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<OutputType>Exe</OutputType>
<LangVersion>preview</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Messages\Messages.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NServiceBus.AmazonSQS" Version="9.0.0-alpha.1" />
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Microsoft.Extensions.Logging;

public class ResponseMessageHandler(ILogger<ResponseMessageHandler> logger) : IHandleMessages<ResponseMessage>
{
public Task Handle(ResponseMessage message, IMessageHandlerContext context)
{
logger.LogInformation("Handling {MessageType} in RegularEndpoint", nameof(ResponseMessage));
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace LambdaFunctions;

public static class Endpoint
{
#region EndpointSetup
public static IAwsLambdaSQSEndpoint Configuration => new AwsLambdaSQSEndpoint(context =>
{
var endpointConfiguration = new AwsLambdaSQSEndpointConfiguration("ServerlessEndpoint");
endpointConfiguration.UseSerialization<SystemJsonSerializer>();

var routing = endpointConfiguration.RoutingSettings;

routing.RouteToEndpoint(typeof(TriggerMessage), "ServerlessEndpoint");
routing.RouteToEndpoint(typeof(ResponseMessage), "RegularEndpoint");

return endpointConfiguration;
});
#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Amazon.Lambda.Annotations;
using Amazon.Lambda.Annotations.APIGateway;
using Amazon.Lambda.Core;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace LambdaFunctions
{
public class HttpLambda
{
#region HttpFunctionHandler
[LambdaFunction(Policies = "AWSLambda_FullAccess, AmazonSQSFullAccess")]
[HttpApi(LambdaHttpMethod.Get, "/")]
public async Task<string> HttpGetHandler(ILambdaContext context)
{
await serverlessEndpoint.Send(new TriggerMessage(), context);
return $"{nameof(TriggerMessage)} sent.";
}

#endregion

static readonly IAwsLambdaSQSEndpoint serverlessEndpoint = Endpoint.Configuration;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"profiles": {
"Mock Lambda Test Tool": {
"commandName": "Executable",
"commandLineArgs": "--port 5050",
"workingDirectory": ".\\bin\\$(Configuration)\\net8.0",
"executablePath": "%USERPROFILE%\\.dotnet\\tools\\dotnet-lambda-test-tool-8.0.exe"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
<LangVersion>preview</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Messages\Messages.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Amazon.Lambda.Annotations" Version="1.*" />
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.*" />
<PackageReference Include="Amazon.Lambda.Core" Version="2.*" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.*" />
<PackageReference Include="Amazon.Lambda.SQSEvents" Version="2.*" />
<PackageReference Include="NServiceBus.AmazonSQS" Version="9.0.0-alpha.1" />
<PackageReference Include="NServiceBus.AwsLambda.SQS" Version="4.0.0-alpha.1" />
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.2.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServerlessEndpoint", "ServerlessEndpoint.csproj", "{5203F0FC-9459-4724-C2D4-BA60791FED68}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5203F0FC-9459-4724-C2D4-BA60791FED68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5203F0FC-9459-4724-C2D4-BA60791FED68}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5203F0FC-9459-4724-C2D4-BA60791FED68}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5203F0FC-9459-4724-C2D4-BA60791FED68}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6FBD541B-2DD7-43F4-9807-269CEA802E90}
EndGlobalSection
EndGlobal
Loading