Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support to provide Pubsub (and Topic) during runtime at startup for controller methods.

See original GitHub issue

Describe the proposal

Pubsub name might not be known at compile time, so it would be reasonable to have an approach to specify it during runtime at startup. It’s currently possible if mapping endpoints directly (, but not possible for methods within a controller.

My proposal is to make it possible to resolve Pubsub (and Topic) from an environment variable when specified in the TopicAttribute. This is e.g. possible in Azure Functions with most of the attributes there. This would allow us to use the attribute as follows:

[Topic("%MyPubSub%", "%MyTopic%")]
public async Task<IActionResult> OnEvent([FromBody] Event data)

%MyPubSub% and %MyTopic% can then be resolved from an environment variable using e.g. Environment.ExpandEnvironmentVariables, or using a more sophisticated approach as Azure Functions SDK (see NameResolverExtensions.cs).

Another approach would be to add an overload to MapSubscribeHandler to somehow override the values specified in TopicAttribute. Don’t know what the best approach would be here yet… Currently, to workaround this, I’ve just created my own MapSubscribeHandler which takes a string parameter for overriding the PubSub name.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

rynowakcommented, May 18, 2021

One obvious way is to implement a custom attribute (which implements the ITopicMetadata interface) and handle the logic there.

Yes. This is the pretty simple case. You get to write custom code to compute the topic name based on whatever inputs you like, as long as they are available from statics + attribute constructor.

These are useful because you can build your own conventions, or consider extra data sources like env-vars.

Are there other ways of adding metadata to endpoints within a controller? A quick google didn’t yield much info about this topic.

Yes, you can do this via application model and write your own code to configure the model.

The reason I like the attribute-based approach is that it’s pretty easy to understand. Technically it doesn’t enable anything that new that’s impossible today.

Application model on the other hand is somewhat involved to understand and work with.

andreatosatocommented, Jun 3, 2021

I create my TopicEnvAttribute and pass environment variable to ctor

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Dapr publish & subscribe building block
At startup, the Dapr runtime will call the application on a well-known endpoint to identify and create the required subscriptions:.
Read more >
Writing and responding to Pub/Sub messages
Writing and responding to Pub/Sub messages · On this page · Prerequisites · Cloning the sample app · Create a topic and subscription....
Read more >
Microservices: Pub/Sub with Dapr, docker-compose, and . ...
In the previous article, We discussed service-to-service invocation. ... This controller method is used to publish the message to the topic.
Read more >
Troubleshoot Pub/Sub with the .NET SDK
The most common problem with pub/sub is that the pub/sub endpoint in your application is not being called. There are a few layers...
Read more >
Getting Started | Messaging with Google Cloud Pub/Sub
You will need a topic and a subscription to send and receive messages from Google Cloud Pub/Sub. You can create them in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found