Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ jobs:
8.x
9.x
10.x
11.x

- name: Setup NuGet
uses: nuget/setup-nuget@v2

- name: Restore NuGet packages
run: nuget restore OpenAC.Net.DFe.Core.sln
run: dotnet restore OpenAC.Net.DFe.Core.sln
working-directory: src

- name: Build solution
Expand Down
49 changes: 49 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Pull Request CI

on:
pull_request:

permissions:
contents: read

defaults:
run:
working-directory: src

jobs:
validate:
name: Build, Test and Pack
runs-on: windows-latest
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false

- name: Setup .NET
uses: actions/setup-dotnet@v6
with:
dotnet-version: |
8.x
9.x
10.x
11.x

- name: Build solution
run: dotnet build OpenAC.Net.DFe.Core.sln --configuration Release -p:GeneratePackageOnBuild=false

- name: Run tests on .NET 8
run: dotnet run --project OpenAC.Net.DFe.Core.Tests/OpenAC.Net.DFe.Core.Tests.csproj --configuration Release --framework net8.0 --no-build

- name: Run tests on .NET 11
run: dotnet run --project OpenAC.Net.DFe.Core.Tests/OpenAC.Net.DFe.Core.Tests.csproj --configuration Release --framework net11.0 --no-build

- name: Pack NuGet package
run: dotnet pack OpenAC.Net.DFe.Core/OpenAC.Net.DFe.Core.csproj --configuration Release --no-build --output ../nuget

