Skip to content

Commit

Permalink
Add ConfigureAwait(false) (#21110)
Browse files Browse the repository at this point in the history
Set up Microsoft.CodeAnalysis.FxCopAnalyzers with only the
ConfigureAwait rule enabled.

Closes #10164
  • Loading branch information
roji committed Jun 2, 2020
1 parent f727d7a commit e1c9a3a
Show file tree
Hide file tree
Showing 85 changed files with 543 additions and 274 deletions.
1 change: 0 additions & 1 deletion All.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{CE6B50B2-34AE-44C9-940A-4E48C3E1B3BC}"
ProjectSection(SolutionItems) = preProject
src\Directory.Build.props = src\Directory.Build.props
src\EFCore.ruleset = src\EFCore.ruleset
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-ef", "src\dotnet-ef\dotnet-ef.csproj", "{2D66A1DA-D102-4DD9-960B-7D863BBB53DE}"
Expand Down
1 change: 1 addition & 0 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<SQLitePCLRawBundleWinsqlite3Version>2.0.3</SQLitePCLRawBundleWinsqlite3Version>
<SQLitePCLRawCoreVersion>2.0.3</SQLitePCLRawCoreVersion>
<IdentityServer4EntityFrameworkVersion>3.0.0</IdentityServer4EntityFrameworkVersion>
<FxCopAnalyzersVersion>3.0.0</FxCopAnalyzersVersion>
<StyleCopAnalyzersVersion>1.1.118</StyleCopAnalyzersVersion>
<BenchmarkDotNetVersion>0.12.0</BenchmarkDotNetVersion>
<MicrosoftDataSqlClientVersion>2.0.0-preview3.20122.2</MicrosoftDataSqlClientVersion>
Expand Down
5 changes: 5 additions & 0 deletions rulesets/EFCore.ruleset
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="EFCore" ToolsVersion="15.0">
<Include Path="StyleCop.ruleset" Action="Default" />
<Include Path="FxCop.ruleset" Action="Default" />
</RuleSet>
218 changes: 218 additions & 0 deletions rulesets/FxCop.ruleset

Large diffs are not rendered by default.

15 changes: 2 additions & 13 deletions EFCore.ruleset → rulesets/StyleCop.ruleset
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="EFCore" ToolsVersion="15.0">
<?xml version="1.0"?>
<RuleSet Name="StyleCop" Description="StyleCop." ToolsVersion="15.0">
<Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
<Rule Id="SA1000" Action="None" />
<Rule Id="SA1001" Action="None" />
Expand Down Expand Up @@ -154,15 +154,4 @@
<Rule Id="SA1649" Action="None" />
<Rule Id="SA1650" Action="Warning" />
</Rules>
<Rules AnalyzerId="Roslynator.CSharp.Analyzers" RuleNamespace="Roslynator.CSharp.Analyzers">
<Rule Id="RCS1079" Action="None" />
<Rule Id="RCS1090" Action="None" />
<Rule Id="RCS1118" Action="None" />
<Rule Id="RCS1141" Action="None" />
<Rule Id="RCS1142" Action="None" />
<Rule Id="RCS1165" Action="None" />
<Rule Id="RCS1168" Action="None" />
<Rule Id="RCS1194" Action="None" />
<Rule Id="RCS1206" Action="None" />
</Rules>
</RuleSet>
8 changes: 7 additions & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
<Project>
<Import Project="..\Directory.Build.props" />

<PropertyGroup>
<IsPackable>True</IsPackable>
<IncludeSymbols>True</IncludeSymbols>
<CodeAnalysisRuleSet>..\..\rulesets\EFCore.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="$(FxCopAnalyzersVersion)" PrivateAssets="All" />
</ItemGroup>

</Project>
1 change: 0 additions & 1 deletion src/EFCore.Abstractions/EFCore.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<AssemblyName>Microsoft.EntityFrameworkCore.Abstractions</AssemblyName>
<RootNamespace>Microsoft.EntityFrameworkCore</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<CodeAnalysisRuleSet>..\..\EFCore.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion src/EFCore.Analyzers/EFCore.Analyzers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<AssemblyName>Microsoft.EntityFrameworkCore.Analyzers</AssemblyName>
<RootNamespace>Microsoft.EntityFrameworkCore</RootNamespace>
<IncludeBuildOutput>false</IncludeBuildOutput>
<CodeAnalysisRuleSet>..\..\EFCore.ruleset</CodeAnalysisRuleSet>
<NuspecFile>$(MSBuildProjectName).nuspec</NuspecFile>
</PropertyGroup>

Expand Down
1 change: 0 additions & 1 deletion src/EFCore.Cosmos/EFCore.Cosmos.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<RootNamespace>Microsoft.EntityFrameworkCore.Cosmos</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>$(PackageTags);CosmosDb;SQL API</PackageTags>
<CodeAnalysisRuleSet>..\..\EFCore.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public async ValueTask<bool> MoveNextAsync()
_cosmosQueryContext.InitializeStateManager(_performIdentityResolution);
}

