Skip to content

Commit 42cf63d

Browse files
committed
EventHub support in scripting.
This uses an updated EventHub support from the WebJobs.ServiceBus package. Includes samples an an end-2-end test for Node. Must set these env variables to run the - AzureWebJobsEventHubSender - AzureWebJobsEventHubReceiver - AzureWebJobsEventHubPath
1 parent 10e57bd commit 42cf63d

33 files changed

+471
-39
lines changed

WebJobs.Script.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ManualTrigger", "ManualTrig
130130
sample\ManualTrigger\index.js = sample\ManualTrigger\index.js
131131
EndProjectSection
132132
EndProject
133+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "EventHubTrigger", "EventHubTrigger", "{B80C88CF-68EE-4400-A68D-216F1F85179C}"
134+
ProjectSection(SolutionItems) = preProject
135+
function.json = function.json
136+
index.js = index.js
137+
EndProjectSection
138+
EndProject
133139
Global
134140
GlobalSection(SolutionConfigurationPlatforms) = preSolution
135141
Debug|Any CPU = Debug|Any CPU
@@ -183,5 +189,6 @@ Global
183189
{08F97FA9-3D24-4C3D-AE6B-18C5132549AB} = {FF9C0818-30D3-437A-A62D-7A61CA44F459}
184190
{5A089F10-FC0E-4FA6-AC5C-1A76FC468359} = {FF9C0818-30D3-437A-A62D-7A61CA44F459}
185191
{844FBB84-D62F-49D4-A28B-BB900DA3C32B} = {FF9C0818-30D3-437A-A62D-7A61CA44F459}
192+
{B80C88CF-68EE-4400-A68D-216F1F85179C} = {FF9C0818-30D3-437A-A62D-7A61CA44F459}
186193
EndGlobalSection
187194
EndGlobal

function.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"bindings": {
3+
"input": [
4+
{
5+
"type": "eventHubTrigger",
6+
"connectionString": "%AzureWebJobsEventHubReceiver%",
7+
"path": "%AzureWebJobsEventHubPath%"
8+
}
9+
],
10+
11+
"output": [
12+
{
13+
"type": "eventHub",
14+
"name" : "output",
15+
"connectionString": "%AzureWebJobsEventHubSender%",
16+
"path": "%AzureWebJobsEventHubPath%"
17+
}
18+
]
19+
}
20+
}

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = function (input, context) {
2+
context.log("Node.js script processed queue message '" + input.prop1 + "'");
3+
input.val1++;
4+
5+
input.prop1 = "third";
6+
7+
context.done(null, input);
8+
}

src/WebJobs.Script.Host/App.config

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
<bindingRedirect oldVersion="0.0.0.0-4.0.20511.1437" newVersion="4.0.20511.1437" />
1616
</dependentAssembly>
1717
<dependentAssembly>
18-
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
19-
<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
18+
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
19+
<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
2020
</dependentAssembly>
2121
<dependentAssembly>
2222
<assemblyIdentity name="FSharp.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
@@ -30,6 +30,10 @@
3030
<assemblyIdentity name="Microsoft.Azure.WebJobs" publicKeyToken="31bf3856ad364e35" culture="neutral" />
3131
<bindingRedirect oldVersion="0.0.0.0-1.1.2.0" newVersion="1.1.2.0" />
3232
</dependentAssembly>
33+
<dependentAssembly>
34+
<assemblyIdentity name="Microsoft.WindowsAzure.Storage" publicKeyToken="31bf3856ad364e35" culture="neutral" />
35+
<bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.3.0.0" />
36+
</dependentAssembly>
3337
</assemblyBinding>
3438
</runtime>
35-
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" /></startup></configuration>
39+
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" /></startup></configuration>

src/WebJobs.Script.Host/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ static void Main(string[] args)
2424

2525
ScriptHostManager scriptHostManager = new ScriptHostManager(config);
2626
scriptHostManager.RunAndBlock();
27-
}
27+
}
2828
}
2929
}

src/WebJobs.Script.Host/WebJobs.Script.Host.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@
9797
<HintPath>..\..\packages\WindowsAzure.ServiceBus.2.7.6\lib\net40-full\Microsoft.ServiceBus.dll</HintPath>
9898
<Private>True</Private>
9999
</Reference>
100+
<Reference Include="Microsoft.ServiceBus.Messaging.EventProcessorHost, Version=0.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
101+
<HintPath>..\..\packages\Microsoft.Azure.ServiceBus.EventProcessorHost.1.4.0\lib\net45-full\Microsoft.ServiceBus.Messaging.EventProcessorHost.dll</HintPath>
102+
<Private>True</Private>
103+
</Reference>
100104
<Reference Include="Microsoft.WindowsAzure.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
101105
<HintPath>..\..\packages\Microsoft.WindowsAzure.ConfigurationManager.2.0.3\lib\net40\Microsoft.WindowsAzure.Configuration.dll</HintPath>
102106
<Private>True</Private>

