11namespace 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}
0 commit comments