Microsoft service bus - Receive messages from Bus with OnMessage() method
My application has to receive a message every time new message is posted. So I'm using OnMessage()
method as mentioned in Microsoft documentation.
When new messages are posted the OnMessage()
method does not seem to be working. To resolve this, I've placed the code into a separate task with infinite loop. This seems totally wrong.
public void ReceiveMessageFromSubscription(string topicName, string subscriptioName)
{
Task.Factory.StartNew(() =>
{
while (true)
{
SubscriptionClient Client = SubscriptionClient.CreateFromConnectionString(connectionString, topicName, subscriptionName);
Client.OnMessage((message) =>
{
try
{
var message = brokerMessage.GetBody<MessageDto>();
newMessage.AnnounceNewMessage(message);
message.Complete();
}
catch (Exception ex)
{
message.Abandon();
}
});
}
});
}
Whenever there is a message in Subscription the OnMessage()
method has to be called. Can anyone please help me with this.
c# .net azureservicebus
add a comment |
My application has to receive a message every time new message is posted. So I'm using OnMessage()
method as mentioned in Microsoft documentation.
When new messages are posted the OnMessage()
method does not seem to be working. To resolve this, I've placed the code into a separate task with infinite loop. This seems totally wrong.
public void ReceiveMessageFromSubscription(string topicName, string subscriptioName)
{
Task.Factory.StartNew(() =>
{
while (true)
{
SubscriptionClient Client = SubscriptionClient.CreateFromConnectionString(connectionString, topicName, subscriptionName);
Client.OnMessage((message) =>
{
try
{
var message = brokerMessage.GetBody<MessageDto>();
newMessage.AnnounceNewMessage(message);
message.Complete();
}
catch (Exception ex)
{
message.Abandon();
}
});
}
});
}
Whenever there is a message in Subscription the OnMessage()
method has to be called. Can anyone please help me with this.
c# .net azureservicebus
...does not seem to be working...
- this information is not really helpful. It does not say what exactly did not work, i.e. did you get any error messages anywhere, did you try to debug the issue to make sure that it actually does the intended thing.
– JohnB
Nov 23 '18 at 9:54
So you started a new thread, and that thread creates unlimited number of instances ofSubscriptionClient
, as fast as it can, and subscribes each of theSubscriptionClient
s toOnMessage
. Don't you run out of memory right after starting this?
– GSerg
Nov 23 '18 at 9:56
@JohnB ,the OnMessage() method is not calling automatically whenever there is a message in Subscriptionclient
– Harika
Nov 23 '18 at 10:03
@GSerg,Presently it's working fine.But I know that this is not the correct way to achieve this and It will cause Memory out of range issues .That's why I'm trying to find a solution without repeating that loop.I've added that loop to check whether it's working or not.Can you please help me
– Harika
Nov 23 '18 at 10:24
add a comment |
My application has to receive a message every time new message is posted. So I'm using OnMessage()
method as mentioned in Microsoft documentation.
When new messages are posted the OnMessage()
method does not seem to be working. To resolve this, I've placed the code into a separate task with infinite loop. This seems totally wrong.
public void ReceiveMessageFromSubscription(string topicName, string subscriptioName)
{
Task.Factory.StartNew(() =>
{
while (true)
{
SubscriptionClient Client = SubscriptionClient.CreateFromConnectionString(connectionString, topicName, subscriptionName);
Client.OnMessage((message) =>
{
try
{
var message = brokerMessage.GetBody<MessageDto>();
newMessage.AnnounceNewMessage(message);
message.Complete();
}
catch (Exception ex)
{
message.Abandon();
}
});
}
});
}
Whenever there is a message in Subscription the OnMessage()
method has to be called. Can anyone please help me with this.
c# .net azureservicebus
My application has to receive a message every time new message is posted. So I'm using OnMessage()
method as mentioned in Microsoft documentation.
When new messages are posted the OnMessage()
method does not seem to be working. To resolve this, I've placed the code into a separate task with infinite loop. This seems totally wrong.
public void ReceiveMessageFromSubscription(string topicName, string subscriptioName)
{
Task.Factory.StartNew(() =>
{
while (true)
{
SubscriptionClient Client = SubscriptionClient.CreateFromConnectionString(connectionString, topicName, subscriptionName);
Client.OnMessage((message) =>
{
try
{
var message = brokerMessage.GetBody<MessageDto>();
newMessage.AnnounceNewMessage(message);
message.Complete();
}
catch (Exception ex)
{
message.Abandon();
}
});
}
});
}
Whenever there is a message in Subscription the OnMessage()
method has to be called. Can anyone please help me with this.
c# .net azureservicebus
c# .net azureservicebus
edited Nov 23 '18 at 9:52
GSerg
58.9k14101219
58.9k14101219
asked Nov 23 '18 at 9:47
Harika
11
11
...does not seem to be working...
- this information is not really helpful. It does not say what exactly did not work, i.e. did you get any error messages anywhere, did you try to debug the issue to make sure that it actually does the intended thing.
– JohnB
Nov 23 '18 at 9:54
So you started a new thread, and that thread creates unlimited number of instances ofSubscriptionClient
, as fast as it can, and subscribes each of theSubscriptionClient
s toOnMessage
. Don't you run out of memory right after starting this?
– GSerg
Nov 23 '18 at 9:56
@JohnB ,the OnMessage() method is not calling automatically whenever there is a message in Subscriptionclient
– Harika
Nov 23 '18 at 10:03
@GSerg,Presently it's working fine.But I know that this is not the correct way to achieve this and It will cause Memory out of range issues .That's why I'm trying to find a solution without repeating that loop.I've added that loop to check whether it's working or not.Can you please help me
– Harika
Nov 23 '18 at 10:24
add a comment |
...does not seem to be working...
- this information is not really helpful. It does not say what exactly did not work, i.e. did you get any error messages anywhere, did you try to debug the issue to make sure that it actually does the intended thing.
– JohnB
Nov 23 '18 at 9:54
So you started a new thread, and that thread creates unlimited number of instances ofSubscriptionClient
, as fast as it can, and subscribes each of theSubscriptionClient
s toOnMessage
. Don't you run out of memory right after starting this?
– GSerg
Nov 23 '18 at 9:56
@JohnB ,the OnMessage() method is not calling automatically whenever there is a message in Subscriptionclient
– Harika
Nov 23 '18 at 10:03
@GSerg,Presently it's working fine.But I know that this is not the correct way to achieve this and It will cause Memory out of range issues .That's why I'm trying to find a solution without repeating that loop.I've added that loop to check whether it's working or not.Can you please help me
– Harika
Nov 23 '18 at 10:24
...does not seem to be working...
- this information is not really helpful. It does not say what exactly did not work, i.e. did you get any error messages anywhere, did you try to debug the issue to make sure that it actually does the intended thing.– JohnB
Nov 23 '18 at 9:54
...does not seem to be working...
- this information is not really helpful. It does not say what exactly did not work, i.e. did you get any error messages anywhere, did you try to debug the issue to make sure that it actually does the intended thing.– JohnB
Nov 23 '18 at 9:54
So you started a new thread, and that thread creates unlimited number of instances of
SubscriptionClient
, as fast as it can, and subscribes each of the SubscriptionClient
s to OnMessage
. Don't you run out of memory right after starting this?– GSerg
Nov 23 '18 at 9:56
So you started a new thread, and that thread creates unlimited number of instances of
SubscriptionClient
, as fast as it can, and subscribes each of the SubscriptionClient
s to OnMessage
. Don't you run out of memory right after starting this?– GSerg
Nov 23 '18 at 9:56
@JohnB ,the OnMessage() method is not calling automatically whenever there is a message in Subscriptionclient
– Harika
Nov 23 '18 at 10:03
@JohnB ,the OnMessage() method is not calling automatically whenever there is a message in Subscriptionclient
– Harika
Nov 23 '18 at 10:03
@GSerg,Presently it's working fine.But I know that this is not the correct way to achieve this and It will cause Memory out of range issues .That's why I'm trying to find a solution without repeating that loop.I've added that loop to check whether it's working or not.Can you please help me
– Harika
Nov 23 '18 at 10:24
@GSerg,Presently it's working fine.But I know that this is not the correct way to achieve this and It will cause Memory out of range issues .That's why I'm trying to find a solution without repeating that loop.I've added that loop to check whether it's working or not.Can you please help me
– Harika
Nov 23 '18 at 10:24
add a comment |
1 Answer
1
active
oldest
votes
OnMessage API is an asynchronous process that receives messages in an event-driven message pump. It doesn't stop receiving until you either dispose the client or the code that is running it is terminated. The code above is wrong. You should not instantiate a subscription client in a tight loop and register your callback each time. What you should be doing is creating your client, registering a callback with a desired concurrency, and hold on to that client until you no longer need to receive messages.
Remember, it's a message pump that has to run all the time. Official documentation is a bit dry, perhaps this post will help.
In addition to that, I would strongly recommend not to use the legacy client WindowsAzure.ServiceBus which you're using. Instead, prefer the new Microsoft.Azure.ServiceBus client.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444185%2fmicrosoft-service-bus-receive-messages-from-bus-with-onmessage-method%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
OnMessage API is an asynchronous process that receives messages in an event-driven message pump. It doesn't stop receiving until you either dispose the client or the code that is running it is terminated. The code above is wrong. You should not instantiate a subscription client in a tight loop and register your callback each time. What you should be doing is creating your client, registering a callback with a desired concurrency, and hold on to that client until you no longer need to receive messages.
Remember, it's a message pump that has to run all the time. Official documentation is a bit dry, perhaps this post will help.
In addition to that, I would strongly recommend not to use the legacy client WindowsAzure.ServiceBus which you're using. Instead, prefer the new Microsoft.Azure.ServiceBus client.
add a comment |
OnMessage API is an asynchronous process that receives messages in an event-driven message pump. It doesn't stop receiving until you either dispose the client or the code that is running it is terminated. The code above is wrong. You should not instantiate a subscription client in a tight loop and register your callback each time. What you should be doing is creating your client, registering a callback with a desired concurrency, and hold on to that client until you no longer need to receive messages.
Remember, it's a message pump that has to run all the time. Official documentation is a bit dry, perhaps this post will help.
In addition to that, I would strongly recommend not to use the legacy client WindowsAzure.ServiceBus which you're using. Instead, prefer the new Microsoft.Azure.ServiceBus client.
add a comment |
OnMessage API is an asynchronous process that receives messages in an event-driven message pump. It doesn't stop receiving until you either dispose the client or the code that is running it is terminated. The code above is wrong. You should not instantiate a subscription client in a tight loop and register your callback each time. What you should be doing is creating your client, registering a callback with a desired concurrency, and hold on to that client until you no longer need to receive messages.
Remember, it's a message pump that has to run all the time. Official documentation is a bit dry, perhaps this post will help.
In addition to that, I would strongly recommend not to use the legacy client WindowsAzure.ServiceBus which you're using. Instead, prefer the new Microsoft.Azure.ServiceBus client.
OnMessage API is an asynchronous process that receives messages in an event-driven message pump. It doesn't stop receiving until you either dispose the client or the code that is running it is terminated. The code above is wrong. You should not instantiate a subscription client in a tight loop and register your callback each time. What you should be doing is creating your client, registering a callback with a desired concurrency, and hold on to that client until you no longer need to receive messages.
Remember, it's a message pump that has to run all the time. Official documentation is a bit dry, perhaps this post will help.
In addition to that, I would strongly recommend not to use the legacy client WindowsAzure.ServiceBus which you're using. Instead, prefer the new Microsoft.Azure.ServiceBus client.
answered Nov 23 '18 at 17:29
Sean Feldman
8,91422848
8,91422848
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444185%2fmicrosoft-service-bus-receive-messages-from-bus-with-onmessage-method%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
...does not seem to be working...
- this information is not really helpful. It does not say what exactly did not work, i.e. did you get any error messages anywhere, did you try to debug the issue to make sure that it actually does the intended thing.– JohnB
Nov 23 '18 at 9:54
So you started a new thread, and that thread creates unlimited number of instances of
SubscriptionClient
, as fast as it can, and subscribes each of theSubscriptionClient
s toOnMessage
. Don't you run out of memory right after starting this?– GSerg
Nov 23 '18 at 9:56
@JohnB ,the OnMessage() method is not calling automatically whenever there is a message in Subscriptionclient
– Harika
Nov 23 '18 at 10:03
@GSerg,Presently it's working fine.But I know that this is not the correct way to achieve this and It will cause Memory out of range issues .That's why I'm trying to find a solution without repeating that loop.I've added that loop to check whether it's working or not.Can you please help me
– Harika
Nov 23 '18 at 10:24