Microsoft service bus - Receive messages from Bus with OnMessage() method












0














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.










share|improve this question
























  • ...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 SubscriptionClients 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










  • @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


















0














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.










share|improve this question
























  • ...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 SubscriptionClients 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










  • @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
















0












0








0







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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 of SubscriptionClient, as fast as it can, and subscribes each of the SubscriptionClients 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










  • @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










  • 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 SubscriptionClients 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










  • @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 SubscriptionClients 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 SubscriptionClients 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














1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer





















    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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.






    share|improve this answer


























      0














      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.






      share|improve this answer
























        0












        0








        0






        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 17:29









        Sean Feldman

        8,91422848




        8,91422848






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            How to ignore python UserWarning in pytest?

            Alexandru Averescu