Skip to content

Commit db5734a

Browse files
committed
Add support for more configuration diagnostics
1 parent e7abb69 commit db5734a

4 files changed

Lines changed: 175 additions & 9 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ static AnalyzerTestFixture()
112112
MetadataReference.CreateFromFile(Assembly.Load("System.Runtime").Location),
113113
#endif
114114
MetadataReference.CreateFromFile(typeof(IFunctionEndpoint).GetTypeInfo().Assembly.Location),
115-
MetadataReference.CreateFromFile(typeof(EndpointConfiguration).GetTypeInfo().Assembly.Location));
115+
MetadataReference.CreateFromFile(typeof(EndpointConfiguration).GetTypeInfo().Assembly.Location),
116+
MetadataReference.CreateFromFile(typeof(AzureServiceBusTransport).GetTypeInfo().Assembly.Location));
116117
}
117118

118119
static readonly ImmutableList<PortableExecutableReference> ProjectReferences;

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

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,110 @@ void Bar(ServiceBusTriggeredEndpointConfiguration endpointConfig)
4848
return Assert(AzureFunctionsDiagnostics.LimitMessageProcessingToNotAllowedId, source);
4949
}
5050

51+
[Test]
52+
public Task DiagnosticIsReportedForDefineCriticalErrorAction()
53+
{
54+
var source =
55+
$@"using NServiceBus;
56+
using System;
57+
using System.Threading.Tasks;
58+
class Foo
59+
{{
60+
void Bar(ServiceBusTriggeredEndpointConfiguration endpointConfig)
61+
{{
62+
[|endpointConfig.AdvancedConfiguration.DefineCriticalErrorAction((errorContext, cancellationToken) => Task.CompletedTask)|];
63+
64+
var advancedConfig = endpointConfig.AdvancedConfiguration;
65+
[|advancedConfig.DefineCriticalErrorAction((errorContext, cancellationToken) => Task.CompletedTask)|];
66+
}}
67+
}}";
68+
69+
return Assert(AzureFunctionsDiagnostics.DefineCriticalErrorActionNotAllowedId, source);
70+
}
71+
72+
[Test]
73+
public Task DiagnosticIsReportedForSetDiagnosticsPath()
74+
{
75+
var source =
76+
$@"using NServiceBus;
77+
using System;
78+
using System.Threading.Tasks;
79+
class Foo
80+
{{
81+
void Bar(ServiceBusTriggeredEndpointConfiguration endpointConfig)
82+
{{
83+
[|endpointConfig.AdvancedConfiguration.SetDiagnosticsPath(null)|];
84+
85+
var advancedConfig = endpointConfig.AdvancedConfiguration;
86+
[|advancedConfig.SetDiagnosticsPath(null)|];
87+
}}
88+
}}";
89+
90+
return Assert(AzureFunctionsDiagnostics.SetDiagnosticsPathNotAllowedId, source);
91+
}
92+
93+
[Test]
94+
public Task DiagnosticIsReportedForMakeInstanceUniquelyAddressable()
95+
{
96+
var source =
97+
$@"using NServiceBus;
98+
using System;
99+
using System.Threading.Tasks;
100+
class Foo
101+
{{
102+
void Bar(ServiceBusTriggeredEndpointConfiguration endpointConfig)
103+
{{
104+
[|endpointConfig.AdvancedConfiguration.MakeInstanceUniquelyAddressable(null)|];
105+
106+
var advancedConfig = endpointConfig.AdvancedConfiguration;
107+
[|advancedConfig.MakeInstanceUniquelyAddressable(null)|];
108+
}}
109+
}}";
110+
111+
return Assert(AzureFunctionsDiagnostics.MakeInstanceUniquelyAddressableNotAllowedId, source);
112+
}
113+
114+
// TODO: Figue out how to test UseTransport<T> extensions
115+
[Test]
116+
public Task DiagnosticIsReportedForUseTransport()
117+
{
118+
var source =
119+
$@"using NServiceBus;
120+
using System;
121+
using System.Threading.Tasks;
122+
class Foo
123+
{{
124+
void Bar(ServiceBusTriggeredEndpointConfiguration endpointConfig)
125+
{{
126+
[|endpointConfig.AdvancedConfiguration.UseTransport(new AzureServiceBusTransport(null))|];
127+
128+
var advancedConfig = endpointConfig.AdvancedConfiguration;
129+
[|advancedConfig.UseTransport(new AzureServiceBusTransport(null))|];
130+
}}
131+
}}";
132+
133+
return Assert(AzureFunctionsDiagnostics.UseTransportNotAllowedId, source);
134+
}
135+
136+
[Test]
137+
public Task DiagnosticIsReportedForOverrideLocalAddress()
138+
{
139+
var source =
140+
$@"using NServiceBus;
141+
using System;
142+
using System.Threading.Tasks;
143+
class Foo
144+
{{
145+
void Bar(ServiceBusTriggeredEndpointConfiguration endpointConfig)
146+
{{
147+
[|endpointConfig.AdvancedConfiguration.OverrideLocalAddress(null)|];
148+
149+
var advancedConfig = endpointConfig.AdvancedConfiguration;
150+
[|advancedConfig.OverrideLocalAddress(null)|];
151+
}}
152+
}}";
153+
154+
return Assert(AzureFunctionsDiagnostics.OverrideLocalAddressNotAllowedId, source);
155+
}
51156
}
52157
}