var hasNext = await _enumerator.MoveNextAsync();
var hasNext = await _enumerator.MoveNextAsync().ConfigureAwait(false);

Current
= hasNext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ public async ValueTask<bool> MoveNextAsync()
_readItemExpression.Container,
partitionKey,
resourceId,
_cancellationToken);
_cancellationToken)
.ConfigureAwait(false);

return ShapeResult();
}
Expand Down
41 changes: 23 additions & 18 deletions src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,15 @@ private async Task<bool> CreateItemOnceAsync(
await using var writer = new StreamWriter(stream, new UTF8Encoding(), bufferSize: 1024, leaveOpen: false);
using var jsonWriter = new JsonTextWriter(writer);
JsonSerializer.Create().Serialize(jsonWriter, parameters.Document);
await jsonWriter.FlushAsync(cancellationToken);
await jsonWriter.FlushAsync(cancellationToken).ConfigureAwait(false);

var entry = parameters.Entry;
var container = Client.GetDatabase(_databaseId).GetContainer(parameters.ContainerId);
var itemRequestOptions = CreateItemRequestOptions(entry);
var partitionKey = CreatePartitionKey(entry);

using var response = await container.CreateItemStreamAsync(stream, partitionKey, itemRequestOptions, cancellationToken);
using var response = await container.CreateItemStreamAsync(stream, partitionKey, itemRequestOptions, cancellationToken)
.ConfigureAwait(false);
ProcessResponse(response, entry);

return response.StatusCode == HttpStatusCode.Created;
Expand Down Expand Up @@ -348,15 +349,16 @@ private async Task<bool> ReplaceItemOnceAsync(
using var writer = new StreamWriter(stream, new UTF8Encoding(), bufferSize: 1024, leaveOpen: false);
using var jsonWriter = new JsonTextWriter(writer);
JsonSerializer.Create().Serialize(jsonWriter, parameters.Document);
await jsonWriter.FlushAsync(cancellationToken);
await jsonWriter.FlushAsync(cancellationToken).ConfigureAwait(false);

var entry = parameters.Entry;
var container = Client.GetDatabase(_databaseId).GetContainer(parameters.ContainerId);
var itemRequestOptions = CreateItemRequestOptions(entry);
var partitionKey = CreatePartitionKey(entry);

using var response = await container.ReplaceItemStreamAsync(
stream, parameters.ItemId, partitionKey, itemRequestOptions, cancellationToken);
stream, parameters.ItemId, partitionKey, itemRequestOptions, cancellationToken)
.ConfigureAwait(false);
ProcessResponse(response, entry);

return response.StatusCode == HttpStatusCode.OK;
Expand Down Expand Up @@ -417,7 +419,8 @@ public virtual async Task<bool> DeleteItemOnceAsync(
var partitionKey = CreatePartitionKey(entry);

using var response = await items.DeleteItemStreamAsync(
parameters.DocumentId, partitionKey, itemRequestOptions, cancellationToken: cancellationToken);
parameters.DocumentId, partitionKey, itemRequestOptions, cancellationToken: cancellationToken)
.ConfigureAwait(false);
ProcessResponse(response, entry);

return response.StatusCode == HttpStatusCode.NoContent;
Expand Down Expand Up @@ -550,7 +553,8 @@ internal virtual async Task<JObject> ExecuteReadItemAsync(
_commandLogger.ExecutingReadItem(partitionKey, resourceId);

var responseMessage = await CreateSingleItemQuery(
containerId, partitionKey, resourceId, cancellationToken);
containerId, partitionKey, resourceId, cancellationToken)
.ConfigureAwait(false);

return JObjectFromReadItemResponseMessage(responseMessage);
}
Expand Down Expand Up @@ -597,11 +601,12 @@ private async Task<ResponseMessage> CreateSingleItemQuery(
CancellationToken cancellationToken = default)
{
var container = Client.GetDatabase(_databaseId).GetContainer(containerId);

return await container.ReadItemStreamAsync(
resourceId,
string.IsNullOrEmpty(partitionKey) ? PartitionKey.None : new PartitionKey(partitionKey),
cancellationToken: cancellationToken);
resourceId,
string.IsNullOrEmpty(partitionKey) ? PartitionKey.None : new PartitionKey(partitionKey),
cancellationToken: cancellationToken)
.ConfigureAwait(false);
}


