Microservice does not receive RabbitMq message - c#

When I add a new user to my IdentiyServerService the following MassTransit code is called:
var newUserCreated = new UserCreated
{
UserId = userId.ToString(),
Name = user.Name
};
await _bus.Publish(newUserCreated);
My destination is that my ProfileService receive this event by RabbitMq.
My RabbitMq configuration in my Startup.cs (IdentiyServerService)
private static void ConfigureBus(ContainerBuilder builder)
{
builder.Register(context =>
{
return Bus.Factory.CreateUsingRabbitMq(config =>
{
var host = config.Host(new Uri("rabbitmq://localhost"), h =>
{
h.Username("guest");
h.Password("guest");
});
});
}).As<IBus, IBusControl, IPublishEndpoint>().SingleInstance();
}
I start the bus like this
//Startup.cs IdentityServerService
var container = containerBuilder.Build();
var busControl = container.Resolve<IBusControl>();
busControl.Start();
The configuration in my ProfileService look quite almost the same. The difference is, that I add an consumer in my Startup.cs (ProfileService)
config.ReceiveEndpoint(host, "user_queue", ep =>
{
ep.Consumer<UserCreatedConsumer>(); // The consumer is registered explicitly this time.
});
I add an IConsumer as well
public class UserCreatedConsumer : IConsumer<UserCreated>
{
public Task Consume(ConsumeContext<UserCreated> context)
{
var user = context.Message;
Debug.WriteLine("My debug string here");
return TaskUtil.Completed;
}
}
When I create a new user the messages receive the RabbitMq (publish rates increase). But then nothing goes on. The total number of messages in the Queued messages does not change.
I have two connections (I would expect IdentityServerService and ProfileService) and I have different queues (I was only expecting one: user_queue)
When I implement the IConsumer inside my IdentityServerService I receive the message.
I have no error log, warnings or something else.
Anyway... 1) Why does ProfileService not receive the message? 2) And why do I have so much queues?
If you need some more information... please tell
Edit
When I send a message in the rabbitMq-management my ProfileService receive a message, but now I get the following error
MassTransit.Messages Error: 0 : R-FAULT
rabbitmq://localhost/user_queue_new Value cannot be null.
Parameter name: source,
System.Runtime.Serialization.SerializationException: An exception occurred while deserializing the message envelope ---> System.ArgumentNullException: Value cannot be null.
Parameter name: source

Related

How to retrieve messages from RabbitMQ DeadLetter queue using MassTransit?

