mirror of https://github.com/dapr/dotnet-sdk.git
Renames and add data property.
This commit is contained in:
parent
46b86e21d3
commit
c3ba1505df
|
@ -1,5 +1,7 @@
|
||||||
// See https://aka.ms/new-console-template for more information
|
// See https://aka.ms/new-console-template for more information
|
||||||
|
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
using Dapr.Messaging.PublishSubscribe;
|
using Dapr.Messaging.PublishSubscribe;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
@ -14,25 +16,29 @@ var client = DaprPublishSubscribeClient.Create(new() { LoggerFactory = loggerFac
|
||||||
|
|
||||||
Console.WriteLine("Subscribing to topic A...");
|
Console.WriteLine("Subscribing to topic A...");
|
||||||
|
|
||||||
using var subscriptionA = client.SubscribeAsync(
|
using var subscriptionA = client.Subscribe(
|
||||||
"pubsub",
|
"pubsub",
|
||||||
"topicA",
|
"topicA",
|
||||||
(request, cancellationToken) =>
|
(request, cancellationToken) =>
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Received message on topic A: {request}");
|
Console.WriteLine($"Received message on topic A: {request}");
|
||||||
|
|
||||||
|
Console.WriteLine($"Data is: {JsonSerializer.Deserialize<TopicData>(request.Data.Span)}");
|
||||||
|
|
||||||
return Task.FromResult(TopicResponse.Drop);
|
return Task.FromResult(TopicResponse.Drop);
|
||||||
});
|
});
|
||||||
|
|
||||||
Console.WriteLine("Subscribing to topic B...");
|
Console.WriteLine("Subscribing to topic B...");
|
||||||
|
|
||||||
using var subscriptionB = client.SubscribeAsync(
|
using var subscriptionB = client.Subscribe(
|
||||||
"pubsub",
|
"pubsub",
|
||||||
"topicB",
|
"topicB",
|
||||||
(request, cancellationToken) =>
|
(request, cancellationToken) =>
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Received message on topic B: {request}");
|
Console.WriteLine($"Received message on topic B: {request}");
|
||||||
|
|
||||||
|
Console.WriteLine($"Data is: {JsonSerializer.Deserialize<TopicData>(request.Data.Span)}");
|
||||||
|
|
||||||
return Task.FromResult(TopicResponse.Success);
|
return Task.FromResult(TopicResponse.Success);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -41,3 +47,9 @@ Console.WriteLine("Waiting 30s to exit...");
|
||||||
await Task.Delay(TimeSpan.FromSeconds(30));
|
await Task.Delay(TimeSpan.FromSeconds(30));
|
||||||
|
|
||||||
Console.WriteLine("Exiting...");
|
Console.WriteLine("Exiting...");
|
||||||
|
|
||||||
|
sealed record TopicData
|
||||||
|
{
|
||||||
|
[JsonPropertyName("test")]
|
||||||
|
public string? Test { get; init; }
|
||||||
|
}
|
||||||
|
|
|
@ -62,9 +62,9 @@ public abstract class DaprPublishSubscribeClient
|
||||||
/// <param name="topicName"></param>
|
/// <param name="topicName"></param>
|
||||||
/// <param name="handler"></param>
|
/// <param name="handler"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public IDisposable SubscribeAsync(string pubSubName, string topicName, TopicRequestHandler handler)
|
public IDisposable Subscribe(string pubSubName, string topicName, TopicRequestHandler handler)
|
||||||
{
|
{
|
||||||
return SubscribeAsync(pubSubName, topicName, handler, null);
|
return Subscribe(pubSubName, topicName, handler, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -75,5 +75,5 @@ public abstract class DaprPublishSubscribeClient
|
||||||
/// <param name="handler"></param>
|
/// <param name="handler"></param>
|
||||||
/// <param name="options"></param>
|
/// <param name="options"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public abstract IDisposable SubscribeAsync(string pubSubName, string topicName, TopicRequestHandler handler, DaprSubscriptionOptions? options);
|
public abstract IDisposable Subscribe(string pubSubName, string topicName, TopicRequestHandler handler, DaprSubscriptionOptions? options);
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@ sealed class DaprPublishSubscribeGrpcClient : DaprPublishSubscribeClient
|
||||||
this.logger = options?.LoggerFactory?.CreateLogger<DaprPublishSubscribeGrpcClient>();
|
this.logger = options?.LoggerFactory?.CreateLogger<DaprPublishSubscribeGrpcClient>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override IDisposable SubscribeAsync(string pubSubName, string topicName, TopicRequestHandler handler, DaprSubscriptionOptions? options)
|
public override IDisposable Subscribe(string pubSubName, string topicName, TopicRequestHandler handler, DaprSubscriptionOptions? options)
|
||||||
{
|
{
|
||||||
var cts = new CancellationTokenSource();
|
var cts = new CancellationTokenSource();
|
||||||
|
|
||||||
|
@ -94,6 +94,7 @@ sealed class DaprPublishSubscribeGrpcClient : DaprPublishSubscribeClient
|
||||||
Type = response.Type,
|
Type = response.Type,
|
||||||
SpecVersion = response.SpecVersion,
|
SpecVersion = response.SpecVersion,
|
||||||
DataContentType = response.DataContentType,
|
DataContentType = response.DataContentType,
|
||||||
|
Data = response.Data.Memory,
|
||||||
Topic = response.Topic,
|
Topic = response.Topic,
|
||||||
PubSubName = response.PubsubName,
|
PubSubName = response.PubsubName,
|
||||||
Path = response.Path,
|
Path = response.Path,
|
||||||
|
|
|
@ -11,6 +11,8 @@
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace Dapr.Messaging.PublishSubscribe;
|
namespace Dapr.Messaging.PublishSubscribe;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -43,6 +45,11 @@ public sealed record TopicRequest
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string DataContentType { get; init; } = default!;
|
public string DataContentType { get; init; } = default!;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public ReadOnlyMemory<byte> Data { get; init; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
Loading…
Reference in New Issue