Expand Down Expand Up @@ -781,7 +786,7 @@ private sealed class AsyncEnumerator : IAsyncEnumerator<JObject>
private JsonTextReader _jsonReader;

private FeedIterator _query;

public JObject Current { get; private set; }

public AsyncEnumerator(DocumentAsyncEnumerable documentEnumerable, CancellationToken cancellationToken)
Expand All @@ -808,7 +813,7 @@ public async ValueTask<bool> MoveNextAsync()
return false;
}

_responseMessage = await _query.ReadNextAsync(_cancellationToken);
_responseMessage = await _query.ReadNextAsync(_cancellationToken).ConfigureAwait(false);
_responseMessage.EnsureSuccessStatusCode();

_responseStream = _responseMessage.Content;
Expand All @@ -822,26 +827,26 @@ public async ValueTask<bool> MoveNextAsync()
return true;
}

await ResetReadAsync();
await ResetReadAsync().ConfigureAwait(false);

return await MoveNextAsync();
return await MoveNextAsync().ConfigureAwait(false);
}

private async Task ResetReadAsync()
{
_jsonReader?.Close();
_jsonReader = null;
await _reader.DisposeAsyncIfAvailable();
await _reader.DisposeAsyncIfAvailable().ConfigureAwait(false);
_reader = null;
await _responseStream.DisposeAsyncIfAvailable();
await _responseStream.DisposeAsyncIfAvailable().ConfigureAwait(false);
_responseStream = null;
}