I'm trying to notify the user when a message is not received after some time, using MassTransit and RabbitMQ.
From what I read, the timeout is set using the TimeToLive property when the message is published. When that specified time runs out, the message should be automatically added to a Dead Letter queue, named with a "_skipped" at the end.
How do I retrieve messages from Dead Letter queues? In my attempt below, the message is added to the both queues right away, and it never times out.
I think I could do this using sagas, but it seems like a over complicated solution for such a simple problem, so I would like to avoid using it if at all possible.
static void Main(string[] args)
{
var bus = CreateBus("rabbitmq://localhost/", "guest", "guest", true);
var msg = new TestMessage("First Message");
LogMessageSent(msg);
bus.Publish(msg, c => c.TimeToLive = TimeSpan.FromSeconds(15));
Console.ReadKey();
bus.Stop();
bus = CreateBus("rabbitmq://localhost/", "guest", "guest", false);
msg = new TestMessage("SecondMessage");
LogMessageSent(msg);
bus.Publish(msg, c => c.TimeToLive = TimeSpan.FromSeconds(15));
Console.ReadKey();
bus.Stop();
}
private static IBusControl CreateBus(string rabbitUrl, string username, string password, bool enableEndpoint)
{
var bus = Bus.Factory.CreateUsingRabbitMq(c =>
{
var host = c.Host(new Uri(rabbitUrl), h =>
{
h.Username(username);
h.Password(password);
});
if (enableEndpoint)
{
c.ReceiveEndpoint(host, "TestQueue", x =>
{
x.Handler<TestMessage>(e => LogMessageReceived(e.Message, "TestQueue"));
});
}
c.ReceiveEndpoint(host, "TestQueue_skipped", x =>
{
x.Handler<TestMessage>(e => LogMessageReceived(e.Message, "TestQueue_skipped"));
});
});
bus.Start();
return bus;
}
private static void LogMessageSent(TestMessage msg)
{
Console.WriteLine(string.Format("{0} - Message \"{1}\" sent.", DateTime.Now.ToString("HH:mm:ss"), msg.Content));
}
private static Task LogMessageReceived(TestMessage msg, string queueName)
{
Console.WriteLine(string.Format("{0} - Message \"{1}\" received on queue \"{2}\".", DateTime.Now.ToString("HH:mm:ss"), msg.Content, queueName));
return Task.CompletedTask;
}
public class TestMessage
{
public string Content { get; }
public TestMessage(string content)
{
Content = content;
}
}
Because you are calling Publish, the message is sent to every subscriber. Since each receive endpoint is adding the consumer, that creates a subscription (and subsequent exchange binding in RabbitMQ) for that message type. You can disable this by specifying BindMessageExchanges = false on that skipped receive endpoint. You will need to manually remove the exchange binding on the broker.
As to your TimeToLive question, that isn't how it works. TimeToLive is passed to the broker, and if the message expires, it is moved to a broker-specified dead-letter queue, if so configured. It is not moved to the skipped queue which has a different meaning in MassTransit. In MassTransit, the skipped queue is for messages that are delivered to a receive endpoint but there wasn't a consumer configured on that endpoint to consume the message.
For RabbitMQ, you can configure the dead-letter queue in MassTransit by using:
endpoint.BindDeadLetterQueue("dead-letter-queue-name");
This will configure the broker so that messages which reach their TTL are moved to the specified exchange/queue. Then your consumer on that receive endpoint will be able to consume them (again, be sure to set BindMessageExchanges = false on the dead-letter receive endpoint.
For example:
c.ReceiveEndpoint(host, "TestQueue_expired", x =>
{
x.BindMessageExchanges = false;
x.Handler<TestMessage>(e => LogMessageReceived(e.Message, "TestQueue_expired"));
});
And then your original receive endpoint:
c.ReceiveEndpoint(host, "TestQueue", x =>
{
x.BindDeadLetterQueue("TestQueue_expired");
x.Handler<TestMessage>(e => LogMessageReceived(e.Message, "TestQueue"));
});

No client method with the name 'x' found with SignalR

I have an ASP.NET Boilerplate v3.6.2 project (.NET Core / Angular) in which I need to call a client function from a backend method, so I'm using the ASP.NET Core SignalR implementation.
I followed the official documentation, so:
In the backend
In my module, I added the dependency to AbpAspNetCoreSignalRModule:
[DependsOn(typeof(AbpAspNetCoreSignalRModule))]
public class MyModule : AbpModule
And added this NuGet package to the module's project.
Then I extended the AbpCommonHub class to take advantage of the built-in implementation of the SignalR Hub, adding a SendMessage() method used to send the message:
public interface IMySignalRHub : ITransientDependency
{
Task SendMessage(string message);
}
public class MySignalRHub: AbpCommonHub, IMySignalRHub {
protected IHubContext<MySignalRHub> _context;
protected IOnlineClientManager onlineClientManager;
protected IClientInfoProvider clientInfoProvider;
public MySignalRHub(
IHubContext<MySignalRHub> context,
IOnlineClientManager onlineClientManager,
IClientInfoProvider clientInfoProvider)
: base(onlineClientManager, clientInfoProvider) {
AbpSession = NullAbpSession.Instance;
_context = context;
}
public async Task SendMessage(string message) {
await _context.Clients.All.SendAsync("getMessage", string.Format("User {0}: {1}", AbpSession.UserId, message));
}
}
I changed the mapping of the '/signalr' url to MySignalRHub:
public class Startup {
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
[...]
# if FEATURE_SIGNALR
app.UseAppBuilder(ConfigureOwinServices);
# elif FEATURE_SIGNALR_ASPNETCORE
app.UseSignalR(routes => {
routes.MapHub<MySignalRHub>("/signalr");
});
# endif
[...]
}
}
Then I use the hub to send a message in a service implementation:
public class MyAppService: AsyncCrudAppService<MyEntity, MyDto, int, PagedAndSortedResultRequestDto, MyCreateDto, MyDto>, IMyAppService {
private readonly IMySignalRHub _hub;
public MyAppService(IRepository<MyEntity> repository, IMySignalRHub hub) : base(repository) {
_hub = hub;
}
public override Task<MyDto> Create(MyCreateDto input) {
_hub.SendMessage("test string").Wait();
[...]
}
}
In the client
All the configurations and inclusions are already in place from the original template. When I open the Angular app I can see this console logs:
DEBUG: Connected to SignalR server!
DEBUG: Registered to the SignalR server!
When I try to call the backend service which sends the message to the client, I get this warning in console:
Warning: No client method with the name 'getMessage' found.
I tried many solutions found in the official documentation and on the Internet but none of them worked. I'm not able to define the 'getMessage' handler in the client code.
Some non-working examples I tried:
Implementation 1:
// This point is reached
abp.event.on('getMessage', userNotification => {
debugger; // Never reaches this point
});
Implementation 2:
// This point is reached
abp.event.on('abp.notifications.received', userNotification => {
debugger; // Never reaches this point
});
Implementation 3:
// This is taken from the official documentation and triggers the error:
// ERROR TypeError: abp.signalr.startConnection is not a function
abp.signalr.startConnection('/signalr', function (connection) {
connection.on('getMessage', function (message) {
console.log('received message: ' + message);
});
});
Have you ever found yourself in this situation? Do you have a simple working example of the handler definition in the Angular client?
UPDATE
I tried this alternative solution, changing the implementation of the SignalRAspNetCoreHelper class (a shared class which is shipped with the base template):
export class SignalRAspNetCoreHelper {
static initSignalR(): void {
var encryptedAuthToken = new UtilsService().getCookieValue(AppConsts.authorization.encrptedAuthTokenName);
abp.signalr = {
autoConnect: true,
connect: undefined,
hubs: undefined,
qs: AppConsts.authorization.encrptedAuthTokenName + "=" + encodeURIComponent(encryptedAuthToken),
remoteServiceBaseUrl: AppConsts.remoteServiceBaseUrl,
startConnection: undefined,
url: '/signalr'
};
jQuery.getScript(AppConsts.appBaseUrl + '/assets/abp/abp.signalr-client.js', () => {
// ADDED THIS
abp.signalr.startConnection(abp.signalr.url, function (connection) {
connection.on('getMessage', function (message) { // Register for incoming messages
console.log('received message: ' + message);
});
});
});
}
}
Now in the console I can see both the messages:
Warning: No client method with the name 'getMessage' found.
SignalRAspNetCoreHelper.ts:22 received message: User 2: asd
So it is working, but not fully. Somewhere the 'getMessage' handler is not visible.
What is the proper way to implement the messages handler in Angular with ASP.NET Boilerplate?
You should use Clients.Others.SendAsync or Client.AllExcept.SendAsync instead of Clients.All.SendAsync.
Clients.All.SendAsync is designed for scenarios where the client wants to send AND receive messages (like a chat room). Hence all connected clients are supposed to implement connection.on('getMessage', in order to receive the notifications. If they don't, they raise the warning No client method with the name 'x' found when receiving back the notification they just pushed.
In your scenario, I understand you have one kind of client pushing notifications and another kind receiving them (like a GPS device sending positions to a tracking application). In that scenario, using Clients.Others.SendAsync or Client.AllExcept.SendAsync will ensure pushing clients will not be broadcasted back the notification they (or their kind) just pushed.
Set autoConnect: false to start your own connection:
abp.signalr = {
autoConnect: false,
// ...
};
Better yet...
Don't extend AbpCommonHub. You'll find that real-time notification stops working and you need to replace IRealTimeNotifier because SignalRRealTimeNotifier uses AbpCommonHub.
Do you have a simple working example of the handler definition in the Angular client?
What is the proper way to implement the messages handler in Angular with ASPNet Boilerplate?
Follow the documentation and create a separate hub.
I was facing the same error in my Angular application where I use the package "#aspnet/signalr": "1.1.4".
The cause of this issue was I wasn't calling the subscribe method after join the channel.
public async getWorkSheetById(worksheetId: string): Promise < Worksheet > {
const worksheet: Worksheet = await this._worksheetService.getWorksheet(worksheetId);
this.displayWorksheet(worksheet);
await this._worksheetEventsService.joinWorksheetChannel(this._loadedWorksheet.id);
return worksheet;
}
So, to fix this I called the subscribe method after join await this.subscribeToSendMethod(this._loadedWorksheet))
public subscribeToSendMethod(loadedWorksheet: Worksheet): Worksheet {
let newWorksheet: Worksheet;
this._hubConnection.on('Send', (groupId: string, payloadType: string, payload: string, senderUserId: string)=> {
newWorksheet=this._worksheetHandler.handlePayload(payloadType, payload, loadedWorksheet);
this.displayWorksheet(newWorksheet);
}
);
return newWorksheet;
}

