Skip to content

Commit d4aeb14

Browse files
committed
inline InitializeEndpointUsedOutsideHandlerIfNecessary
1 parent e4f700e commit d4aeb14

File tree

1 file changed

+25
-50
lines changed

1 file changed

+25
-50
lines changed

src/NServiceBus.AzureFunctions.InProcess.ServiceBus/FunctionEndpoint.cs

Lines changed: 25 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,8 @@ public async Task Process(Message message, ExecutionContext executionContext, IL
3636
FunctionsLoggerFactory.Instance.SetCurrentLogger(functionsLogger);
3737

3838
var messageContext = CreateMessageContext(message);
39-
var functionExecutionContext = new FunctionExecutionContext(executionContext, functionsLogger);
40-
41-
await InitializeEndpointIfNecessary(functionExecutionContext,
42-
messageContext.ReceiveCancellationTokenSource.Token).ConfigureAwait(false);
39+
await InitializeEndpointIfNecessary(executionContext, functionsLogger, CancellationToken.None)
40+
.ConfigureAwait(false);
4341

4442
try
4543
{
@@ -78,12 +76,7 @@ MessageContext CreateMessageContext(Message originalMessage)
7876
}
7977
}
8078

81-
/// <summary>
82-
/// Allows to forcefully initialize the endpoint if it hasn't been initialized yet.
83-
/// </summary>
84-
/// <param name="executionContext">The execution context.</param>
85-
/// <param name="token">The cancellation token or default cancellation token.</param>
86-
async Task InitializeEndpointIfNecessary(FunctionExecutionContext executionContext, CancellationToken token = default)
79+
async Task InitializeEndpointIfNecessary(ExecutionContext executionContext, ILogger logger, CancellationToken token = default)
8780
{
8881
if (pipeline == null)
8982
{
@@ -92,7 +85,8 @@ async Task InitializeEndpointIfNecessary(FunctionExecutionContext executionConte
9285
{
9386
if (pipeline == null)
9487
{
95-
endpoint = await endpointFactory(executionContext).ConfigureAwait(false);
88+
var functionExecutionContext = new FunctionExecutionContext(executionContext, logger);
89+
endpoint = await endpointFactory(functionExecutionContext).ConfigureAwait(false);
9690

9791
pipeline = configuration.PipelineInvoker;
9892
}
@@ -107,8 +101,9 @@ async Task InitializeEndpointIfNecessary(FunctionExecutionContext executionConte
107101
/// <inheritdoc />
108102
public async Task Send(object message, SendOptions options, ExecutionContext executionContext, ILogger functionsLogger = null)
109103
{
110-
await InitializeEndpointUsedOutsideHandlerIfNecessary(executionContext, functionsLogger).ConfigureAwait(false);
104+
FunctionsLoggerFactory.Instance.SetCurrentLogger(functionsLogger);
111105

106+
await InitializeEndpointIfNecessary(executionContext, functionsLogger).ConfigureAwait(false);
112107
await endpoint.Send(message, options).ConfigureAwait(false);
113108
}
114109

@@ -121,8 +116,9 @@ public Task Send(object message, ExecutionContext executionContext, ILogger func
121116
/// <inheritdoc />
122117
public async Task Send<T>(Action<T> messageConstructor, SendOptions options, ExecutionContext executionContext, ILogger functionsLogger = null)
123118
{
124-
await InitializeEndpointUsedOutsideHandlerIfNecessary(executionContext, functionsLogger).ConfigureAwait(false);
119+
FunctionsLoggerFactory.Instance.SetCurrentLogger(functionsLogger);
125120

121+
await InitializeEndpointIfNecessary(executionContext, functionsLogger).ConfigureAwait(false);
126122
await endpoint.Send(messageConstructor, options).ConfigureAwait(false);
127123
}
128124

@@ -135,75 +131,54 @@ public Task Send<T>(Action<T> messageConstructor, ExecutionContext executionCont
135131
/// <inheritdoc />
136132
public async Task Publish(object message, PublishOptions options, ExecutionContext executionContext, ILogger functionsLogger = null)
137133
{
138-
await InitializeEndpointUsedOutsideHandlerIfNecessary(executionContext, functionsLogger).ConfigureAwait(false);
134+
FunctionsLoggerFactory.Instance.SetCurrentLogger(functionsLogger);
139135

136+
await InitializeEndpointIfNecessary(executionContext, functionsLogger).ConfigureAwait(false);
140137
await endpoint.Publish(message, options).ConfigureAwait(false);
141138
}
142139

143140
/// <inheritdoc />
144141
public async Task Publish<T>(Action<T> messageConstructor, PublishOptions options, ExecutionContext executionContext, ILogger functionsLogger = null)
145142
{
146-
await InitializeEndpointUsedOutsideHandlerIfNecessary(executionContext, functionsLogger).ConfigureAwait(false);
143+
FunctionsLoggerFactory.Instance.SetCurrentLogger(functionsLogger);
147144

145+
await InitializeEndpointIfNecessary(executionContext, functionsLogger).ConfigureAwait(false);
148146
await endpoint.Publish(messageConstructor, options).ConfigureAwait(false);
149147
}
150148

151149
/// <inheritdoc />
152-
public async Task Publish(object message, ExecutionContext executionContext, ILogger functionsLogger = null)
153-
{
154-
await InitializeEndpointUsedOutsideHandlerIfNecessary(executionContext, functionsLogger).ConfigureAwait(false);
155-
156-
await endpoint.Publish(message).ConfigureAwait(false);
157-
}
150+
public Task Publish(object message, ExecutionContext executionContext, ILogger functionsLogger = null) =>
151+
Publish(message, new PublishOptions(), executionContext, functionsLogger);
158152

159153
/// <inheritdoc />
160-
public async Task Publish<T>(Action<T> messageConstructor, ExecutionContext executionContext, ILogger functionsLogger = null)
161-
{
162-
await InitializeEndpointUsedOutsideHandlerIfNecessary(executionContext, functionsLogger).ConfigureAwait(false);
163-
164-
await endpoint.Publish(messageConstructor).ConfigureAwait(false);
165-
}
154+
public Task Publish<T>(Action<T> messageConstructor, ExecutionContext executionContext, ILogger functionsLogger = null) =>
155+
Publish(messageConstructor, new PublishOptions(), executionContext, functionsLogger);
166156

167157
/// <inheritdoc />
168158
public async Task Subscribe(Type eventType, SubscribeOptions options, ExecutionContext executionContext, ILogger functionsLogger = null)
169159
{
170-
await InitializeEndpointUsedOutsideHandlerIfNecessary(executionContext, functionsLogger).ConfigureAwait(false);
160+
FunctionsLoggerFactory.Instance.SetCurrentLogger(functionsLogger);
171161

162+
await InitializeEndpointIfNecessary(executionContext, functionsLogger).ConfigureAwait(false);
172163
await endpoint.Subscribe(eventType, options).ConfigureAwait(false);
173164
}
174165

175166
/// <inheritdoc />
176-
public async Task Subscribe(Type eventType, ExecutionContext executionContext, ILogger functionsLogger = null)
177-
{
178-
await InitializeEndpointUsedOutsideHandlerIfNecessary(executionContext, functionsLogger).ConfigureAwait(false);
179-
180-
await endpoint.Subscribe(eventType).ConfigureAwait(false);
181-
}
167+
public Task Subscribe(Type eventType, ExecutionContext executionContext, ILogger functionsLogger = null) =>
168+
Subscribe(eventType, new SubscribeOptions(), executionContext, functionsLogger);
182169

183170
/// <inheritdoc />
184171
public async Task Unsubscribe(Type eventType, UnsubscribeOptions options, ExecutionContext executionContext, ILogger functionsLogger = null)
185172
{
186-
await InitializeEndpointUsedOutsideHandlerIfNecessary(executionContext, functionsLogger).ConfigureAwait(false);
173+
FunctionsLoggerFactory.Instance.SetCurrentLogger(functionsLogger);
187174

175+
await InitializeEndpointIfNecessary(executionContext, functionsLogger).ConfigureAwait(false);
188176
await endpoint.Unsubscribe(eventType, options).ConfigureAwait(false);
189177
}
190178

191179
/// <inheritdoc />
192-
public async Task Unsubscribe(Type eventType, ExecutionContext executionContext, ILogger functionsLogger = null)
193-
{
194-
await InitializeEndpointUsedOutsideHandlerIfNecessary(executionContext, functionsLogger).ConfigureAwait(false);
195-
196-
await endpoint.Unsubscribe(eventType).ConfigureAwait(false);
197-
}
198-
199-
async Task InitializeEndpointUsedOutsideHandlerIfNecessary(ExecutionContext executionContext, ILogger functionsLogger)
200-
{
201-
FunctionsLoggerFactory.Instance.SetCurrentLogger(functionsLogger);
202-
203-
var functionExecutionContext = new FunctionExecutionContext(executionContext, functionsLogger);
204-
205-
await InitializeEndpointIfNecessary(functionExecutionContext).ConfigureAwait(false);
206-
}
180+
public Task Unsubscribe(Type eventType, ExecutionContext executionContext, ILogger functionsLogger = null) =>
181+
Unsubscribe(eventType, new UnsubscribeOptions(), executionContext, functionsLogger);
207182

208183
internal static void LoadAssemblies(string assemblyDirectory)
209184
{

0 commit comments

Comments
 (0)