我正在使用两个 .NET Core Web API;一个用于发布内容,另一个用于使用 NATS 流服务器订阅该内容。
在出版商方面
string clientID = "abc-publisher";
string clusterID = "test-cluster";
string subject = "testing Subject1";
string data = "Testing with the API";
byte[] payload = Encoding.UTF8.GetBytes(data);
try
{
var opts = StanOptions.GetDefaultOptions();
//opts.StartWithLastReceived();
opts.NatsURL = StanConsts.DefaultNatsURL;
using (var c = new StanConnectionFactory().CreateConnection(clusterID, clientID,opts))
{
string returnData = c.Publish(subject, payload, (obj, pubArgs) =>
{
string s = pubArgs.GUID;
});
}
}
catch (Exception ex)
{
string msg = ex.Message.ToString();
}
在订阅方
using (var c = new StanConnectionFactory().CreateConnection(clusterID, clientID)) {
var opts = StanSubscriptionOptions.GetDefaultOptions();
opts.StartWithLastReceived();
var s = c.Subscribe(subject, (obj, args) =>
{
str = Encoding.UTF8.GetString(args.Message.Data);
});
}
但是当我运行项目时,我无法转到订阅者的回调方法。