public async ValueTask DisposeAsync()
{
await ResetReadAsync();
await ResetReadAsync().ConfigureAwait(false);

await _responseMessage.DisposeAsyncIfAvailable();
await _responseMessage.DisposeAsyncIfAvailable().ConfigureAwait(false);
_responseMessage = null;
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,24 @@ public virtual bool EnsureCreated()
/// </summary>
public virtual async Task<bool> EnsureCreatedAsync(CancellationToken cancellationToken = default)
{
var created = await _cosmosClient.CreateDatabaseIfNotExistsAsync(cancellationToken);
var created = await _cosmosClient.CreateDatabaseIfNotExistsAsync(cancellationToken)
.ConfigureAwait(false);
foreach (var entityType in _model.GetEntityTypes())
{
var containerName = entityType.GetContainer();
if (containerName != null)
{
created |= await _cosmosClient.CreateContainerIfNotExistsAsync(
containerName,
GetPartitionKeyStoreName(entityType),
cancellationToken);
containerName,
GetPartitionKeyStoreName(entityType),
cancellationToken)
.ConfigureAwait(false);
}
}

if (created)
{
await SeedAsync(cancellationToken);
await SeedAsync(cancellationToken).ConfigureAwait(false);
}

return created;
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public override async Task<int> SaveChangesAsync(
entriesSaved.Add(entry);
try
{
if (await SaveAsync(entry, cancellationToken))
if (await SaveAsync(entry, cancellationToken).ConfigureAwait(false))
{
rowsAffected++;
}
Expand All @@ -186,7 +186,7 @@ public override async Task<int> SaveChangesAsync(
foreach (var rootEntry in rootEntriesToSave)
{
if (!entriesSaved.Contains(rootEntry)
&& await SaveAsync(rootEntry, cancellationToken))
&& await SaveAsync(rootEntry, cancellationToken).ConfigureAwait(false))
{
rowsAffected++;
}
Expand Down
1 change: 0 additions & 1 deletion src/EFCore.Design/EFCore.Design.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<AssemblyName>Microsoft.EntityFrameworkCore.Design</AssemblyName>
<RootNamespace>Microsoft.EntityFrameworkCore</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<CodeAnalysisRuleSet>..\..\EFCore.ruleset</CodeAnalysisRuleSet>
<DevelopmentDependency>True</DevelopmentDependency>
</PropertyGroup>

Expand Down
1 change: 0 additions & 1 deletion src/EFCore.InMemory/EFCore.InMemory.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<RootNamespace>Microsoft.EntityFrameworkCore.InMemory</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>$(PackageTags);In-Memory</PackageTags>
<CodeAnalysisRuleSet>..\..\EFCore.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion src/EFCore.Proxies/EFCore.Proxies.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<RootNamespace>Microsoft.EntityFrameworkCore</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>$(PackageTags);Lazy-loading</PackageTags>
<CodeAnalysisRuleSet>..\..\EFCore.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public async Task<InterceptionResult<DbDataReader>> ReaderExecutingAsync(
{
for (var i = 0; i < _interceptors.Length; i++)
{
result = await _interceptors[i].ReaderExecutingAsync(command, eventData, result, cancellationToken);
result = await _interceptors[i].ReaderExecutingAsync(command, eventData, result, cancellationToken)
.ConfigureAwait(false);
}

return result;
Expand All @@ -121,7 +122,8 @@ public async Task<InterceptionResult<object>> ScalarExecutingAsync(
{
for (var i = 0; i < _interceptors.Length; i++)
{
result = await _interceptors[i].ScalarExecutingAsync(command, eventData, result, cancellationToken);
result = await _interceptors[i].ScalarExecutingAsync(command, eventData, result, cancellationToken)
.ConfigureAwait(false);
}

return result;
Expand All @@ -135,7 +137,8 @@ public async Task<InterceptionResult<int>> NonQueryExecutingAsync(
{
for (var i = 0; i < _interceptors.Length; i++)
{
result = await _interceptors[i].NonQueryExecutingAsync(command, eventData, result, cancellationToken);
result = await _interceptors[i].NonQueryExecutingAsync(command, eventData, result, cancellationToken)
.ConfigureAwait(false);
}

return result;
Expand Down Expand Up @@ -188,7 +191,8 @@ public async Task<DbDataReader> ReaderExecutedAsync(
{
for (var i = 0; i < _interceptors.Length; i++)
{
result = await _interceptors[i].ReaderExecutedAsync(command, eventData, result, cancellationToken);
result = await _interceptors[i].ReaderExecutedAsync(command, eventData, result, cancellationToken)
.ConfigureAwait(false);
}

return result;
Expand All @@ -202,7 +206,8 @@ public async Task<object> ScalarExecutedAsync(
{
for (var i = 0; i < _interceptors.Length; i++)
{
result = await _interceptors[i].ScalarExecutedAsync(command, eventData, result, cancellationToken);
result = await _interceptors[i].ScalarExecutedAsync(command, eventData, result, cancellationToken)
.ConfigureAwait(false);
}

return result;
Expand All @@ -216,7 +221,8 @@ public async Task<int> NonQueryExecutedAsync(
{
for (var i = 0; i < _interceptors.Length; i++)
{
result = await _interceptors[i].NonQueryExecutedAsync(command, eventData, result, cancellationToken);
result = await _interceptors[i].NonQueryExecutedAsync(command, eventData, result, cancellationToken)
.ConfigureAwait(false);
}

return result;
Expand All @@ -235,7 +241,8 @@ public async Task CommandFailedAsync(
{
for (var i = 0; i < _interceptors.Length; i++)
{
await _interceptors[i].CommandFailedAsync(command, eventData, cancellationToken);
await _interceptors[i].CommandFailedAsync(command, eventData, cancellationToken)
.ConfigureAwait(false);
}
}

Expand Down
Loading

0 comments on commit e1c9a3a

Please sign in to comment.