Skip to content

Commit

Permalink
Configured static analysis and applied ConfigureAwait(false) to core …
Browse files Browse the repository at this point in the history
…classes

Fixes #130
  • Loading branch information
oskardudycz committed Dec 1, 2022
1 parent c7597ae commit 82ce320
Show file tree
Hide file tree
Showing 35 changed files with 157 additions and 89 deletions.
38 changes: 38 additions & 0 deletions .editorconfig
Expand Up @@ -156,3 +156,41 @@ csharp_space_between_method_call_empty_parameter_list_parentheses = false
# Wrapping preferences
csharp_preserve_single_line_statements = false
csharp_preserve_single_line_blocks = true


###############################
# Reliability Inspections #
###############################

# CA2012: Use ValueTasks correctly
dotnet_diagnostic.CA2012.severity = error

# VSTHRD002 Avoid problematic synchronous waits
dotnet_diagnostic.VSTHRD002.severity = warning

# VSTHRD011 Use AsyncLazy<T>
dotnet_diagnostic.VSTHRD011.severity = warning

# VSTHRD100 Avoid async void methods
dotnet_diagnostic.VSTHRD100.severity = error

# VSTHRD101 Avoid unsupported async delegates
dotnet_diagnostic.VSTHRD101.severity = error

# VSTHRD102 Implement internal logic asynchronously
dotnet_diagnostic.VSTHRD102.severity = error

# VSTHRD103 Call async methods when in an async method
dotnet_diagnostic.VSTHRD103.severity = error

# VSTHRD110 Observe result of async calls
dotnet_diagnostic.VSTHRD110.severity = warning

# VSTHRD111 Use .ConfigureAwait(bool)
dotnet_diagnostic.VSTHRD111.severity = error

# VSTHRD112 Implement System.IAsyncDisposable
dotnet_diagnostic.VSTHRD112.severity = error

# VSTHRD200 Use Async suffix for async methods
dotnet_diagnostic.VSTHRD200.severity = none
12 changes: 12 additions & 0 deletions Core.Build.props
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<AnalysisModeReliability>true</AnalysisModeReliability>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisLevel>latest</AnalysisLevel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.4.27" PrivateAssets="All"/>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0" PrivateAssets="All" Condition=" '$(TargetFrawework)' == 'netstandard2.0' "/>
</ItemGroup>
</Project>
13 changes: 7 additions & 6 deletions Core.ElasticSearch/Core.ElasticSearch.csproj
Expand Up @@ -6,15 +6,16 @@


<ItemGroup>
<PackageReference Include="NEST" Version="7.17.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="NEST" Version="7.17.5"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0"/>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
<ProjectReference Include="..\Core\Core.csproj"/>
</ItemGroup>

<Import Project="../Core.Build.props"/>
</Project>
4 changes: 2 additions & 2 deletions Core.ElasticSearch/Projections/ElasticSearchProjection.cs
Expand Up @@ -28,7 +28,7 @@ public async Task Handle(EventEnvelope<TEvent> eventEnvelope, CancellationToken
var id = getId(eventEnvelope.Data);
var indexName = IndexNameMapper.ToIndexName<TView>();

var entity = (await elasticClient.GetAsync<TView>(id, i => i.Index(indexName), ct))?.Source ??
var entity = (await elasticClient.GetAsync<TView>(id, i => i.Index(indexName), ct).ConfigureAwait(false))?.Source ??
(TView) Activator.CreateInstance(typeof(TView), true)!;

entity.When(eventEnvelope);
Expand All @@ -37,7 +37,7 @@ public async Task Handle(EventEnvelope<TEvent> eventEnvelope, CancellationToken
entity,
i => i.Index(indexName).Id(id).VersionType(VersionType.External).Version((long)eventEnvelope.Metadata.StreamPosition),
ct
);
).ConfigureAwait(false);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Core.ElasticSearch/Repository/ElasticSearchRepository.cs
Expand Up @@ -26,7 +26,7 @@ IElasticClient elasticClient

public async Task<T?> Find(Guid id, CancellationToken cancellationToken)
{
var response = await elasticClient.GetAsync<T>(id, ct: cancellationToken);
var response = await elasticClient.GetAsync<T>(id, ct: cancellationToken).ConfigureAwait(false);
return response?.Source;
}

Expand Down
15 changes: 8 additions & 7 deletions Core.EventStoreDB/Core.EventStoreDB.csproj
Expand Up @@ -6,16 +6,17 @@


<ItemGroup>
<PackageReference Include="EventStore.Client.Grpc.Streams" Version="22.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="EventStore.Client.Grpc.Streams" Version="22.0.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0"/>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0"/>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
<ProjectReference Include="..\Core\Core.csproj"/>
</ItemGroup>

