简体   繁体   中英

How do I specify a particular endpoint with a topic/subscription in Azure ServiceBusTriggerAttribute?

I have created a service bus topic ( my-topic ) in my Azure account. I have created a listen only rule ( listen_rule ) on that topic, and also a subscription to that topic ( my-subscription ), for my code to trigger on.

How do I add that specific subscription/rule to the [ServiceBusTrigger] ?

In app.config I have this configuration section:

<add key="MySubscription" value="my-subscription" />
<add key="MyTopic" value="Endpoint=sb://XXXXXXX.servicebus.windows.net/;SharedAccessKeyName=listen_rule;SharedAccessKey=XXXX=;EntityPath=my-topic"/>

In my code I have:

    public async Task ProcessMyMessageAsync(
        [ServiceBusTrigger("%MyTopic%", "%MySubscription%")]
        BrokeredMessage brokeredMessage)

This throws an exception:

The entity path/name 'Endpoint=sb://XXXX.servicebus.windows.net/;SharedAccessKeyName=listen_rule;SharedAccessKey=XXXX=;EntityPath=topic' exceeds the '50' character limit.

I've found lots of examples on the web where there is a generic AzureWebJobsServiceBus connection string, but that gives access to the whole service bus, I want to lock it down to this particular topic/rule.

ps I'm using .NET framework 4.5.2 (yeah, I know, it's an old application), and Microsoft.Azure.WebJobs.ServiceBus 2.0.0.

pps Ideally, I'd like to specify the whole subscription in one configuration line if possible.

Try this, it takes your topic name , subscription name and servicebus connection string as parameters,

public async Task ProcessMyMessageAsync([ServiceBusTrigger("%MyTopic%","%MySubscription%", Connection = "ServiceBusEndPoint")]BrokeredMessage brokeredMessage)

and in your appsettings

<add key="MySubscription" value="my-subscription" /> <add key="MyTopic" value="my-topic"/> <Add Key="ServiceBusEndPoint" value="Endpoint=sb://XXXXXXX.servicebus.windows.net/;SharedAccessKeyName=listen_rule;SharedAccessKey=XXXX="/>

If this doesnt work try adding the servicebusendpoint in connection string instead of app setting that should work.

update for Microsoft.Azure.WebJobs.ServiceBus 2.0.0.

use can setup the service bus connection string and storage connection string over here in program.cs

`

    static void Main()
    {
        var config = new JobHostConfiguration("your storage connection string ");

        if (config.IsDevelopment)
        {
            config.UseDevelopmentSettings();

        }
        config.UseServiceBus(new ServiceBusConfiguration() { ConnectionString = "your servcebus connection string " });
        var host = new JobHost(config);
        // The following code ensures that the WebJob will be running continuously
        host.RunAndBlock();
    }
`

// Please set the following connection strings in app.config for this WebJob to run: // AzureWebJobsDashboard and AzureWebJobsStorage

There are two ways to get service bus trigger, one is webjob and one is azure function. I check the Microsoft.Azure.WebJobs.ServiceBus 2.0.0 package looks like this trigger doesn't support the connection property.

Mostly I suppose you want to use the webjob sdk. So below is my environment.

  • Microsoft.Azure.WebJobs 2.0.0
  • Microsoft.Azure.WebJobs.Core 2.0.0
  • Microsoft.Azure.WebJobs.ServiceBus 2.0.0

In the app.config, I set the AzureWebJobsServiceBus connection.

<connectionStrings>

    <add name="AzureWebJobsDashboard" connectionString="" />
    <add name="AzureWebJobsStorage" connectionString="" />
    <add name="AzureWebJobsServiceBus" connectionString="service bus connection string"/>
  </connectionStrings>

Then below is my Program.cs .

using Microsoft.Azure.WebJobs;

namespace WebJob1
{

    class Program
    {

        static void Main()
        {
            var config = new JobHostConfiguration();
            config.UseServiceBus();

            if (config.IsDevelopment)
            {
                config.UseDevelopmentSettings();

            }

            var host = new JobHost(config);
            // The following code ensures that the WebJob will be running continuously
            host.RunAndBlock();
        }
    }
}

And the below is my Function.cs . I set the connection with the ServiceBusAccount attribute. I found it here: Attributes and annotations .

using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.ServiceBus.Messaging;

namespace WebJob1
{
    public class Functions
    {

        [ServiceBusAccount("AzureWebJobsServiceBus")]
        public static void ProcessQueueMessage([ServiceBusTrigger(topicName:"testtopic",subscriptionName:"testsub", access: AccessRights.Manage)] string message, TextWriter log)
        {
            log.WriteLine(message);
        }
    }
}

And here is my test screenshot.

在此处输入图片说明

And if you want is azure function you could refer to the official tutorial: Create your first function using Visual Studio and this service bus binding (other pages have the trigger information)

Hope this could help you, if you still have other problem please feel free to let me know.

I'm going to answer this myself, incase anyone else found this issue:

I was missing a connection string:

<connectionStrings>
  <add name="AzureWebJobsMyEndPoint" connectionString="Endpoint=sb://XXXX.servicebus.windows.net/;SharedAccessKeyName=listen_rule;SharedAccessKey=XXXX"/>
</connectionStrings>
<appSettings>
  <add key="MySubscription" value="subscription-name"/>
  <add key="MyTopic" value="topic-name"/>
</appSetting>

And I needed ServiceBusAccountAttribute on the trigger:

public async Task ProcessMyMessageAsync(
    [ServiceBusAccount("MyEndPoint")]
    [ServiceBusTrigger("%MyTopic%", "%MySubscription%", AccessRights.Listen)]
    BrokeredMessage brokeredMessage)

Notice how the ServiceBusAccount used MyEndPoint , but the connection string uses that as a suffix to AzureWebJobs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM