From f2ddf936e2e69737b04d4256cc74f56bb110d824 Mon Sep 17 00:00:00 2001 From: jung Date: Sat, 19 Oct 2024 12:52:15 +0200 Subject: [PATCH] fixed warnings --- .../TestHelper.cs | 10 +++++---- .../Core.ServiceMesh.SourceGen.csproj | 8 +++++++ Core.ServiceMesh.Tests/UnitTest1.cs | 17 ++++++++------ .../Internal/ConsumerRegistration.cs | 11 +++++----- .../Internal/ServiceMeshWorker.cs | 22 +++++++++---------- .../Internal/ServiceRegistration.cs | 6 ++--- Core.ServiceMesh/ServiceMeshExtensions.cs | 4 ++-- SampleApp/Services/AnotherService.cs | 2 +- 8 files changed, 46 insertions(+), 34 deletions(-) diff --git a/Core.ServiceMesh.SourceGen.Tests/TestHelper.cs b/Core.ServiceMesh.SourceGen.Tests/TestHelper.cs index a54dcee..fc56141 100644 --- a/Core.ServiceMesh.SourceGen.Tests/TestHelper.cs +++ b/Core.ServiceMesh.SourceGen.Tests/TestHelper.cs @@ -6,12 +6,12 @@ namespace Core.ServiceMesh.SourceGen.Tests; public static class TestHelper { - public static async Task VerifySourceGen(string source) + public static Task VerifySourceGen(string source) { var syntaxTree = CSharpSyntaxTree.ParseText(source); // force reference - typeof(ServiceMeshAttribute).ToString(); + var _ = typeof(ServiceMeshAttribute).ToString(); var references = AppDomain .CurrentDomain.GetAssemblies() @@ -34,8 +34,8 @@ public static async Task VerifySourceGen(string source) var generator = new ServiceMeshGenerator(); - var driver = CSharpGeneratorDriver.Create(generator) - .RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out _); + CSharpGeneratorDriver.Create(generator) + .RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out var _); foreach (var tree in outputCompilation.SyntaxTrees.Skip(1)) { @@ -44,5 +44,7 @@ public static async Task VerifySourceGen(string source) Assert.Empty(genErrors); } + + return Task.CompletedTask; } } \ No newline at end of file diff --git a/Core.ServiceMesh.SourceGen/Core.ServiceMesh.SourceGen.csproj b/Core.ServiceMesh.SourceGen/Core.ServiceMesh.SourceGen.csproj index a4214c1..aa51757 100644 --- a/Core.ServiceMesh.SourceGen/Core.ServiceMesh.SourceGen.csproj +++ b/Core.ServiceMesh.SourceGen/Core.ServiceMesh.SourceGen.csproj @@ -25,6 +25,14 @@ true + + 1701;1702;NU5128 + + + + 1701;1702;NU5128 + + diff --git a/Core.ServiceMesh.Tests/UnitTest1.cs b/Core.ServiceMesh.Tests/UnitTest1.cs index 06d0db7..6fcda67 100644 --- a/Core.ServiceMesh.Tests/UnitTest1.cs +++ b/Core.ServiceMesh.Tests/UnitTest1.cs @@ -1,6 +1,7 @@ using Core.ServiceMesh.Abstractions; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; using SampleInterfaces; using Xunit.Abstractions; @@ -10,9 +11,9 @@ namespace Core.ServiceMesh.Tests; public class UnitTest1(ITestOutputHelper logger) : IAsyncLifetime { private readonly CancellationTokenSource _cancellation = new(); - private IServiceMesh _mesh; - private IServiceProvider _serviceProvider; - private BackgroundService _worker; + private IServiceMesh? _mesh; + private IServiceProvider? _serviceProvider; + private BackgroundService? _worker; public async Task InitializeAsync() { @@ -34,16 +35,18 @@ public async Task InitializeAsync() _mesh = _serviceProvider.GetRequiredService(); _worker = (BackgroundService)_mesh; + logger.WriteLine("starting background worker"); await _worker.StartAsync(_cancellation.Token); } public async Task DisposeAsync() { + logger.WriteLine("aborting background worker"); await _cancellation.CancelAsync(); try { - await _worker.ExecuteTask!; + await _worker!.ExecuteTask!; } catch { @@ -54,7 +57,7 @@ public async Task DisposeAsync() [Fact] public async Task Test1() { - var someService = _mesh.CreateProxy(); + var someService = _mesh!.CreateProxy(); var res = await someService.GenericAdd(2, 4); @@ -64,7 +67,7 @@ public async Task Test1() [Fact] public async Task Test2() { - var someService = _mesh.CreateProxy(); + var someService = _mesh!.CreateProxy(); var res = await someService.GenericAdd(6m, 6m); @@ -74,7 +77,7 @@ public async Task Test2() [Fact] public async Task Test3() { - var someService = _mesh.CreateProxy(); + var someService = _mesh!.CreateProxy(); var list = new List(); diff --git a/Core.ServiceMesh/Internal/ConsumerRegistration.cs b/Core.ServiceMesh/Internal/ConsumerRegistration.cs index 99d208e..f10ff8a 100644 --- a/Core.ServiceMesh/Internal/ConsumerRegistration.cs +++ b/Core.ServiceMesh/Internal/ConsumerRegistration.cs @@ -6,14 +6,13 @@ namespace Core.ServiceMesh.Internal; internal class ConsumerRegistration { - public string Name { get; init; } + public string Name { get; init; } = string.Empty; public bool IsDurable { get; init; } - public string[] Subjects { get; init; } - public string Stream { get; init; } + public string[] Subjects { get; init; } = []; + public string Stream { get; init; } = string.Empty; public string? QueueGroup { get; init; } - public Type Consumer { get; init; } - public FrozenDictionary Methods { get; init; } + public Type Consumer { get; init; } = null!; + public FrozenDictionary Methods { get; init; } = FrozenDictionary.Empty; public bool Obsolete { get; init; } - public DurableConsumerAttribute? Durable { get; init; } } \ No newline at end of file diff --git a/Core.ServiceMesh/Internal/ServiceMeshWorker.cs b/Core.ServiceMesh/Internal/ServiceMeshWorker.cs index 3a22159..fba94e1 100644 --- a/Core.ServiceMesh/Internal/ServiceMeshWorker.cs +++ b/Core.ServiceMesh/Internal/ServiceMeshWorker.cs @@ -399,7 +399,7 @@ private async Task TransientWorker(CancellationToken stoppingToken) var parentContext = Propagators.DefaultTextMapPropagator.Extract(default, msg.Headers, (headers, key) => { - if (headers.TryGetValue(key, out var value)) + if (headers!.TryGetValue(key, out var value)) return [value[0]]; return Array.Empty(); @@ -447,7 +447,7 @@ private async Task ServiceWorker(CancellationToken stoppingToken) var parentContext = Propagators.DefaultTextMapPropagator.Extract(default, msg.Headers, (headers, key) => { - if (headers.TryGetValue(key, out var value)) + if (headers!.TryGetValue(key, out var value)) return [value[0]]; return Array.Empty(); @@ -479,8 +479,8 @@ private async Task ServiceWorker(CancellationToken stoppingToken) for (var i = 0; i < signatures.Length; i++) { - var sig = signatures[i]; - args[i] = options.Deserialize(invocation.Arguments[i], sig, false); + var sig = signatures[i]!; + args[i] = options.Deserialize(invocation.Arguments[i], sig, false)!; } await using var scope = serviceProvider.CreateAsyncScope(); @@ -488,19 +488,19 @@ private async Task ServiceWorker(CancellationToken stoppingToken) if (method.ReturnType.GetGenericTypeDefinition() == typeof(IAsyncEnumerable<>)) { - var subId = msg.Headers["return-sub-id"].FirstOrDefault(); + var subId = msg.Headers!["return-sub-id"].FirstOrDefault(); try { var resType = method.ReturnType.GenericTypeArguments[0]; var wrapper = typeof(ServiceMeshWorker).GetMethod(nameof(AsyncEnumerableWrapper), - BindingFlags.NonPublic | BindingFlags.Instance) + BindingFlags.NonPublic | BindingFlags.Instance)! .MakeGenericMethod(resType); - dynamic stream = method.Invoke(instance, args.ToArray()); + dynamic stream = method.Invoke(instance, args.ToArray())!; - dynamic awaitable = wrapper.Invoke(this, [subId, stream]); + dynamic awaitable = wrapper.Invoke(this, [subId, stream])!; await awaitable; } catch (Exception ex) @@ -510,7 +510,7 @@ private async Task ServiceWorker(CancellationToken stoppingToken) ["exception"] = ex.Message }; - await nats.PublishAsync(subId, [], headers); + await nats.PublishAsync(subId!, [], headers); activity?.SetStatus(ActivityStatusCode.Error); activity?.RecordException(ex); } @@ -523,7 +523,7 @@ private async Task ServiceWorker(CancellationToken stoppingToken) method = method.MakeGenericMethod( invocation.Generics.Select(options.ResolveType).ToArray()!); - dynamic awaitable = method.Invoke(instance, args.ToArray()); + dynamic awaitable = method.Invoke(instance, args.ToArray())!; await awaitable; if (method.ReturnType == typeof(Task)) @@ -573,7 +573,7 @@ private async Task DurableWorker(CancellationToken stoppingToken) var parentContext = Propagators.DefaultTextMapPropagator.Extract(default, msg.Headers, (headers, key) => { - if (headers.TryGetValue(key, out var value)) + if (headers!.TryGetValue(key, out var value)) return [value[0]]; return Array.Empty(); diff --git a/Core.ServiceMesh/Internal/ServiceRegistration.cs b/Core.ServiceMesh/Internal/ServiceRegistration.cs index aefcc15..cea0e1e 100644 --- a/Core.ServiceMesh/Internal/ServiceRegistration.cs +++ b/Core.ServiceMesh/Internal/ServiceRegistration.cs @@ -5,12 +5,12 @@ namespace Core.ServiceMesh.Internal; internal class ServiceRegistration { - public string Name { get; init; } - public string Sub { get; init; } + public string Name { get; init; } = string.Empty; + public string Sub { get; init; } = string.Empty; //public string Subject { get; init; } = string.Empty; public Type InterfaceType { get; init; } = null!; public Type ImplementationType { get; init; } = null!; - public FrozenDictionary Methods { get; init; } + public FrozenDictionary Methods { get; init; } = FrozenDictionary.Empty; public string QueueGroup { get; init; } = string.Empty; } \ No newline at end of file diff --git a/Core.ServiceMesh/ServiceMeshExtensions.cs b/Core.ServiceMesh/ServiceMeshExtensions.cs index 825e9ba..ddbf80f 100644 --- a/Core.ServiceMesh/ServiceMeshExtensions.cs +++ b/Core.ServiceMesh/ServiceMeshExtensions.cs @@ -69,7 +69,7 @@ public static IServiceCollection AddServiceMesh(this IServiceCollection services { var subject = applyPrefix( - $"{attr.Name}.{method.Name}.G{method.GetGenericArguments().Length}P{method.GetParameters().Length}"); + $"{attr!.Name}.{method.Name}.G{method.GetGenericArguments().Length}P{method.GetParameters().Length}"); if (subject == null) continue; @@ -79,7 +79,7 @@ public static IServiceCollection AddServiceMesh(this IServiceCollection services Services.Add(new ServiceRegistration { - Name = attr.Name, + Name = attr!.Name, Sub = applyPrefix(attr.Name)!, InterfaceType = itype, ImplementationType = type, diff --git a/SampleApp/Services/AnotherService.cs b/SampleApp/Services/AnotherService.cs index 2db32ae..97788f3 100644 --- a/SampleApp/Services/AnotherService.cs +++ b/SampleApp/Services/AnotherService.cs @@ -5,7 +5,7 @@ namespace SampleApp.Services; [ServiceMesh] -public class AnotherService(ILogger logger) : IAnotherService +public class AnotherService : IAnotherService { public ValueTask SampleA(SampleRequest request) {