Skip to content

Commit e0a783e

Browse files
committed
Adding ServiceBus output binding support + sample
1 parent d5985ad commit e0a783e

File tree

16 files changed

+133
-23
lines changed

16 files changed

+133
-23
lines changed

WebJobs.Script.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{446135
9999
.nuget\packages.config = .nuget\packages.config
100100
EndProjectSection
101101
EndProject
102+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ServiceBusQueueTrigger", "ServiceBusQueueTrigger", "{5C89A41E-D179-4D2F-B66E-0F77BF1DE53E}"
103+
ProjectSection(SolutionItems) = preProject
104+
sample\ServiceBusQueueTrigger\function.json = sample\ServiceBusQueueTrigger\function.json
105+
sample\ServiceBusQueueTrigger\index.js = sample\ServiceBusQueueTrigger\index.js
106+
EndProjectSection
107+
EndProject
102108
Global
103109
GlobalSection(SolutionConfigurationPlatforms) = preSolution
104110
Debug|Any CPU = Debug|Any CPU
@@ -142,5 +148,6 @@ Global
142148
{644AFFC7-46C5-483A-812E-F5C9B93CBA86} = {FF9C0818-30D3-437A-A62D-7A61CA44F459}
143149
{1B6E0EA1-DB38-431C-864A-2A553DACB8DA} = {FF9C0818-30D3-437A-A62D-7A61CA44F459}
144150
{5C458CC0-F3EC-463E-9091-2B08955C4375} = {1B6E0EA1-DB38-431C-864A-2A553DACB8DA}
151+
{5C89A41E-D179-4D2F-B66E-0F77BF1DE53E} = {FF9C0818-30D3-437A-A62D-7A61CA44F459}
145152
EndGlobalSection
146153
EndGlobal

sample/QueueTrigger/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module.exports = function (context) {
55

66
context.output({
77
receipt: JSON.stringify(context.workItem)
8-
})
8+
});
99

1010
context.done();
1111
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"bindings": {
3+
"input": [
4+
{
5+
"type": "serviceBusTrigger",
6+
"name": "message",
7+
"queueName": "samples-input"
8+
}
9+
],
10+
"output": [
11+
{
12+
"type": "serviceBus",
13+
"name": "message",
14+
"queueName": "samples-input"
15+
}
16+
]
17+
}
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = function (context) {
2+
var message = context.message;
3+
context.log("Node.js ServiceBus queue trigger function processed message '" + JSON.stringify(message) + "'");
4+
5+
if (message.count < 1)
6+
{
7+
// write a message back to the queue that this function is triggered on
8+
// ensuring that we only loop on this once
9+
message.count += 1;
10+
context.output({
11+
message: JSON.stringify(message)
12+
});
13+
}
14+
15+
context.done();
16+
}

sample/TimerTrigger/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = function (context) {
1212
};
1313
context.output({
1414
message: JSON.stringify(message)
15-
})
15+
});
1616

1717
logger.log(timeStamp, context.done);
1818
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
<Private>True</Private>
6868
</Reference>
6969
<Reference Include="Microsoft.Azure.WebJobs, Version=1.1.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
70-
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.Core.1.1.1-alpha-10195\lib\net45\Microsoft.Azure.WebJobs.dll</HintPath>
70+
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.Core.1.1.1-alpha-10200\lib\net45\Microsoft.Azure.WebJobs.dll</HintPath>
7171
<Private>True</Private>
7272
</Reference>
7373
<Reference Include="Microsoft.Azure.WebJobs.Extensions, Version=1.0.1.0, Culture=neutral, processorArchitecture=MSIL">
@@ -83,11 +83,11 @@
8383
<Private>True</Private>
8484
</Reference>
8585
<Reference Include="Microsoft.Azure.WebJobs.Host, Version=1.1.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
86-
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.1.1.1-alpha-10195\lib\net45\Microsoft.Azure.WebJobs.Host.dll</HintPath>
86+
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.1.1.1-alpha-10200\lib\net45\Microsoft.Azure.WebJobs.Host.dll</HintPath>
8787
<Private>True</Private>
8888
</Reference>
8989
<Reference Include="Microsoft.Azure.WebJobs.ServiceBus, Version=1.1.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
90-
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.ServiceBus.1.1.1-alpha-10195\lib\net45\Microsoft.Azure.WebJobs.ServiceBus.dll</HintPath>
90+
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.ServiceBus.1.1.1-alpha-10200\lib\net45\Microsoft.Azure.WebJobs.ServiceBus.dll</HintPath>
9191
<Private>True</Private>
9292
</Reference>
9393
<Reference Include="Microsoft.Data.Edm, Version=5.6.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">