- name: Upload NuGet package
uses: actions/upload-artifact@v7
with:
name: nuget
if-no-files-found: error
retention-days: 7
path: nuget/*.nupkg
6 changes: 2 additions & 4 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ jobs:
8.x
9.x
10.x
11.x

- name: Setup NuGet
uses: nuget/setup-nuget@v2

- name: Restore NuGet packages
run: nuget restore OpenAC.Net.DFe.Core.sln
run: dotnet restore OpenAC.Net.DFe.Core.sln
working-directory: src

- name: Build solution
Expand Down
11 changes: 9 additions & 2 deletions src/OpenAC.Net.DFe.Core.Tests/Http/DFeHttpTransportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,24 @@ public async Task CookiesAreNotSharedBetweenOperations()
}

[Test]
#pragma warning disable SYSLIB0014 // Verifica que o transporte não altera a configuração TLS global legada.
public async Task TimeoutUsesTheWholeTimeSpanAndDoesNotChangeGlobalTls()
{
using var server = new LocalServer(_ => 200, TimeSpan.FromMilliseconds(1100));
var config = new HttpTestConfiguration();
config.WebServices.TimeOut = TimeSpan.FromMilliseconds(1800);
var config = new HttpTestConfiguration
{
WebServices =
{
TimeOut = TimeSpan.FromMilliseconds(1800)
}
};
var originalProtocols = ServicePointManager.SecurityProtocol;
using var request = new HttpRequestMessage(HttpMethod.Get, server.Url);
using var response = await DFeHttpTransport.Instance.SendAsync(request, new(config.WebServices));
await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK);
await Assert.That(ServicePointManager.SecurityProtocol).IsEqualTo(originalProtocols);
}
#pragma warning restore SYSLIB0014

[Test]
public async Task TimeoutCancelsResponseRead()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>net8.0;net11.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private static TesteXml GenerateXml()
TesteEnum1 = TesteEnum.Value1,
TesteEnum2 = null,
TestNullInt = 999,
TestDateTz = new DateTimeOffset(DateTime.Now, TimeSpan.FromHours(-4))
TestDateTz = new DateTimeOffset(2026, 1, 15, 12, 0, 0, TimeSpan.FromHours(-4))
};

var cdata = File.ReadAllText("cdata_teste.xml");
Expand Down
5 changes: 1 addition & 4 deletions src/OpenAC.Net.DFe.Core/Http/DFeHttpClientPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ private static Entry CreateEntry(DFeHttpTransportOptions options)
if (certificate != null)
handler.SslOptions.ClientCertificates = new X509CertificateCollection { certificate };
#else
#if NETFRAMEWORK
// As referências do .NET 4.6.2 não expõem o setter presente em runtimes mais recentes.
typeof(HttpClientHandler).GetProperty("SslProtocols")?.SetValue(handler, options.Protocols);
#else
#if !NETFRAMEWORK
handler.SslProtocols = options.Protocols;
#endif
if (!options.ValidateServerCertificate)
Expand Down
30 changes: 28 additions & 2 deletions src/OpenAC.Net.DFe.Core/Http/DFeHttpTransport.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
#if NETFRAMEWORK
using System.Net;
#endif
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -8,6 +11,10 @@ namespace OpenAC.Net.DFe.Core.Http;
/// <summary>Transporte desktop com pool por certificado, TLS, proxy, perfil e host.</summary>
public class DFeHttpTransport : DFeHttpTransportBase
{
#if NETFRAMEWORK
private static readonly SemaphoreSlim tlsLock = new(1, 1);
#endif

/// <summary>Instância compartilhada para consumidores sem injeção de dependências.</summary>
public static DFeHttpTransport Instance { get; } = new();

Expand All @@ -18,9 +25,28 @@ protected override async Task<HttpResponseMessage> SendCoreAsync(HttpRequestMess
if (options.Certificate != null)
Common.DFeCertificadosConfigBase.ValidarCertificado(options.Certificate, options.MargemValidadeCertificado);
using var lease = DFeHttpClientPool.Instance.Acquire(options, request.RequestUri!);

// O empréstimo permanece ativo até terminar o corpo, inclusive se a entrada expirar.
return await lease.Pipeline.ExecuteAsync(async token =>
await lease.Client.SendAsync(request, HttpCompletionOption.ResponseContentRead, token).ConfigureAwait(false),
cancellationToken).ConfigureAwait(false);
{
#if NETFRAMEWORK
// A configuração é global. Evita que envios do transporte sobrescrevam o TLS uns dos outros.
await tlsLock.WaitAsync(token).ConfigureAwait(false);
var previousProtocols = ServicePointManager.SecurityProtocol;
try
{
ServicePointManager.SecurityProtocol = (SecurityProtocolType)options.Protocols;
#endif
return await lease.Client.SendAsync(request, HttpCompletionOption.ResponseContentRead, token)
.ConfigureAwait(false);
#if NETFRAMEWORK
}
finally
{
ServicePointManager.SecurityProtocol = previousProtocols;
tlsLock.Release();
}
#endif
}, cancellationToken).ConfigureAwait(false);
}
}
12 changes: 6 additions & 6 deletions src/OpenAC.Net.DFe.Core/OpenAC.Net.DFe.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net462;net48;netstandard2.0;net8.0;net9.0;net10.0</TargetFrameworks>
<TargetFrameworks>net462;net48;netstandard2.0;net8.0;net9.0;net10.0;net11.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Nullable>annotations</Nullable>
<AssemblyName>OpenAC.Net.DFe.Core</AssemblyName>
Expand All @@ -21,10 +21,10 @@
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageProjectUrl>https://openac.net.br/</PackageProjectUrl>
<PackageVersion>1.8.0</PackageVersion>
<AssemblyVersion>1.8.0</AssemblyVersion>
<FileVersion>1.8.0</FileVersion>
<Version>1.8.0</Version>
<PackageVersion>1.8.1</PackageVersion>
<AssemblyVersion>1.8.1</AssemblyVersion>
<FileVersion>1.8.1</FileVersion>
<Version>1.8.1</Version>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<DebugType>embedded</DebugType>
Expand Down Expand Up @@ -80,7 +80,7 @@
</None>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'net8.0' or '$(TargetFramework)' == 'net9.0' or '$(TargetFramework)' == 'net10.0'">
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'net8.0' or '$(TargetFramework)' == 'net9.0' or '$(TargetFramework)' == 'net10.0' or '$(TargetFramework)' == 'net11.0'">
<PackageReference Include="System.Drawing.Common" Version="10.0.14" />
<PackageReference Include="System.Security.Cryptography.Xml" Version="10.0.14" />
</ItemGroup>
Expand Down