src/NServiceBus.AzureFunctions.Analyzer/AzureFunctionsConfigurationAnalyzer.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,27 @@ public class AzureFunctionsConfigurationAnalyzer : DiagnosticAnalyzer
1313
{
1414
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(
1515
AzureFunctionsDiagnostics.PurgeOnStartupNotAllowed,
16-
AzureFunctionsDiagnostics.LimitMessageProcessingToNotAllowed
16+
AzureFunctionsDiagnostics.LimitMessageProcessingToNotAllowed,
17+
AzureFunctionsDiagnostics.DefineCriticalErrorActionNotAllowed,
18+
AzureFunctionsDiagnostics.SetDiagnosticsPathNotAllowed,
19+
AzureFunctionsDiagnostics.MakeInstanceUniquelyAddressableNotAllowed,
20+
AzureFunctionsDiagnostics.UseTransportNotAllowed,
21+
AzureFunctionsDiagnostics.OverrideLocalAddressNotAllowed
1722
);
1823

24+
static readonly Dictionary<string, DiagnosticDescriptor> NotAllowedEndpointConfigurationMethods
25+
= new Dictionary<string, DiagnosticDescriptor>
26+
{
27+
["PurgeOnStartup"] = AzureFunctionsDiagnostics.PurgeOnStartupNotAllowed,
28+
["LimitMessageProcessingConcurrencyTo"] = AzureFunctionsDiagnostics.LimitMessageProcessingToNotAllowed,
29+
["DefineCriticalErrorAction"] = AzureFunctionsDiagnostics.DefineCriticalErrorActionNotAllowed,
30+
["SetDiagnosticsPath"] = AzureFunctionsDiagnostics.SetDiagnosticsPathNotAllowed,
31+
["MakeInstanceUniquelyAddressable"] = AzureFunctionsDiagnostics.MakeInstanceUniquelyAddressableNotAllowed,
32+
["UseTransport"] = AzureFunctionsDiagnostics.UseTransportNotAllowed,
33+
["OverrideLocalAddress"] = AzureFunctionsDiagnostics.OverrideLocalAddressNotAllowed,
34+
};
35+
36+
1937
public override void Initialize(AnalysisContext context)
2038
{
2139
context.EnableConcurrentExecution();
@@ -52,12 +70,5 @@ static void Analyze(SyntaxNodeAnalysisContext context)
5270
context.ReportDiagnostic(diagnosticDescriptor, invocationExpression);
5371
}
5472
}
55-
56-
static readonly Dictionary<string, DiagnosticDescriptor> NotAllowedEndpointConfigurationMethods
57-
= new Dictionary<string, DiagnosticDescriptor>
58-
{
59-
["PurgeOnStartup"] = AzureFunctionsDiagnostics.PurgeOnStartupNotAllowed,
60-
["LimitMessageProcessingConcurrencyTo"] = AzureFunctionsDiagnostics.LimitMessageProcessingToNotAllowed
61-
};
6273
}
6374
}

