Skip to content

Commit 8929455

Browse files
committed
send and reply options
1 parent db5734a commit 8929455

3 files changed

Lines changed: 94 additions & 1 deletion

File tree

src/NServiceBus.AzureFunctions.Analyzer.Tests/AzureFunctionsConfigurationAnalyzerTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,43 @@ void Bar(ServiceBusTriggeredEndpointConfiguration endpointConfig)
153153

154154
return Assert(AzureFunctionsDiagnostics.OverrideLocalAddressNotAllowedId, source);
155155
}
156+
157+
[Test]
158+
public Task DiagnosticIsReportedForRouteReplyToThisInstance()
159+
{
160+
var source =
161+
$@"using NServiceBus;
162+
using System;
163+
using System.Threading.Tasks;
164+
class Foo
165+
{{
166+
void Bar(ServiceBusTriggeredEndpointConfiguration endpointConfig)
167+
{{
168+
var replyOptions = new ReplyOptions();
169+
[|replyOptions.RouteReplyToThisInstance()|];
170+
}}
171+
}}";
172+
173+
return Assert(AzureFunctionsDiagnostics.RouteReplyToThisInstanceNotAllowedId, source);
174+
}
175+
176+
[Test]
177+
public Task DiagnosticIsReportedForRouteToThisInstance()
178+
{
179+
var source =
180+
$@"using NServiceBus;
181+
using System;
182+
using System.Threading.Tasks;
183+
class Foo
184+
{{
185+
void Bar(ServiceBusTriggeredEndpointConfiguration endpointConfig)
186+
{{
187+
var options = new SendOptions();
188+
[|options.RouteToThisInstance()|];
189+
}}
190+
}}";
191+
192+
return Assert(AzureFunctionsDiagnostics.RouteToThisInstanceNotAllowedId, source);
193+
}
156194
}
157195
}

src/NServiceBus.AzureFunctions.Analyzer/AzureFunctionsConfigurationAnalyzer.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ public class AzureFunctionsConfigurationAnalyzer : DiagnosticAnalyzer
1818
AzureFunctionsDiagnostics.SetDiagnosticsPathNotAllowed,
1919
AzureFunctionsDiagnostics.MakeInstanceUniquelyAddressableNotAllowed,
2020
AzureFunctionsDiagnostics.UseTransportNotAllowed,
21-
AzureFunctionsDiagnostics.OverrideLocalAddressNotAllowed
21+
AzureFunctionsDiagnostics.OverrideLocalAddressNotAllowed,
22+
AzureFunctionsDiagnostics.RouteReplyToThisInstanceNotAllowed,
23+
AzureFunctionsDiagnostics.RouteToThisInstanceNotAllowed
2224
);
2325

2426
static readonly Dictionary<string, DiagnosticDescriptor> NotAllowedEndpointConfigurationMethods
@@ -33,6 +35,12 @@ static readonly Dictionary<string, DiagnosticDescriptor> NotAllowedEndpointConfi
3335
["OverrideLocalAddress"] = AzureFunctionsDiagnostics.OverrideLocalAddressNotAllowed,
3436
};
3537

38+
static readonly Dictionary<string, DiagnosticDescriptor> NotAllowedSendAndReplyOptions
39+
= new Dictionary<string, DiagnosticDescriptor>
40+
{
41+
["RouteReplyToThisInstance"] = AzureFunctionsDiagnostics.RouteReplyToThisInstanceNotAllowed,
42+
["RouteToThisInstance"] = AzureFunctionsDiagnostics.RouteToThisInstanceNotAllowed
43+
};
3644

3745
public override void Initialize(AnalysisContext context)
3846
{
@@ -53,6 +61,13 @@ static void Analyze(SyntaxNodeAnalysisContext context)
5361
return;
5462
}
5563

64+
AnalyzeEndpointConfiguration(context, invocationExpression, memberAccessExpression);
65+
66+
AnalyzeSendAndReplyOptions(context, invocationExpression, memberAccessExpression);
67+
}
68+
69+
static void AnalyzeEndpointConfiguration(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocationExpression, MemberAccessExpressionSyntax memberAccessExpression)
70+
{
5671
if (!NotAllowedEndpointConfigurationMethods.TryGetValue(memberAccessExpression.Name.Identifier.Text, out var diagnosticDescriptor))
5772
{
5873
return;
@@ -70,5 +85,25 @@ static void Analyze(SyntaxNodeAnalysisContext context)
7085
context.ReportDiagnostic(diagnosticDescriptor, invocationExpression);
7186
}
7287
}
88+
89+
static void AnalyzeSendAndReplyOptions(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocationExpression, MemberAccessExpressionSyntax memberAccessExpression)
90+
{
91+
if (!NotAllowedSendAndReplyOptions.TryGetValue(memberAccessExpression.Name.Identifier.Text, out var diagnosticDescriptor))
92+
{
93+
return;
94+
}
95+
96+
var memberAccessSymbol = context.SemanticModel.GetSymbolInfo(memberAccessExpression, context.CancellationToken);
97+
98+
if (!(memberAccessSymbol.Symbol is IMethodSymbol methodSymbol))
99+
{
100+
return;
101+
}
102+
103+
if (methodSymbol.ReceiverType.ToString() == "NServiceBus.SendOptions" || methodSymbol.ReceiverType.ToString() == "NServiceBus.ReplyOptions")
104+
{
105+
context.ReportDiagnostic(diagnosticDescriptor, invocationExpression);
106+
}
107+
}
73108
}
74109
}

src/NServiceBus.AzureFunctions.Analyzer/AzureFunctionsDiagnostics.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public static class AzureFunctionsDiagnostics
1111
public const string MakeInstanceUniquelyAddressableNotAllowedId = "NSBAF0005";
1212
public const string UseTransportNotAllowedId = "NSBAF0006";
1313
public const string OverrideLocalAddressNotAllowedId = "NSBAF0007";
14+
public const string RouteReplyToThisInstanceNotAllowedId = "NSBAF0008";
15+
public const string RouteToThisInstanceNotAllowedId = "NSBAF0009";
1416

1517
const string DiagnosticCategory = "NServiceBus.AzureFunctions";
1618

@@ -76,5 +78,23 @@ public static class AzureFunctionsDiagnostics
7678
defaultSeverity: DiagnosticSeverity.Error,
7779
isEnabledByDefault: true
7880
);
81+
82+
internal static readonly DiagnosticDescriptor RouteReplyToThisInstanceNotAllowed = new DiagnosticDescriptor(
83+
id: RouteReplyToThisInstanceNotAllowedId,
84+
title: "RouteReplyToThisInstance is not supported in Azure Functions",
85+
messageFormat: "Azure Functions endpoints do not control the message receiver and cannot configure receiver routing.",
86+
category: DiagnosticCategory,
87+
defaultSeverity: DiagnosticSeverity.Error,
88+
isEnabledByDefault: true
89+
);
90+
91+
internal static readonly DiagnosticDescriptor RouteToThisInstanceNotAllowed = new DiagnosticDescriptor(
92+
id: RouteToThisInstanceNotAllowedId,
93+
title: "RouteToThisInstance is not supported in Azure Functions",
94+
messageFormat: "Azure Functions endpoints do not control the message receiver and cannot configure receiver routing.",
95+
category: DiagnosticCategory,
96+
defaultSeverity: DiagnosticSeverity.Error,
97+
isEnabledByDefault: true
98+
);
7999
}
80100
}

0 commit comments

Comments
 (0)