Reputation: 2031
I am using Webjob SDK for serving an azure service bus.
It works fine and now I would like to start to parameterize the inputs to it.
I have been doing:
public static void ProcessTopicStatusMessage([ServiceBusTrigger("%topic%", "%subname%")] BrokeredMessage message,
TextWriter logger)
Now i would also like to add an additional parameter: endpoint. Something like this:
public static void ProcessTopicStatusMessage([ServiceBusTrigger("%topic%", "%sub%")] BrokeredMessage message, Uri endPoint,
TextWriter logger)
I figured that it would be possible by binding the URI type to the config like this:
config.BindingFactory.BindToInput<ServiceBusAttribute,Uri>(typeof(Uri), new Uri(myURI));
But it doesn't seem to work. Is this even possible or do I have to make my own custom trigger?
Upvotes: 1
Views: 195
Reputation: 29746
As you mention, you can declare a static field to store you uri or you can inject it using a DI container. The JobHostConfiguration
has a JobActivator
that allows you to use DI.
Here I am using SimpleInjector(nuget) to create a custom job activator.
public class SimpleInjectorJobActivator : IJobActivator
{
private readonly Container _container;
public SimpleInjectorJobActivator(Container container)
{
_container = container;
}
public T CreateInstance<T>()
{
return (T)_container.GetInstance(typeof(T));
}
}
Now you need to create a class that will encapsulate your function trigger:
public class TopicMessageProcessor
{
private readonly Uri _myUry;
public TopicMessageProcessor(Uri myUry)
{
_myUry = myUry;
}
public void ProcessTopicStatusMessage([ServiceBusTrigger("%topic%", "%sub%")] BrokeredMessage message,
TextWriter logger)
{
// _myUry is accessible here
}
}
In you main
function, you need to configure the DI container:
static void Main()
{
...
// get the uri from the config database
Uri myUry = ...
var container = new Container();
container.Register(() => new TopicMessageProcessor(myUry));
container.Verify();
var config = new JobHostConfiguration
{
JobActivator = new SimpleInjectorJobActivator(container)
};
...
}
Just notice that this is a very simple example. Let me know if it helps.
Upvotes: 1