<Import Project="../Core.Build.props"/>
</Project>
2 changes: 1 addition & 1 deletion Core.EventStoreDB/Events/AggregateStreamExtensions.cs
Expand Up @@ -22,7 +22,7 @@ public static class AggregateStreamExtensions
cancellationToken: cancellationToken
);

if (await readResult.ReadState == ReadState.StreamNotFound)
if (await readResult.ReadState.ConfigureAwait(false) == ReadState.StreamNotFound)
return null;

var aggregate = (T)Activator.CreateInstance(typeof(T), true)!;
Expand Down
6 changes: 3 additions & 3 deletions Core.EventStoreDB/Repository/EventStoreDBRepository.cs
Expand Up @@ -45,7 +45,7 @@ IActivityScope activityScope
StreamState.NoStream,
GetEventsToStore(aggregate, TelemetryPropagator.GetPropagationContext(activity)),
cancellationToken: ct
);
).ConfigureAwait(false);
return result.NextExpectedStreamRevision.ToUInt64();
},
token
Expand All @@ -63,7 +63,7 @@ IActivityScope activityScope
nextVersion,
eventsToAppend,
cancellationToken: ct
);
).ConfigureAwait(false);
return result.NextExpectedStreamRevision.ToUInt64();
},
token
Expand All @@ -81,7 +81,7 @@ IActivityScope activityScope
nextVersion,
eventsToAppend,
cancellationToken: ct
);
).ConfigureAwait(false);
return result.NextExpectedStreamRevision.ToUInt64();
},
token
Expand Down
6 changes: 3 additions & 3 deletions Core.EventStoreDB/Repository/RepositoryExtensions.cs
Expand Up @@ -11,7 +11,7 @@ public static class RepositoryExtensions
CancellationToken ct
) where T : class, IAggregate
{
var entity = await repository.Find(id, ct);
var entity = await repository.Find(id, ct).ConfigureAwait(false);

return entity ?? throw AggregateNotFoundException.For<T>(id);
}
Expand All @@ -24,10 +24,10 @@ CancellationToken ct
CancellationToken ct = default
) where T : class, IAggregate
{
var entity = await repository.Get(id, ct);
var entity = await repository.Get(id, ct).ConfigureAwait(false);

action(entity);

return await repository.Update(entity, expectedVersion, ct);
return await repository.Update(entity, expectedVersion, ct).ConfigureAwait(false);
}
}
Expand Up @@ -22,12 +22,12 @@ public class EventStoreDBSubscriptionCheckpointRepository: ISubscriptionCheckpoi
var result = eventStoreClient.ReadStreamAsync(Direction.Backwards, streamName, StreamPosition.End, 1,
cancellationToken: ct);

if (await result.ReadState == ReadState.StreamNotFound)
if (await result.ReadState.ConfigureAwait(false) == ReadState.StreamNotFound)
{
return null;
}

ResolvedEvent? @event = await result.FirstOrDefaultAsync(ct);
ResolvedEvent? @event = await result.FirstOrDefaultAsync(ct).ConfigureAwait(false);

return @event?.Deserialize<CheckpointStored>()?.Position;
}
Expand All @@ -46,7 +46,7 @@ public async ValueTask Store(string subscriptionId, ulong position, Cancellation
StreamState.StreamExists,
eventToAppend,
cancellationToken: ct
);
).ConfigureAwait(false);
}
catch (WrongExpectedVersionException)
{
Expand All @@ -58,15 +58,15 @@ public async ValueTask Store(string subscriptionId, ulong position, Cancellation
StreamState.NoStream,
new StreamMetadata(1),
cancellationToken: ct
);
).ConfigureAwait(false);

// append event again expecting stream to not exist
await eventStoreClient.AppendToStreamAsync(
streamName,
StreamState.NoStream,
eventToAppend,
cancellationToken: ct
);
).ConfigureAwait(false);
}
}