Multiple messages to the bot in quick succession crash it

Setup
I have a a bot that runs on .NET + Bot Framework + Azure + Facebook Messenger.
Initial Problem
I was trying to solve a problem when sending several messages to the bot triggers an exception and HTTP error 412. Microsoft describes this problem here: https://learn.microsoft.com/en-us/bot-framework/troubleshoot-general-problems#what-causes-an-error-with-http-status-code-412-precondition-failed-or-http-status-code-409-conflict
First Solution
In the page above, Microsoft provides an outdated sample code to resolve this issue. In this github issue, there is a revised version of that code that is supposed to work. I put it inside the constructor of my MessageController:
static MessagesController()
{
// Prevent exception in the bot and HTTP error 412 when the user
// sends multiple messages in quick succession. This may cause
// potential problems with consistency of getting/setting user
// properties.
// See https://learn.microsoft.com/en-us/bot-framework/troubleshoot-general-problems#what-causes-an-error-with-http-status-code-412-precondition-failed-or-http-status-code-409-conflict
// for details. The above link contains wrong code sample, revised
// code is from here: https://github.com/Microsoft/BotBuilder/issues/2345
var builder = new ContainerBuilder();
builder
.Register(c => new CachingBotDataStore(c.ResolveKeyed<IBotDataStore<BotData>>(typeof(ConnectorStore)), CachingBotDataStoreConsistencyPolicy.LastWriteWins))
.As<IBotDataStore<BotData>>()
.AsSelf()
.InstancePerLifetimeScope();
builder.Update(Conversation.Container);
}
Second Problem
Now, the exception still occurs when I send several messages to the bot in a quick succession. However, it changed from HTTP error 412 to something else:
One or more errors occurred. at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task1.get_Result() at MyBot.SetUserDataProperty(Activity activity, String PropertyName, String ValueToSet) in C:\Users\xxx.cs:line 230
Update: I've checked the InnerException of the above and it turns out to be the same old HTTP error 412:
The remote server returned an error: (412) Precondition Failed.
The offending code is a function that writes to the bot storage. The line 230 referenced above is the last line of this function:
public static void SetUserDataProperty(Activity activity, string PropertyName, string ValueToSet)
{
StateClient client = activity.GetStateClient();
BotData userData = client.BotState.GetUserData(activity.ChannelId, activity.From.Id);
userData.SetProperty<string>(PropertyName, ValueToSet);
//client.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData);
// Await async call without making the function asynchronous:
var temp = Task.Run(() => client.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData)).Result;
}
Question
What else can I do to make sure that the user is able to send multiple messages in quick succession without triggering an exception when writing to the BotState storage?
I think there are a few issues here
The way you are trying to do this activity.GetStateClient(); is a only intended to be used for prototyping. We do no reccomend this method for production level code. You can set user data like context.UserData.SetValue("food", "Nachos" ); in the dialog and the values will automagically get saved when the dialog is serialized.
Most likely you are calling this method SetUserDataProperty from a dialog so when you do this var temp = Task.Run(() => client.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData)).Result; it is conflicting and causing the error.
please review this blog post to learn more
Here is how to implement your follow up question:
if (activity.Type == ActivityTypes.Message)
{
var message = activity as IMessageActivity;
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
{
var botDataStore = scope.Resolve<IBotDataStore<BotData>>();
var key = new AddressKey()
{
BotId = message.Recipient.Id,
ChannelId = message.ChannelId,
UserId = message.From.Id,
ConversationId = message.Conversation.Id,
ServiceUrl = message.ServiceUrl
};
ConversationReference r = new ConversationReference();
var userData = await botDataStore.LoadAsync(key, BotStoreType.BotUserData, CancellationToken.None);
userData.SetProperty("key 1", "value1");
userData.SetProperty("key 2", "value2");
await botDataStore.SaveAsync(key, BotStoreType.BotUserData, userData, CancellationToken.None);
await botDataStore.FlushAsync(key, CancellationToken.None);
}
await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
}
you will need to implement this class or something similar:
public class AddressKey : IAddress
{
public string BotId { get; set; }
public string ChannelId { get; set; }
public string ConversationId { get; set; }
public string ServiceUrl { get; set; }
public string UserId { get; set; }
}