src/WebJobs.Script.Host/packages.config

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
99
<package id="Microsoft.AspNet.WebHooks.Common" version="1.2.0-beta4" targetFramework="net45" />
1010
<package id="Microsoft.AspNet.WebHooks.Receivers" version="1.2.0-beta4" targetFramework="net45" />
11-
<package id="Microsoft.Azure.WebJobs" version="1.1.1-alpha-10195" targetFramework="net45" />
12-
<package id="Microsoft.Azure.WebJobs.Core" version="1.1.1-alpha-10195" targetFramework="net45" />
11+
<package id="Microsoft.Azure.WebJobs" version="1.1.1-alpha-10200" targetFramework="net45" />
12+
<package id="Microsoft.Azure.WebJobs.Core" version="1.1.1-alpha-10200" targetFramework="net45" />
1313
<package id="Microsoft.Azure.WebJobs.Extensions" version="1.0.1-alpha-10210" targetFramework="net45" />
1414
<package id="Microsoft.Azure.WebJobs.Extensions.SendGrid" version="1.0.1-alpha-10210" targetFramework="net45" />
1515
<package id="Microsoft.Azure.WebJobs.Extensions.WebHooks" version="1.0.0-beta3-10210" targetFramework="net45" />
16-
<package id="Microsoft.Azure.WebJobs.ServiceBus" version="1.1.1-alpha-10195" targetFramework="net45" />
16+
<package id="Microsoft.Azure.WebJobs.ServiceBus" version="1.1.1-alpha-10200" targetFramework="net45" />
1717
<package id="Microsoft.Data.Edm" version="5.6.2" targetFramework="net45" />
1818
<package id="Microsoft.Data.OData" version="5.6.2" targetFramework="net45" />
1919
<package id="Microsoft.Data.Services.Client" version="5.6.2" targetFramework="net45" />

src/WebJobs.Script/Binding/Binding.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ internal static Collection<Binding> GetBindings(JobHostConfiguration config, JAr
6767
string queueName = (string)binding["queueName"];
6868
bindings.Add(new QueueBinding(config, name, queueName, fileAccess, isTrigger: false));
6969
}
70+
else if (type == "serviceBus")
71+
{
72+
if (fileAccess != FileAccess.Write)
73+
{
74+
throw new InvalidOperationException("ServiceBus binding can only be used for output.");
75+
}
76+
string queueOrTopicName = (string)(binding["queueName"] ?? binding["topicName"]);
77+
bindings.Add(new ServiceBusBinding(config, name, queueOrTopicName, fileAccess, isTrigger: false));
78+
}
7079
else if (type == "queueTrigger")
7180
{
7281
string queueName = (string)binding["queueName"];
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
}

src/WebJobs.Script/Description/FunctionDescriptorProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ protected ParameterDescriptor ParseServiceBusTrigger(JObject trigger, Type trigg
118118
}
119119
else
120120
{
121-
throw new InvalidOperationException("Invalid servicebus trigger configuration.");
121+
throw new InvalidOperationException("Invalid ServiceBus trigger configuration.");
122122
}
123123

124124
string parameterName = (string)trigger["name"];

0 commit comments

Comments
 (0)