Skip to content
Closed
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
23 changes: 21 additions & 2 deletions .github/workflows/_publish-code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,34 @@ jobs:
steps:
- name: Check out Git repository
uses: actions/checkout@v4

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: '9.0.x'

- name: Restore strong-name key from secret
run: |
echo "${{ secrets.MINDEE_SNK_B64 }}" | base64 -d > /tmp/Mindee.snk
chmod 600 /tmp/Mindee.snk

- name: Install dependencies
run: dotnet restore "src/Mindee"
- name: Build
run: dotnet build "src/Mindee" --configuration Release --no-restore

- name: Build (strong-name signed)
run: dotnet build "src/Mindee" --configuration Release --no-restore -p:MindeeStrongNameKeyFile=/tmp/Mindee.snk

- name: Pack
run: dotnet pack "src/Mindee" -c Release -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg --no-build --output nuget

- name: Publish NuGet packages to NuGet
run: dotnet nuget push nuget/*.nupkg --api-key ${{ secrets.NUGET_KEY }} --source "nuget.org" --skip-duplicate

- name: Verify strong-name (PublicKeyToken)
run: |
sudo apt-get update
sudo apt-get install -y mono-devel unzip
unzip -p nuget/*.nupkg lib/net48/Mindee.dll > /tmp/Mindee.dll
sn -T /tmp/Mindee.dll
sn -vf /tmp/Mindee.dll

2 changes: 1 addition & 1 deletion .github/workflows/_test-units.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
matrix:
dotnet:
- "net472"
- "net482"
- "net48"
- "net6.0"
- "net8.0"
- "net10.0"
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,7 @@ local_test/

# DocFX files
_site

# StrongName files
*.snk
*.snk.b64
12 changes: 10 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<VersionPrefix>3.37.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
<VersionPrefix>3.37.1</VersionPrefix>
<VersionSuffix>strongname</VersionSuffix>
<Authors>Mindee</Authors>
<PackageProjectUrl>https://github.com/mindee/mindee-api-dotnet</PackageProjectUrl>
<RepositoryUrl>https://github.com/mindee/mindee-api-dotnet</RepositoryUrl>
Expand Down Expand Up @@ -40,4 +40,12 @@
<FileAlignment>512</FileAlignment>
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
</PropertyGroup>

<PropertyGroup Condition="'$(MindeeStrongNameKeyFile)' != ''">
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>$(MindeeStrongNameKeyFile)</AssemblyOriginatorKeyFile>
<DelaySign>false</DelaySign>
<PublicSign>false</PublicSign>
<NoWarn>$(NoWarn);CS8002</NoWarn>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@

namespace Mindee.Extensions.DependencyInjection
{
#if !NET6_0_OR_GREATER
/// <summary>
/// Wrapper for V2 RestClient to work around lack of keyed services in .NET Framework
/// </summary>
internal sealed class MindeeV2RestClientWrapper
{
public RestClient Client { get; }

public MindeeV2RestClientWrapper(RestClient client)
{
Client = client;
}
}
#endif

/// <summary>
/// To configure DI.
/// </summary>
Expand Down Expand Up @@ -74,7 +89,11 @@ public static void AddMindeeApiV2(
services.AddSingleton(serviceProvider =>
{
var settings = serviceProvider.GetRequiredService<IOptions<MindeeSettingsV2>>();
#if NET6_0_OR_GREATER
var restClient = serviceProvider.GetRequiredKeyedService<RestClient>("MindeeV2RestClient");
#else
var restClient = serviceProvider.GetRequiredService<MindeeV2RestClientWrapper>().Client;
#endif
var logger = serviceProvider.GetService<ILoggerFactory>()?.CreateLogger<MindeeApiV2>();
return new MindeeApiV2(settings, restClient, logger);
});
Expand Down Expand Up @@ -116,6 +135,7 @@ private static void RegisterV1RestSharpClient(IServiceCollection services, bool

private static void RegisterV2RestSharpClient(IServiceCollection services, bool throwOnError)
{
#if NET6_0_OR_GREATER
services.AddKeyedSingleton("MindeeV2RestClient", (provider, _) =>
{
var settings = provider.GetRequiredService<IOptions<MindeeSettingsV2>>().Value;
Expand Down Expand Up @@ -146,6 +166,40 @@ private static void RegisterV2RestSharpClient(IServiceCollection services, bool
};
return new RestClient(clientOptions);
});
#else
// For .NET Framework, register as a named singleton using a wrapper approach
services.AddSingleton(provider =>
{
var settings = provider.GetRequiredService<IOptions<MindeeSettingsV2>>().Value;
settings.MindeeBaseUrl = Environment.GetEnvironmentVariable("MindeeV2__BaseUrl");
if (string.IsNullOrEmpty(settings.MindeeBaseUrl))
{
settings.MindeeBaseUrl = "https://api-v2.mindee.net";
}

if (settings.RequestTimeoutSeconds <= 0)
{
settings.RequestTimeoutSeconds = 120;
}

if (string.IsNullOrEmpty(settings.ApiKey))
{
settings.ApiKey = Environment.GetEnvironmentVariable("MindeeV2__ApiKey");
}

var clientOptions = new RestClientOptions
{
BaseUrl = new Uri(settings.MindeeBaseUrl),
FollowRedirects = false,
Timeout = TimeSpan.FromSeconds(settings.RequestTimeoutSeconds),
UserAgent = BuildUserAgent(),
Expect100Continue = false,
CachePolicy = new CacheControlHeaderValue { NoCache = true, NoStore = true },
ThrowOnAnyError = throwOnError
};
return new MindeeV2RestClientWrapper(new RestClient(clientOptions));
});
#endif
}

private static string BuildUserAgent()
Expand Down
4 changes: 4 additions & 0 deletions src/Mindee/Http/MindeeApiV2.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Threading.Tasks;
#if NET6_0_OR_GREATER
using Microsoft.Extensions.DependencyInjection;
#endif
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
Expand All @@ -17,7 +19,9 @@ internal sealed class MindeeApiV2 : HttpApiV2

public MindeeApiV2(
IOptions<MindeeSettingsV2> mindeeSettings,
#if NET6_0_OR_GREATER
[FromKeyedServices("MindeeV2RestClient")]
#endif
RestClient httpClient,
ILogger<MindeeApiV2> logger = null)
{
Expand Down
30 changes: 25 additions & 5 deletions src/Mindee/Mindee.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,47 @@
<TargetFrameworks>net472;net48;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
</PropertyGroup>

<!-- Common packages for all frameworks -->
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.6"/>
<PackageReference Include="SkiaSharp" Version="2.88.8"/>
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.8"/>
<PackageReference Include="System.Text.Json" Version="8.0.5"/>
<PackageReference Include="Docnet.Core" Version="2.3.1"/>
<PackageReference Include="RestSharp" Version="112.0.0"/>
</ItemGroup>

<!-- Microsoft.Extensions packages for .NET Framework 4.x -->
<ItemGroup Condition="'$(TargetFramework)' == 'net472' Or '$(TargetFramework)' == 'net48'">
<PackageReference Include="Microsoft.CSharp" Version="4.7.0"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0"/>
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0"/>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.4"/>
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0"/>
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="6.0.0"/>
</ItemGroup>

<!-- Microsoft.Extensions packages for modern .NET -->
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0' Or '$(TargetFramework)' == 'net7.0' Or '$(TargetFramework)' == 'net8.0' Or '$(TargetFramework)' == 'net9.0'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.6"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.6"/>
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.6"/>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.6"/>
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.6"/>
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="9.0.6"/>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net472' Or '$(TargetFramework)' == 'net48'">
<PackageReference Include="Microsoft.CSharp" Version="4.7.0"/>
<!-- InternalsVisibleTo without public key (for normal builds) -->
<ItemGroup Condition="'$(MindeeStrongNameKeyFile)' == ''">
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Mindee.UnitTests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

<ItemGroup>
<!-- InternalsVisibleTo with public key (for strong-name signed builds) -->
<ItemGroup Condition="'$(MindeeStrongNameKeyFile)' != ''">
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Mindee.UnitTests</_Parameter1>
<_Parameter1>Mindee.UnitTests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100d56a9b5be0ad792696d7de8d383af59291fbd83c0d702199974a915d8bd994a79e6f1b72e4bfec19277bca659087a2424391eb12d96584394fb5df0c1d5b9eb06d114f06f0f040039933a6a80bb9ac2adaf96fb98c3679224a21ef359a4954616fc4924fa04595abe2d01f6125a1c40f0ff69e8ba7b849af8fbedca91a452b9e</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

Expand Down
Loading