|
| 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 Microsoft.Azure.WebJobs.Host.Bindings.Path; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace Microsoft.Azure.WebJobs.Script.Binding |
| 10 | +{ |
| 11 | + public class EventHubBinding : FunctionBinding |
| 12 | + { |
| 13 | + private readonly BindingTemplate _eventHubNameBindingTemplate; |
| 14 | + |
| 15 | + public EventHubBinding(ScriptHostConfiguration config, string name, string eventHubName, FileAccess access, bool isTrigger) : |
| 16 | + base(config, name, "eventhub", access, isTrigger) |
| 17 | + { |
| 18 | + EventHubName = eventHubName; |
| 19 | + _eventHubNameBindingTemplate = BindingTemplate.FromString(EventHubName); |
| 20 | + } |
| 21 | + |
| 22 | + public string EventHubName { get; private set; } |
| 23 | + |
| 24 | + public override bool HasBindingParameters |
| 25 | + { |
| 26 | + get |
| 27 | + { |
| 28 | + return _eventHubNameBindingTemplate.ParameterNames.Any(); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public override async Task BindAsync(BindingContext context) |
| 33 | + { |
| 34 | + string eventHubName = this.EventHubName; |
| 35 | + if (context.BindingData != null) |
| 36 | + { |
| 37 | + eventHubName = _eventHubNameBindingTemplate.Bind(context.BindingData); |
| 38 | + } |
| 39 | + |
| 40 | + eventHubName = Resolve(eventHubName); |
| 41 | + |
| 42 | + // only an output binding is supported |
| 43 | + IAsyncCollector<byte[]> collector = context.Binder.Bind<IAsyncCollector<byte[]>>(new ServiceBus.EventHubAttribute(eventHubName)); |
| 44 | + byte[] bytes; |
| 45 | + using (MemoryStream ms = new MemoryStream()) |
| 46 | + { |
| 47 | + context.Value.CopyTo(ms); |
| 48 | + bytes = ms.ToArray(); |
| 49 | + } |
| 50 | + await collector.AddAsync(bytes); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments