Skip to content

Commit

Permalink
add pubsub via sdk sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Yongguang Zhu committed Jul 18, 2023
1 parent bdda197 commit 3cc3a6b
Show file tree
Hide file tree
Showing 25 changed files with 224 additions and 7 deletions.
6 changes: 4 additions & 2 deletions PubSubConsumer/Controllers/DaprSubscribeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ public ActionResult<DaprSubscribeOutput[]> Get()
{
PubSubName="pubsub",
Topic="quickstarts/wakeup",
Route="/api/wakeup"
Route="/api/wakeup",
DeadLetterTopic="poisonMessages"
},
new DaprSubscribeOutput
{
PubSubName="pubsub",
Topic="quickstarts/sleep",
Route="/api/sleep"
Route="/api/sleep",
DeadLetterTopic="poisonMessages"
}
});
}
Expand Down
3 changes: 3 additions & 0 deletions PubSubConsumer/Models/DaprSubscribeOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ public class DaprSubscribeOutput

[JsonPropertyName("route")]
public string Route { get; set; }

[JsonPropertyName("deadLetterTopic")]
public string DeadLetterTopic { get; set; }
}
}
2 changes: 1 addition & 1 deletion PubSubConsumer/run.cmd
Original file line number Diff line number Diff line change
@@ -1 +1 @@
dapr run --app-id quickstarts-psc --app-port 5000 --dapr-http-port 13504 --components-path ../components -- dotnet run
dapr run --app-id quickstarts-psc --app-port 5000 --dapr-http-port 13504 --resources-path ../components/common -- dotnet run
61 changes: 61 additions & 0 deletions PubSubConsumerViaSdk/Controllers/WeatherForecastController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Dapr;
using Microsoft.AspNetCore.Mvc;
using PubSubConsumerViaSdk.Models;

namespace PubSubConsumerViaSdk.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

private readonly ILogger<WeatherForecastController> _logger;

public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}

[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}

[HttpPost("sleep")]
[Topic("pubsub", "quickstarts/sleep", DeadLetterTopic = "poisonMessages")]
public IActionResult Sleep(MessageInput model)
{
//using (var sr = new StreamReader(Request.Body))
//{
// var payload = await sr.ReadToEndAsync();
// _logger.LogInformation(payload);
//}
_logger.LogInformation(model.Name);
return BadRequest();
}

[HttpPost("wakeup")]
[Topic("pubsub", "quickstarts/wakeup", DeadLetterTopic = "poisonMessages")]
public IActionResult Wakeup(MessageInput model)
{
//using (var sr = new StreamReader(Request.Body))
//{
// var payload = await sr.ReadToEndAsync();
// _logger.LogInformation(payload);
//}
_logger.LogInformation(model.Name);
return Ok();
}
}
}
14 changes: 14 additions & 0 deletions PubSubConsumerViaSdk/Models/MessageInput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace PubSubConsumerViaSdk.Models
{
public class MessageInput
{
[JsonPropertyName("name")]
public string Name { get; set; } = string.Empty;
}
}
19 changes: 19 additions & 0 deletions PubSubConsumerViaSdk/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.AspNetCore.Builder;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers().AddDapr();

var app = builder.Build();

// Configure the HTTP request pipeline.

app.UseAuthorization();

app.MapControllers();

app.MapSubscribeHandler();

app.Run();
31 changes: 31 additions & 0 deletions PubSubConsumerViaSdk/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:27996",
"sslPort": 0
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "weatherforecast",
"applicationUrl": "http://localhost:5109",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
13 changes: 13 additions & 0 deletions PubSubConsumerViaSdk/PubSubConsumerViaSdk.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapr.AspNetCore" Version="1.11.0" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions PubSubConsumerViaSdk/WeatherForecast.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace PubSubConsumerViaSdk
{
public class WeatherForecast
{
public DateOnly Date { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

public string? Summary { get; set; }
}
}
8 changes: 8 additions & 0 deletions PubSubConsumerViaSdk/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions PubSubConsumerViaSdk/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
1 change: 1 addition & 0 deletions PubSubConsumerViaSdk/run.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dapr run --app-id quickstarts-psc-sdk --app-port 5109 --resources-path ../components/common -- dotnet run
2 changes: 1 addition & 1 deletion PubSubProducer/run.cmd
Original file line number Diff line number Diff line change
@@ -1 +1 @@
dapr run --app-id quickstarts-psp --dapr-http-port 13505 --components-path ../components -- dotnet run
dapr run --app-id quickstarts-psp --dapr-http-port 13505 --resources-path ../components/common -- dotnet run
16 changes: 16 additions & 0 deletions PubSubProducerViaSdk/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Dapr.Client;

const string pubsubName = "pubsub";
const string topicName1 = "quickstarts/wakeup";
const string topicName2 = "quickstarts/sleep";

var daprClient = new DaprClientBuilder().Build();
Console.WriteLine($"Starting publish message to: {pubsubName}");

Console.WriteLine($"To topic: {topicName1} ...");
daprClient.PublishEventAsync(pubsubName, topicName1, new { name = "Jack" }).Wait();

Console.WriteLine($"To topic: {topicName2} ...");
daprClient.PublishEventAsync(pubsubName, topicName2, new { name = "Mike" }).Wait();

Console.WriteLine("Successfully published message.");
14 changes: 14 additions & 0 deletions PubSubProducerViaSdk/PubSubProducerViaSdk.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapr.Client" Version="1.11.0" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions PubSubProducerViaSdk/run.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dapr run --app-id quickstarts-psp-sdk --resources-path ../components/common -- dotnet run
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: redisconfig
name: appconfig
spec:
type: configuration.redis
version: v1
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: localsecretstore
name: secretstore
spec:
type: secretstores.local.env
version: v1
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 13 additions & 1 deletion dapr-dotnet-quickstarts.sln
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfigurationWithSdk", "Con
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SecretsManagementWithSdk", "SecretsManagementWithSdk\SecretsManagementWithSdk.csproj", "{356F17A3-8AC3-4E02-A6FB-0B82B95895BB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BindingsWithSdk", "BindingsWithSdk\BindingsWithSdk.csproj", "{C5536E04-62E3-4FC3-AEA7-C1AF80364E74}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BindingsWithSdk", "BindingsWithSdk\BindingsWithSdk.csproj", "{C5536E04-62E3-4FC3-AEA7-C1AF80364E74}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PubSubConsumerViaSdk", "PubSubConsumerViaSdk\PubSubConsumerViaSdk.csproj", "{407155E9-7F05-4B6E-8DCC-8D18AD148B3F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PubSubProducerViaSdk", "PubSubProducerViaSdk\PubSubProducerViaSdk.csproj", "{EF19C49A-E3F9-4327-AB81-656702A07C3D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -64,6 +68,14 @@ Global
{C5536E04-62E3-4FC3-AEA7-C1AF80364E74}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C5536E04-62E3-4FC3-AEA7-C1AF80364E74}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C5536E04-62E3-4FC3-AEA7-C1AF80364E74}.Release|Any CPU.Build.0 = Release|Any CPU
{407155E9-7F05-4B6E-8DCC-8D18AD148B3F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{407155E9-7F05-4B6E-8DCC-8D18AD148B3F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{407155E9-7F05-4B6E-8DCC-8D18AD148B3F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{407155E9-7F05-4B6E-8DCC-8D18AD148B3F}.Release|Any CPU.Build.0 = Release|Any CPU
{EF19C49A-E3F9-4327-AB81-656702A07C3D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EF19C49A-E3F9-4327-AB81-656702A07C3D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EF19C49A-E3F9-4327-AB81-656702A07C3D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EF19C49A-E3F9-4327-AB81-656702A07C3D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 3cc3a6b

Please sign in to comment.