Skip to content

Commit b64cd15

Browse files
authored
Enable Roslyn analysis - others - part 1 (open-telemetry#4741)
1 parent f32f048 commit b64cd15

File tree

61 files changed

+244
-205
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+244
-205
lines changed

examples/demo/Client/Examples.Client.csproj

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

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
</PropertyGroup>
88

examples/demo/Client/Program.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
// dotnet run http://localhost:5200
66
if (args.Length != 1)
77
{
8-
Console.WriteLine(@"URL missing");
8+
#pragma warning disable CA1303 // Do not pass literals as localized parameters
9+
Console.WriteLine("URL missing");
10+
#pragma warning restore CA1303 // Do not pass literals as localized parameters
911
return 2;
1012
}
1113

@@ -15,13 +17,13 @@
1517
{
1618
try
1719
{
18-
var content = await httpClient.GetStringAsync(url);
20+
var content = await httpClient.GetStringAsync(new Uri(url)).ConfigureAwait(false);
1921
Console.WriteLine(content);
2022
}
2123
catch (HttpRequestException ex)
2224
{
2325
Console.WriteLine(ex.Message);
2426
}
2527

26-
Thread.Sleep(5000);
28+
await Task.Delay(5000).ConfigureAwait(false);
2729
}

examples/demo/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM mcr.microsoft.com/dotnet/sdk:8.0.416-jammy@sha256:71217e2029c9844f13aa98114ae4aea9e77287cd8c152f7b7c09f8af61a1affa
1+
FROM mcr.microsoft.com/dotnet/sdk:10.0.101-noble@sha256:d1823fecac3689a2eb959e02ee3bfe1c2142392808240039097ad70644566190
22

33
# install OpenTelemetry .NET Automatic Instrumentation
44
ARG OTEL_VERSION=1.13.0
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
</PropertyGroup>
77

88
<ItemGroup>
99
<PackageReference Include="Microsoft.Data.SqlClient" />
10-
<PackageReference Include="Microsoft.Extensions.Logging" />
1110
</ItemGroup>
1211

1312
</Project>

examples/demo/Service/Program.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020

2121
async Task<string> Handler(ILogger<Program> logger)
2222
{
23-
await ExecuteSql("SELECT 1");
23+
await ExecuteSql("SELECT 1").ConfigureAwait(false);
2424

2525
// .NET Diagnostics: create a manual span
2626
using (var activity = activitySource.StartActivity("SayHello"))
2727
{
2828
activity?.SetTag("foo", 1);
2929
activity?.SetTag("bar", "Hello, World!");
30-
activity?.SetTag("baz", new int[] { 1, 2, 3 });
30+
activity?.SetTag("baz", (int[])[1, 2, 3]);
3131

3232
var waitTime = Random.Shared.NextDouble(); // max 1 seconds
33-
await Task.Delay(TimeSpan.FromSeconds(waitTime));
33+
await Task.Delay(TimeSpan.FromSeconds(waitTime)).ConfigureAwait(false);
3434

3535
activity?.SetStatus(ActivityStatusCode.Ok);
3636

@@ -47,7 +47,9 @@ async Task<string> Handler(ILogger<Program> logger)
4747
async Task ExecuteSql(string sql)
4848
{
4949
using var connection = new SqlConnection(connectionString);
50-
await connection.OpenAsync();
50+
await connection.OpenAsync().ConfigureAwait(false);
51+
#pragma warning disable CA2100 // Review SQL queries for security vulnerabilities. It is static SQL for demo purposes.
5152
using var command = new SqlCommand(sql, connection);
52-
using var reader = await command.ExecuteReaderAsync();
53+
#pragma warning restore CA2100 // Review SQL queries for security vulnerabilities. It is static SQL for demo purposes.
54+
using var reader = await command.ExecuteReaderAsync().ConfigureAwait(false);
5355
}

examples/playground/AspNetCoreMvc/Controllers/ApiController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public ActionResult Delay(int seconds)
2020
[Route("delay-async/{seconds}")]
2121
public async Task<ActionResult> DelayAsync(int seconds)
2222
{
23-
await Task.Delay(TimeSpan.FromSeconds(seconds));
23+
await Task.Delay(TimeSpan.FromSeconds(seconds)).ConfigureAwait(false);
2424
return Ok(seconds);
2525
}
2626
}

examples/playground/AspNetCoreMvc/Controllers/HomeController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public IActionResult Index()
2222
from prefix in prefixes
2323
let key = (envVar.Key as string)?.ToUpperInvariant()
2424
let value = envVar.Value as string
25-
where key.StartsWith(prefix)
25+
where key.StartsWith(prefix, StringComparison.Ordinal)
2626
orderby key
2727
select new KeyValuePair<string, string>(key, value);
2828

@@ -41,14 +41,14 @@ public IActionResult Delay(int seconds)
4141
public async Task<IActionResult> DelayAsync(int seconds)
4242
{
4343
ViewBag.StackTrace = StackTraceHelper.GetUsefulStack();
44-
await Task.Delay(TimeSpan.FromSeconds(seconds));
44+
await Task.Delay(TimeSpan.FromSeconds(seconds)).ConfigureAwait(false);
4545
return View("Delay", seconds);
4646
}
4747

4848
[Route("bad-request")]
4949
public IActionResult ThrowException()
5050
{
51-
throw new Exception("This was a bad request.");
51+
throw new InvalidOperationException("This was a bad request.");
5252
}
5353

5454
[Route("status-code/{statusCode}")]

examples/playground/AspNetCoreMvc/Examples.AspNetCoreMvc.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<PropertyGroup>
44
<TargetFrameworks>net10.0;net9.0;net8.0</TargetFrameworks>
55
<RootNamespace>Examples.AspNetCoreMvc</RootNamespace>
6+
<!-- CA1515: Consider making public types internal. Controllers needs to be public. -->
7+
<NoWarn>$(NoWarn);CA1515</NoWarn>
68
</PropertyGroup>
79

810
<ItemGroup>

examples/playground/AspNetCoreMvc/OtelSdkPlugin.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ public class OtelSdkPlugin
77
{
88
public void Initializing()
99
{
10+
#pragma warning disable CA1303 // Do not pass literals as localized parameters
1011
Console.WriteLine("Hello from OtelSdkPlugin");
12+
#pragma warning restore CA1303 // Do not pass literals as localized parameters
1113
}
1214
}

examples/playground/AspNetCoreMvc/Program.cs

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

44
namespace Examples.AspNetCoreMvc;
55

6-
public class Program
6+
internal static class Program
77
{
88
public static void Main(string[] args)
99
{

0 commit comments

Comments
 (0)