NServiceBus Handler throwing metadata errors

After updating NServiceBus to the latest version released last week (which also has a new implementation), I'm seeing weird errors when sending json from the client.
I will send a message from the client and the receiver displays this message:
2016-10-18 22:16:33.612 INFO MFG.Receiver.DeviceHandler Got message with id: a222b136-6a4e-474e-8012-cc1c24e5e539
I have a breakpoint in my handler, below, and it shows the message object is baked and there should not be any issues.
public class DeviceHandler : IHandleMessages<DeviceRequest>
{
private readonly IDeviceProvider _provider = new DeviceProvider();
private static readonly ILog Log = LogManager.GetLogger<DeviceHandler>();
public Task Handle(DeviceRequest message, IMessageHandlerContext context)
{
Log.Info($"Got message with id: {context.MessageId}");
...
return context.SendLocal($"Message with Id {context.MessageId} received.");
}
}
When it hits the reply method at the end, it throws the below errors:
2016-10-18 22:16:33.666 INFO NServiceBus.RecoverabilityExecutor Immediate Retry is going to retry message 'a222b136-6a4e-474e-8012-cc1c24e5e539' because of an exception:
System.Exception: Could not find metadata for 'System.String'.
Ensure the following:
1. 'System.String' is included in initial scanning.
2. 'System.String' implements either 'IMessage', 'IEvent' or 'ICommand' or alternatively, if you don't want to implement an interface, you can use 'Unobtrusive Mode'.
at NServiceBus.Unicast.Messages.MessageMetadataRegistry.GetMessageMetadata(Type messageType) in C:\Build\src\NServiceBus.Core\Unicast\Messages\MessageMetadataRegistry.cs:line 39
I'm not sure why it would throw the System.String error, after it already received the message from the handler and the properties are populated...
The json sent looks like this:
{
"$type": "DeviceRequest, MFG.Domain",
"Id": "devices-65",
"DeviceId": 1,
"Location": "Orlando",
"DeviceType": "test"
}
My Sender (client) looks like this:
static void Main()
{
...
using (var channel = connection.CreateModel())
{
var messageId = Guid.NewGuid().ToString();
var properties = channel.CreateBasicProperties();
properties.MessageId = messageId;
var payload = GenerateJsonPayload();
channel.BasicPublish(string.Empty, ServerEndpointName, false, properties, Encoding.UTF8.GetBytes(payload));
Console.WriteLine($"Message with id {messageId} sent to queue.");
}
...
}
public static string GenerateJsonPayload()
{
var obj = new DeviceRequest
{
DeviceId = 1,
DeviceType = "test",
Location = "Orlando"
};
var settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
};
var result = JsonConvert.SerializeObject(obj, Formatting.Indented, settings);
return result;
}
I've had the "could not find metadata" issue before, and it was due to malformed json or not having type. If I remove the JsonSerializerSettings, and just passed a serialized object, I instead get errors:
2016-10-18 22:31:27.698 ERROR NServiceBus.RecoverabilityExecutor Moving message '6405179d-ea36-4264-af2a-704da19af120' to the error queue 'error' because processing failed due to an exception:
NServiceBus.MessageDeserializationException: An error occurred while attempting to extract logical messages from transport message 6405179d-ea36-4264-af2a-704da19af120 ---> System.Exception: Could not find metadata for 'Newtonsoft.Json.Linq.JObject'.
I have no idea what I'm missing here and this wasn't an issue with the previous version. Is this a bug or ... ?
Use a concrete message type for your SendLocal operation.
As part of handling the message you are doing return context.SendLocal($"Message with Id {context.MessageId} received.");. This will try to send a message of type "string" to the local queue. NServiceBus is telling you that no message metadata was registered for the message type of "string". Therefor it can not construct the message and throws an exception.