src/WebJobs.Script.Host/packages.config

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
77
<package id="Microsoft.AspNet.WebHooks.Common" version="1.2.0-beta4" targetFramework="net45" />
88
<package id="Microsoft.AspNet.WebHooks.Receivers" version="1.2.0-beta4" targetFramework="net45" />
9-
<package id="Microsoft.Azure.WebJobs" version="1.1.2-alpha-10251" targetFramework="net46" />
10-
<package id="Microsoft.Azure.WebJobs.Core" version="1.1.2-alpha-10251" targetFramework="net46" />
11-
<package id="Microsoft.Azure.WebJobs.Extensions" version="1.0.2-alpha-10232" targetFramework="net46" />
12-
<package id="Microsoft.Azure.WebJobs.Extensions.SendGrid" version="1.0.2-alpha-10232" targetFramework="net46" />
13-
<package id="Microsoft.Azure.WebJobs.ServiceBus" version="1.1.2-alpha-10251" targetFramework="net46" />
9+
<package id="Microsoft.Azure.WebJobs" version="1.1.2-alpha-10251" targetFramework="net46" />
10+
<package id="Microsoft.Azure.WebJobs.Core" version="1.1.2-alpha-10251" targetFramework="net46" />
11+
<package id="Microsoft.Azure.WebJobs.Extensions" version="1.0.2-alpha-10232" targetFramework="net46" />
12+
<package id="Microsoft.Azure.WebJobs.Extensions.SendGrid" version="1.0.2-alpha-10232" targetFramework="net46" />
13+
<package id="Microsoft.Azure.WebJobs.ServiceBus" version="1.1.2-alpha-10251" targetFramework="net46" />
14+
<package id="Microsoft.Azure.ServiceBus.EventProcessorHost" version="1.4.0" targetFramework="net46" />
1415
<package id="Microsoft.Data.Edm" version="5.6.2" targetFramework="net45" />
1516
<package id="Microsoft.Data.OData" version="5.6.2" targetFramework="net45" />
1617
<package id="Microsoft.Data.Services.Client" version="5.6.2" targetFramework="net45" />

src/WebJobs.Script.WebHost/Web.config

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
4141
</dependentAssembly>
4242
<dependentAssembly>
43-
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
44-
<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
43+
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
44+
<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
4545
</dependentAssembly>
4646
<dependentAssembly>
4747
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
@@ -63,6 +63,10 @@
6363
<assemblyIdentity name="Microsoft.Azure.WebJobs" publicKeyToken="31bf3856ad364e35" culture="neutral" />
6464
<bindingRedirect oldVersion="0.0.0.0-1.1.2.0" newVersion="1.1.2.0" />
6565
</dependentAssembly>
66+
<dependentAssembly>
67+
<assemblyIdentity name="Microsoft.WindowsAzure.Storage" publicKeyToken="31bf3856ad364e35" culture="neutral" />
68+
<bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.3.0.0" />
69+
</dependentAssembly>
6670
</assemblyBinding>
6771
</runtime>
6872
<system.codedom>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
}

src/WebJobs.Script/Binding/FunctionBinding.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ protected FunctionBinding(ScriptHostConfiguration config, string name, string ty
3737

3838
public abstract Task BindAsync(BindingContext context);
3939

40+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "EventHub")]
4041
internal static Collection<FunctionBinding> GetBindings(ScriptHostConfiguration config, IEnumerable<BindingMetadata> bindingMetadatas, FileAccess fileAccess)
4142
{
4243
Collection<FunctionBinding> bindings = new Collection<FunctionBinding>();
@@ -55,6 +56,16 @@ internal static Collection<FunctionBinding> GetBindings(ScriptHostConfiguration
5556
BlobBindingMetadata blobBindingMetadata = (BlobBindingMetadata)bindingMetadata;
5657
bindings.Add(new BlobBinding(config, name, blobBindingMetadata.Path, fileAccess, bindingMetadata.IsTrigger));
5758
break;
59+
case BindingType.EventHub:
60+
case BindingType.EventHubTrigger:
61+
EventHubBindingMetadata eventHubBindingMetadata = (EventHubBindingMetadata)bindingMetadata;
62+
if (!eventHubBindingMetadata.IsTrigger &&
63+
fileAccess != FileAccess.Write)
64+
{
65+
throw new InvalidOperationException("EventHub binding can only be used for output.");
66+
}
67+
bindings.Add(new EventHubBinding(config, name, eventHubBindingMetadata.Path, fileAccess, bindingMetadata.IsTrigger));
68+
break;
5869
case BindingType.Queue:
5970
case BindingType.QueueTrigger:
6071
QueueBindingMetadata queueBindingMetadata = (QueueBindingMetadata)bindingMetadata;

0 commit comments

Comments
 (0)