Expand Down
13 changes: 8 additions & 5 deletions Core.EventStoreDB/Subscriptions/EventStoreDBSubscriptionToAll.cs
Expand Up @@ -60,7 +60,7 @@ public async Task SubscribeToAll(EventStoreDBSubscriptionToAllOptions subscripti

logger.LogInformation("Subscription to all '{SubscriptionId}'", subscriptionOptions.SubscriptionId);

var checkpoint = await checkpointRepository.Load(SubscriptionId, ct);
var checkpoint = await checkpointRepository.Load(SubscriptionId, ct).ConfigureAwait(false);

await eventStoreClient.SubscribeToAllAsync(
checkpoint == null ? FromAll.Start : FromAll.After(new Position(checkpoint.Value, checkpoint.Value)),
Expand All @@ -70,7 +70,7 @@ public async Task SubscribeToAll(EventStoreDBSubscriptionToAllOptions subscripti
subscriptionOptions.FilterOptions,
subscriptionOptions.Credentials,
ct
);
).ConfigureAwait(false);

logger.LogInformation("Subscription to all '{SubscriptionId}' started", SubscriptionId);
}
Expand Down Expand Up @@ -108,9 +108,10 @@ CancellationToken token
async (_, ct) =>
{
// publish event to internal event bus
await eventBus.Publish(eventEnvelope, ct);
await eventBus.Publish(eventEnvelope, ct).ConfigureAwait(false);
await checkpointRepository.Store(SubscriptionId, resolvedEvent.Event.Position.CommitPosition, ct);
await checkpointRepository.Store(SubscriptionId, resolvedEvent.Event.Position.CommitPosition, ct)
.ConfigureAwait(false);
},
new StartActivityOptions
{
Expand All @@ -119,7 +120,7 @@ CancellationToken token
Kind = ActivityKind.Consumer
},
token
);
).ConfigureAwait(false);
}
catch (Exception e)
{
Expand Down Expand Up @@ -162,7 +163,9 @@ private void Resubscribe()
// As this is a background process then we don't need to have async context here.
using (NoSynchronizationContextScope.Enter())
{
#pragma warning disable VSTHRD002
SubscribeToAll(subscriptionOptions, cancellationToken).Wait(cancellationToken);
#pragma warning restore VSTHRD002
}

resubscribed = true;
Expand Down
6 changes: 3 additions & 3 deletions Core.Kafka/Consumers/KafkaConsumer.cs
Expand Up @@ -51,7 +51,7 @@ public async Task StartAsync(CancellationToken cancellationToken)
while (!cancellationToken.IsCancellationRequested)
{
// consume event from Kafka
await ConsumeNextEvent(consumer, cancellationToken);
await ConsumeNextEvent(consumer, cancellationToken).ConfigureAwait(false);
}
}
catch (Exception e)
Expand Down Expand Up @@ -96,7 +96,7 @@ private async Task ConsumeNextEvent(IConsumer<string, string> consumer, Cancella
async (_, ct) =>
{
// publish event to internal event bus
await eventBus.Publish(eventEnvelope, ct);
await eventBus.Publish(eventEnvelope, ct).ConfigureAwait(false);
consumer.Commit();
},
Expand All @@ -118,7 +118,7 @@ private async Task ConsumeNextEvent(IConsumer<string, string> consumer, Cancella
Kind = ActivityKind.Consumer
},
token
);
).ConfigureAwait(false);
}
catch (Exception e)
{
Expand Down
11 changes: 6 additions & 5 deletions Core.Kafka/Core.Kafka.csproj
Expand Up @@ -5,14 +5,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Confluent.Kafka" Version="1.9.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Confluent.Kafka" Version="1.9.3"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0"/>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0"/>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
<ProjectReference Include="..\Core\Core.csproj"/>
</ItemGroup>

<Import Project="../Core.Build.props"/>
</Project>
2 changes: 1 addition & 1 deletion Core.Kafka/Producers/KafkaProducer.cs
Expand Up @@ -63,7 +63,7 @@ public async Task Publish(IEventEnvelope @event, CancellationToken token)
Kind = ActivityKind.Producer
},
token
);
).ConfigureAwait(false);
}
catch (Exception e)
{
Expand Down
15 changes: 8 additions & 7 deletions Core.Marten/Core.Marten.csproj
Expand Up @@ -5,16 +5,17 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Marten" Version="6.0.0-alpha.3" />
<PackageReference Include="MediatR" Version="11.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Marten" Version="6.0.0-alpha.3"/>
<PackageReference Include="MediatR" Version="11.0.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Core.Serialization\Core.Serialization.csproj" />
<ProjectReference Include="..\Core\Core.csproj" />
<ProjectReference Include="..\Core.Serialization\Core.Serialization.csproj"/>
<ProjectReference Include="..\Core\Core.csproj"/>
</ItemGroup>

<Import Project="../Core.Build.props"/>
</Project>
4 changes: 2 additions & 2 deletions Core.Marten/ExternalProjections/MartenExternalProjection.cs
Expand Up @@ -25,7 +25,7 @@ public async Task Handle(EventEnvelope<TEvent> eventEnvelope, CancellationToken
{
var (@event, eventMetadata) = eventEnvelope;

var entity = await session.LoadAsync<TView>(getId(@event), ct) ??
var entity = await session.LoadAsync<TView>(getId(@event), ct).ConfigureAwait(false) ??
(TView)Activator.CreateInstance(typeof(TView), true)!;

var eventLogPosition = eventMetadata.LogPosition;
Expand All @@ -39,7 +39,7 @@ public async Task Handle(EventEnvelope<TEvent> eventEnvelope, CancellationToken

session.Store(entity);

await session.SaveChangesAsync(ct);
await session.SaveChangesAsync(ct).ConfigureAwait(false);
}
}

Expand Down

0 comments on commit 82ce320

Please sign in to comment.