src/NServiceBus.AzureFunctions.Analyzer/AzureFunctionsDiagnostics.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ public static class AzureFunctionsDiagnostics
66
{
77
public const string PurgeOnStartupNotAllowedId = "NSBAF0001";
88
public const string LimitMessageProcessingToNotAllowedId = "NSBAF0002";
9+
public const string DefineCriticalErrorActionNotAllowedId = "NSBAF0003";
10+
public const string SetDiagnosticsPathNotAllowedId = "NSBAF0004";
11+
public const string MakeInstanceUniquelyAddressableNotAllowedId = "NSBAF0005";
12+
public const string UseTransportNotAllowedId = "NSBAF0006";
13+
public const string OverrideLocalAddressNotAllowedId = "NSBAF0007";
914

1015
const string DiagnosticCategory = "NServiceBus.AzureFunctions";
1116

@@ -27,5 +32,49 @@ public static class AzureFunctionsDiagnostics
2732
isEnabledByDefault: true
2833
);
2934

35+
internal static readonly DiagnosticDescriptor DefineCriticalErrorActionNotAllowed = new DiagnosticDescriptor(
36+
id: DefineCriticalErrorActionNotAllowedId,
37+
title: "DefineCriticalErrorAction is not supported in Azure Functions",
38+
messageFormat: "Azure Functions endpoints do not control the application lifecycle and should not define behavior in the case of critical errors.",
39+
category: DiagnosticCategory,
40+
defaultSeverity: DiagnosticSeverity.Error,
41+
isEnabledByDefault: true
42+
);
43+
44+
internal static readonly DiagnosticDescriptor SetDiagnosticsPathNotAllowed = new DiagnosticDescriptor(
45+
id: SetDiagnosticsPathNotAllowedId,
46+
title: "SetDiagnosticsPath is not supported in Azure Functions",
47+
messageFormat: "Azure Functions endpoints should not write diagnostics to the local file system. Use CustomDiagnosticsWriter to write diagnostics to another location.",
48+
category: DiagnosticCategory,
49+
defaultSeverity: DiagnosticSeverity.Error,
50+
isEnabledByDefault: true
51+
);
52+
53+
internal static readonly DiagnosticDescriptor MakeInstanceUniquelyAddressableNotAllowed = new DiagnosticDescriptor(
54+
id: MakeInstanceUniquelyAddressableNotAllowedId,
55+
title: "MakeInstanceUniquelyAddressable is not supported in Azure Functions",
56+
messageFormat: "Azure Functions endpoints have unpredictable lifecycles and should not be uniquely addressable.",
57+
category: DiagnosticCategory,
58+
defaultSeverity: DiagnosticSeverity.Error,
59+
isEnabledByDefault: true
60+
);
61+
62+
internal static readonly DiagnosticDescriptor UseTransportNotAllowed = new DiagnosticDescriptor(
63+
id: UseTransportNotAllowedId,
64+
title: "UseTransport is not supported in Azure Functions",
65+
messageFormat: "The package configures Azure Service Bus transport by default. Use ServiceBusTriggeredEndpointConfiguration.Transport to access the transport configuration.",
66+
category: DiagnosticCategory,
67+
defaultSeverity: DiagnosticSeverity.Warning,
68+
isEnabledByDefault: true
69+
);
70+
71+
internal static readonly DiagnosticDescriptor OverrideLocalAddressNotAllowed = new DiagnosticDescriptor(
72+
id: OverrideLocalAddressNotAllowedId,
73+
title: "OverrideLocalAddress is not supported in Azure Functions",
74+
messageFormat: "Azure Functions endpoints do not control the message receiver and cannot decide the local address.",
75+
category: DiagnosticCategory,
76+
defaultSeverity: DiagnosticSeverity.Error,
77+
isEnabledByDefault: true
78+
);
3079
}
3180
}

0 commit comments

Comments
 (0)