MassTransit not receiving Azure ServiceBus Topic Messages

I'm monitoring the topic and subs and messages are getting in, however my masstransit consumer is not receiving anything.
Here's how it's been setup:
var bus = Bus.Factory.CreateUsingAzureServiceBus(
cfg =>
{
var azSbHost = cfg.Host(new Uri(CloudConfigurationManager.GetSetting("ServiceBus.Url"))
, host =>
{
host.TokenProvider = TokenProvider
.CreateSharedAccessSignatureTokenProvider
(CloudConfigurationManager.GetSetting("ServiceBus.SharedAccessKeyName"),
CloudConfigurationManager.GetSetting("ServiceBus.AccessKey"),
TokenScope.Namespace);
});
cfg.ReceiveEndpoint(azSbHost,
e =>
{
e.Consumer<PingConsumer>();
});
//azSbHost.
});
The Ping Consumer:
public class PingConsumer : IConsumer<Ping>
{
public async Task Consume(ConsumeContext<Ping> pingContext)
{
pingContext.Respond(new Pong
{
Message = "Pong: " + pingContext.Message.Message
});
}
}
And the sender:
var pong = await _bus.CreatePublishRequestClient<Ping, Pong>(TimeSpan.FromSeconds(10),null ).Request(
new Ping {Message = "Ping: " + message});
In Azure, I'm seeing my message count climbing up and not going down. So messages are getting to the queue, but consumer is not consuming the message.
I was missing a VERY important key call to make it all work on both client and server side.
Bus.Start

Categories