Skip to content

Commit 7c0eab2

Browse files
[TestApp.AspNetCore] enable analysis (#6240)
Co-authored-by: Rajkumar Rangaraj <[email protected]>
1 parent 98b3986 commit 7c0eab2

9 files changed

+73
-71
lines changed

test/TestApp.AspNetCore/ActivityMiddleware.cs

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,29 @@
33

44
namespace TestApp.AspNetCore;
55

6-
public class ActivityMiddleware
6+
internal sealed class ActivityMiddleware
77
{
8-
private readonly ActivityMiddlewareImpl impl;
8+
private readonly ActivityMiddlewareCore core;
99
private readonly RequestDelegate next;
1010

11-
public ActivityMiddleware(RequestDelegate next, ActivityMiddlewareImpl impl)
11+
public ActivityMiddleware(RequestDelegate next, ActivityMiddlewareCore core)
1212
{
1313
this.next = next;
14-
this.impl = impl;
14+
this.core = core;
1515
}
1616

1717
public async Task InvokeAsync(HttpContext context)
1818
{
19-
if (this.impl != null)
19+
if (this.core != null)
2020
{
21-
this.impl.PreProcess(context);
21+
this.core.PreProcess(context);
2222
}
2323

24-
await this.next(context);
24+
await this.next(context).ConfigureAwait(true);
2525

26-
if (this.impl != null)
26+
if (this.core != null)
2727
{
28-
this.impl.PostProcess(context);
29-
}
30-
}
31-
32-
public class ActivityMiddlewareImpl
33-
{
34-
public virtual void PreProcess(HttpContext context)
35-
{
36-
// Do nothing
37-
}
38-
39-
public virtual void PostProcess(HttpContext context)
40-
{
41-
// Do nothing
28+
this.core.PostProcess(context);
4229
}
4330
}
4431
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
namespace TestApp.AspNetCore;
5+
6+
internal sealed class ActivityMiddlewareCore
7+
{
8+
public void PreProcess(HttpContext context)
9+
{
10+
// Do nothing
11+
}
12+
13+
public void PostProcess(HttpContext context)
14+
{
15+
// Do nothing
16+
}
17+
}

test/TestApp.AspNetCore/CallbackMiddleware.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,22 @@
33

44
namespace TestApp.AspNetCore;
55

6-
public class CallbackMiddleware
6+
internal sealed class CallbackMiddleware
77
{
8-
private readonly CallbackMiddlewareImpl impl;
8+
private readonly CallbackMiddlewareCore core;
99
private readonly RequestDelegate next;
1010

11-
public CallbackMiddleware(RequestDelegate next, CallbackMiddlewareImpl impl)
11+
public CallbackMiddleware(RequestDelegate next, CallbackMiddlewareCore core)
1212
{
1313
this.next = next;
14-
this.impl = impl;
14+
this.core = core;
1515
}
1616

1717
public async Task InvokeAsync(HttpContext context)
1818
{
19-
if (this.impl == null || await this.impl.ProcessAsync(context))
19+
if (this.core == null || await this.core.ProcessAsync(context).ConfigureAwait(true))
2020
{
21-
await this.next(context);
22-
}
23-
}
24-
25-
public class CallbackMiddlewareImpl
26-
{
27-
public virtual async Task<bool> ProcessAsync(HttpContext context)
28-
{
29-
return await Task.FromResult(true);
21+
await this.next(context).ConfigureAwait(true);
3022
}
3123
}
3224
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
namespace TestApp.AspNetCore;
5+
6+
internal sealed class CallbackMiddlewareCore
7+
{
8+
public Task<bool> ProcessAsync(HttpContext context)
9+
{
10+
return Task.FromResult(true);
11+
}
12+
}

test/TestApp.AspNetCore/Controllers/ChildActivityController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class ChildActivityController : Controller
1515
public Dictionary<string, string> GetChildActivityTraceContext()
1616
{
1717
var result = new Dictionary<string, string>();
18-
var activity = new Activity("ActivityInsideHttpRequest");
18+
using var activity = new Activity("ActivityInsideHttpRequest");
1919
activity.Start();
2020
result["TraceId"] = activity.Context.TraceId.ToString();
2121
result["ParentSpanId"] = activity.ParentSpanId.ToString();

test/TestApp.AspNetCore/Controllers/ErrorController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ public class ErrorController : Controller
1212
[HttpGet]
1313
public string Get()
1414
{
15-
throw new Exception("something's wrong!");
15+
throw new InvalidOperationException("something's wrong!");
1616
}
1717
}

test/TestApp.AspNetCore/Program.cs

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,44 @@
33

44
using TestApp.AspNetCore;
55

6-
public class Program
7-
{
8-
public static void Main(string[] args)
9-
{
10-
var builder = WebApplication.CreateBuilder(args);
6+
var builder = WebApplication.CreateBuilder(args);
117

12-
// Add services to the container.
8+
// Add services to the container.
139

14-
builder.Services.AddControllers();
10+
builder.Services.AddControllers();
1511

16-
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
17-
builder.Services.AddEndpointsApiExplorer();
12+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
13+
builder.Services.AddEndpointsApiExplorer();
1814

19-
builder.Services.AddSwaggerGen();
15+
builder.Services.AddSwaggerGen();
2016

21-
builder.Services.AddMvc();
17+
builder.Services.AddMvc();
2218

23-
builder.Services.AddSingleton<HttpClient>();
19+
builder.Services.AddSingleton<HttpClient>();
2420

25-
builder.Services.AddSingleton(
26-
new CallbackMiddleware.CallbackMiddlewareImpl());
21+
builder.Services.AddSingleton(new CallbackMiddlewareCore());
2722

28-
builder.Services.AddSingleton(
29-
new ActivityMiddleware.ActivityMiddlewareImpl());
23+
builder.Services.AddSingleton(new ActivityMiddlewareCore());
3024

31-
var app = builder.Build();
25+
var app = builder.Build();
3226

33-
// Configure the HTTP request pipeline.
34-
if (app.Environment.IsDevelopment())
35-
{
36-
app.UseSwagger();
37-
app.UseSwaggerUI();
38-
}
27+
// Configure the HTTP request pipeline.
28+
if (app.Environment.IsDevelopment())
29+
{
30+
app.UseSwagger();
31+
app.UseSwaggerUI();
32+
}
3933

40-
app.UseHttpsRedirection();
34+
app.UseHttpsRedirection();
4135

42-
app.UseAuthorization();
36+
app.UseAuthorization();
4337

44-
app.MapControllers();
38+
app.MapControllers();
4539

46-
app.UseMiddleware<CallbackMiddleware>();
40+
app.UseMiddleware<CallbackMiddleware>();
4741

48-
app.UseMiddleware<ActivityMiddleware>();
42+
app.UseMiddleware<ActivityMiddleware>();
4943

50-
app.AddTestMiddleware();
44+
app.AddTestMiddleware();
5145

52-
app.Run();
53-
}
54-
}
46+
app.Run();

test/TestApp.AspNetCore/TestApp.AspNetCore.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>$(TargetFrameworksForAspNetCoreTests)</TargetFrameworks>
5+
<AnalysisLevel>latest-all</AnalysisLevel>
6+
<NoWarn>$(NoWarn);CA1515;CA1822;CA1812</NoWarn>
57
</PropertyGroup>
68

79
<ItemGroup>

test/TestApp.AspNetCore/TestMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace TestApp.AspNetCore;
55

6-
public static class TestMiddleware
6+
internal static class TestMiddleware
77
{
88
private static readonly AsyncLocal<Action<IApplicationBuilder>?> Current = new();
99

0 commit comments

Comments
 (0)