Skip to content

Commit 0130a25

Browse files
authored
Merge pull request #109 from Particular/flatten-types
2 parents a56ac24 + 89ca032 commit 0130a25

File tree

5 files changed

+127
-146
lines changed

5 files changed

+127
-146
lines changed

src/NServiceBus.AzureFunctions.ServiceBus/FunctionEndpoint.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
namespace NServiceBus
22
{
3+
using System;
34
using System.IO;
45
using System.Reflection;
56
using System.Runtime.Loader;
6-
using Logging;
7-
using System;
87
using System.Threading;
98
using System.Threading.Tasks;
109
using AzureFunctions.ServiceBus;
1110
using Extensibility;
11+
using Logging;
1212
using Microsoft.Azure.ServiceBus;
1313
using Microsoft.Extensions.Logging;
1414
using Transport;
@@ -20,11 +20,8 @@
2020
/// </summary>
2121
public class FunctionEndpoint
2222
{
23-
private readonly Func<FunctionExecutionContext, Task<IEndpointInstance>> endpointFactory;
24-
private ServiceBusTriggeredEndpointConfiguration configuration;
25-
2623
/// <summary>
27-
/// Creates a new instance of <see cref="FunctionEndpoint"/> that can handle messages using the provided configuration.
24+
/// Creates a new instance of <see cref="FunctionEndpoint" /> that can handle messages using the provided configuration.
2825
/// </summary>
2926
public FunctionEndpoint(Func<FunctionExecutionContext, ServiceBusTriggeredEndpointConfiguration> configurationFactory)
3027
{
@@ -42,7 +39,7 @@ internal FunctionEndpoint(IStartableEndpointWithExternallyManagedContainer exter
4239
ServiceBusTriggeredEndpointConfiguration configuration, IServiceProvider serviceProvider)
4340
{
4441
this.configuration = configuration;
45-
this.endpointFactory = _ => externallyManagedContainerEndpoint.Start(serviceProvider);
42+
endpointFactory = _ => externallyManagedContainerEndpoint.Start(serviceProvider);
4643
}
4744

4845
/// <summary>
@@ -186,7 +183,10 @@ static bool IsRuntimeAssembly(byte[] publicKeyToken)
186183
protected Func<FunctionExecutionContext, string> AssemblyDirectoryResolver = functionExecutionContext =>
187184
Path.Combine(functionExecutionContext.ExecutionContext.FunctionAppDirectory, "bin");
188185

186+
private readonly Func<FunctionExecutionContext, Task<IEndpointInstance>> endpointFactory;
187+
189188
readonly SemaphoreSlim semaphoreLock = new SemaphoreSlim(initialCount: 1, maxCount: 1);
189+
private ServiceBusTriggeredEndpointConfiguration configuration;
190190

191191
PipelineInvoker pipeline;
192192
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Security.Cryptography;
3+
using System.Text;
4+
5+
namespace NServiceBus.AzureFunctions.ServiceBus
6+
{
7+
static class DeterministicGuid
8+
{
9+
public static Guid Create(string data)
10+
{
11+
// use MD5 hash to get a 16-byte hash of the string
12+
using (var provider = new MD5CryptoServiceProvider())
13+
{
14+
var inputBytes = Encoding.Default.GetBytes(data);
15+
var hashBytes = provider.ComputeHash(inputBytes);
16+
// generate a guid from the hash:
17+
return new Guid(hashBytes);
18+
}
19+
}
20+
}
21+
}

src/NServiceBus.AzureFunctions.ServiceBus/Serverless/ServerlessEndpointConfiguration.cs

Lines changed: 0 additions & 110 deletions
This file was deleted.
Lines changed: 91 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
namespace NServiceBus
22
{
3-
using Logging;
4-
using Microsoft.Azure.WebJobs;
53
using System;
4+
using System.Threading.Tasks;
65
using AzureFunctions.ServiceBus;
6+
using Logging;
7+
using Microsoft.Azure.WebJobs;
8+
using Serialization;
9+
using Transport;
710

811
/// <summary>
912
/// Represents a serverless NServiceBus endpoint running within an AzureServiceBus trigger.
1013
/// </summary>
11-
public class ServiceBusTriggeredEndpointConfiguration : ServerlessEndpointConfiguration
14+
public class ServiceBusTriggeredEndpointConfiguration
1215
{
13-
internal const string DefaultServiceBusConnectionName = "AzureWebJobsServiceBus";
14-
15-
/// <summary>
16-
/// Azure Service Bus transport
17-
/// </summary>
18-
public TransportExtensions<AzureServiceBusTransport> Transport { get; }
19-
2016
static ServiceBusTriggeredEndpointConfiguration()
2117
{
2218
LogManager.UseFactory(FunctionsLoggerFactory.Instance);
@@ -25,11 +21,34 @@ static ServiceBusTriggeredEndpointConfiguration()
2521
/// <summary>
2622
/// Creates a serverless NServiceBus endpoint running within an Azure Service Bus trigger.
2723
/// </summary>
28-
public ServiceBusTriggeredEndpointConfiguration(string endpointName, string connectionStringName = null) : base(endpointName)
24+
public ServiceBusTriggeredEndpointConfiguration(string endpointName, string connectionStringName = null)
2925
{
26+
EndpointConfiguration = new EndpointConfiguration(endpointName);
27+
28+
recoverabilityPolicy.SendFailedMessagesToErrorQueue = true;
29+
EndpointConfiguration.Recoverability().CustomPolicy(recoverabilityPolicy.Invoke);
30+
31+
// Disable diagnostics by default as it will fail to create the diagnostics file in the default path.
32+
// Can be overriden by ServerlessEndpointConfiguration.LogDiagnostics().
33+
EndpointConfiguration.CustomDiagnosticsWriter(_ => Task.CompletedTask);
34+
35+
// 'WEBSITE_SITE_NAME' represents an Azure Function App and the environment variable is set when hosting the function in Azure.
36+
var functionAppName = Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME") ?? Environment.MachineName;
37+
EndpointConfiguration.UniquelyIdentifyRunningInstance()
38+
.UsingCustomDisplayName(functionAppName)
39+
.UsingCustomIdentifier(DeterministicGuid.Create(functionAppName));
40+
41+
// Look for license as an environment variable
42+
var licenseText = Environment.GetEnvironmentVariable("NSERVICEBUS_LICENSE");
43+
if (!string.IsNullOrWhiteSpace(licenseText))
44+
{
45+
EndpointConfiguration.License(licenseText);
46+
}
47+
3048
Transport = UseTransport<AzureServiceBusTransport>();
3149

32-
var connectionString = Environment.GetEnvironmentVariable(connectionStringName ?? DefaultServiceBusConnectionName);
50+
var connectionString =
51+
Environment.GetEnvironmentVariable(connectionStringName ?? DefaultServiceBusConnectionName);
3352
Transport.ConnectionString(connectionString);
3453

3554
var recoverability = AdvancedConfiguration.Recoverability();
@@ -40,7 +59,21 @@ public ServiceBusTriggeredEndpointConfiguration(string endpointName, string conn
4059
}
4160

4261
/// <summary>
43-
/// Attempts to derive the required configuration parameters automatically from the Azure Functions related attributes via reflection.
62+
/// Azure Service Bus transport
63+
/// </summary>
64+
public TransportExtensions<AzureServiceBusTransport> Transport { get; }
65+
66+
internal EndpointConfiguration EndpointConfiguration { get; }
67+
internal PipelineInvoker PipelineInvoker { get; private set; }
68+
69+
/// <summary>
70+
/// Gives access to the underlying endpoint configuration for advanced configuration options.
71+
/// </summary>
72+
public EndpointConfiguration AdvancedConfiguration => EndpointConfiguration;
73+
74+
/// <summary>
75+
/// Attempts to derive the required configuration parameters automatically from the Azure Functions related attributes via
76+
/// reflection.
4477
/// </summary>
4578
public static ServiceBusTriggeredEndpointConfiguration FromAttributes()
4679
{
@@ -50,7 +83,51 @@ public static ServiceBusTriggeredEndpointConfiguration FromAttributes()
5083
return new ServiceBusTriggeredEndpointConfiguration(configuration.QueueName, configuration.Connection);
5184
}
5285

53-
throw new Exception($"Unable to automatically derive the endpoint name from the ServiceBusTrigger attribute. Make sure the attribute exists or create the {nameof(ServiceBusTriggeredEndpointConfiguration)} with the required parameter manually.");
86+
throw new Exception(
87+
$"Unable to automatically derive the endpoint name from the ServiceBusTrigger attribute. Make sure the attribute exists or create the {nameof(ServiceBusTriggeredEndpointConfiguration)} with the required parameter manually.");
88+
}
89+
90+
/// <summary>
91+
/// Define a transport to be used when sending and publishing messages.
92+
/// </summary>
93+
protected TransportExtensions<TTransport> UseTransport<TTransport>()
94+
where TTransport : TransportDefinition, new()
95+
{
96+
var serverlessTransport = EndpointConfiguration.UseTransport<ServerlessTransport<TTransport>>();
97+
98+
PipelineInvoker = serverlessTransport.PipelineAccess();
99+
return serverlessTransport.BaseTransportConfiguration();
54100
}
101+
102+
/// <summary>
103+
/// Define the serializer to be used.
104+
/// </summary>
105+
public SerializationExtensions<T> UseSerialization<T>() where T : SerializationDefinition, new()
106+
{
107+
return EndpointConfiguration.UseSerialization<T>();
108+
}
109+
110+
/// <summary>
111+
/// Disables moving messages to the error queue even if an error queue name is configured.
112+
/// </summary>
113+
public void DoNotSendMessagesToErrorQueue()
114+
{
115+
recoverabilityPolicy.SendFailedMessagesToErrorQueue = false;
116+
}
117+
118+
/// <summary>
119+
/// Logs endpoint diagnostics information to the log. Diagnostics are logged on level <see cref="LogLevel.Info" />.
120+
/// </summary>
121+
public void LogDiagnostics()
122+
{
123+
EndpointConfiguration.CustomDiagnosticsWriter(diagnostics =>
124+
{
125+
LogManager.GetLogger("StartupDiagnostics").Info(diagnostics);
126+
return Task.CompletedTask;
127+
});
128+
}
129+
130+
private readonly ServerlessRecoverabilityPolicy recoverabilityPolicy = new ServerlessRecoverabilityPolicy();
131+
internal const string DefaultServiceBusConnectionName = "AzureWebJobsServiceBus";
55132
}
56133
}
Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,4 @@
11
[assembly: System.Runtime.CompilerServices.InternalsVisibleToAttribute(@"ServiceBus.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001007f16e21368ff041183fab592d9e8ed37e7be355e93323147a1d29983d6e591b04282e4da0c9e18bd901e112c0033925eb7d7872c2f1706655891c5c9d57297994f707d16ee9a8f40d978f064ee1ffc73c0db3f4712691b23bf596f75130f4ec978cf78757ec034625a5f27e6bb50c618931ea49f6f628fd74271c32959efb1c5")]
2-
namespace NServiceBus.AzureFunctions.ServiceBus
3-
{
4-
public abstract class ServerlessEndpointConfiguration
5-
{
6-
protected ServerlessEndpointConfiguration(string endpointName) { }
7-
public NServiceBus.EndpointConfiguration AdvancedConfiguration { get; }
8-
public void DoNotSendMessagesToErrorQueue() { }
9-
public void LogDiagnostics() { }
10-
public NServiceBus.Serialization.SerializationExtensions<T> UseSerialization<T>()
11-
where T : NServiceBus.Serialization.SerializationDefinition, new () { }
12-
protected NServiceBus.TransportExtensions<TTransport> UseTransport<TTransport>()
13-
where TTransport : NServiceBus.Transport.TransportDefinition, new () { }
14-
}
15-
}
162
namespace NServiceBus
173
{
184
public class FunctionEndpoint
@@ -31,10 +17,17 @@ namespace NServiceBus
3117
{
3218
public static void UseNServiceBus(this Microsoft.Azure.Functions.Extensions.DependencyInjection.IFunctionsHostBuilder functionsHostBuilder, System.Func<NServiceBus.ServiceBusTriggeredEndpointConfiguration> configurationFactory) { }
3319
}
34-
public class ServiceBusTriggeredEndpointConfiguration : NServiceBus.AzureFunctions.ServiceBus.ServerlessEndpointConfiguration
20+
public class ServiceBusTriggeredEndpointConfiguration
3521
{
3622
public ServiceBusTriggeredEndpointConfiguration(string endpointName, string connectionStringName = null) { }
23+
public NServiceBus.EndpointConfiguration AdvancedConfiguration { get; }
3724
public NServiceBus.TransportExtensions<NServiceBus.AzureServiceBusTransport> Transport { get; }
25+
public void DoNotSendMessagesToErrorQueue() { }
3826
public static NServiceBus.ServiceBusTriggeredEndpointConfiguration FromAttributes() { }
27+
public void LogDiagnostics() { }
28+
public NServiceBus.Serialization.SerializationExtensions<T> UseSerialization<T>()
29+
where T : NServiceBus.Serialization.SerializationDefinition, new () { }
30+
protected NServiceBus.TransportExtensions<TTransport> UseTransport<TTransport>()
31+
where TTransport : NServiceBus.Transport.TransportDefinition, new () { }
3932
}
4033
}

0 commit comments

Comments
 (0)