|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.Azure.WebJobs.Host.Bindings.Path; |
| 9 | + |
| 10 | +namespace Microsoft.Azure.WebJobs.Script |
| 11 | +{ |
| 12 | + internal class ServiceBusBinding : Binding |
| 13 | + { |
| 14 | + private readonly BindingTemplate _queueOrTopicNameBindingTemplate; |
| 15 | + |
| 16 | + public ServiceBusBinding(JobHostConfiguration config, string name, string queueOrTopicName, FileAccess fileAccess, bool isTrigger) : base(config, name, "serviceBus", fileAccess, isTrigger) |
| 17 | + { |
| 18 | + QueueOrTopicName = queueOrTopicName; |
| 19 | + _queueOrTopicNameBindingTemplate = BindingTemplate.FromString(QueueOrTopicName); |
| 20 | + } |
| 21 | + |
| 22 | + public string QueueOrTopicName { get; private set; } |
| 23 | + |
| 24 | + public override bool HasBindingParameters |
| 25 | + { |
| 26 | + get |
| 27 | + { |
| 28 | + return _queueOrTopicNameBindingTemplate.ParameterNames.Any(); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public override async Task BindAsync(IBinder binder, Stream stream, IReadOnlyDictionary<string, string> bindingData) |
| 33 | + { |
| 34 | + string boundQueueName = QueueOrTopicName; |
| 35 | + if (bindingData != null) |
| 36 | + { |
| 37 | + boundQueueName = _queueOrTopicNameBindingTemplate.Bind(bindingData); |
| 38 | + } |
| 39 | + |
| 40 | + boundQueueName = Resolve(boundQueueName); |
| 41 | + |
| 42 | + // only an output binding is supported |
| 43 | + using (StreamReader reader = new StreamReader(stream)) |
| 44 | + { |
| 45 | + // TODO: only string supported currently - need to support other types |
| 46 | + IAsyncCollector<string> collector = binder.Bind<IAsyncCollector<string>>(new ServiceBusAttribute(boundQueueName)); |
| 47 | + string data = reader.ReadToEnd(); |
| 48 | + await collector.AddAsync(data); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments