Compare commits

...

24 Commits

Author SHA1 Message Date
Developer 02
9628b46ba0 Add project description to ReC.Client.csproj
A `<Description>` tag was added to the `ReC.Client.csproj` file.
This tag describes the project as a client library for interacting
with the ReC.API, offering typed HTTP access and DI integration.
This change improves project metadata for better documentation
and package management.
2025-12-06 00:43:30 +01:00
Developer 02
1f8142852e Add packaging metadata and solution items project
Added a new "Solution Items" project to the solution file, including an `assets/icon.png` file. Updated `ReC.Client.csproj` with packaging metadata such as `PackageId`, `Authors`, `Company`, and `Version`. Included `icon.png` in the package and updated `<TargetFrameworks>` to support `net8.0`. Added the `icon.png` file to the project.
2025-12-06 00:41:48 +01:00
Developer 02
bdd78be66c Add static BuildStaticClient method with Obsolete warning
A new static method `BuildStaticClient(Action<HttpClient> configureClient)`
was added to the `ReC.Client` namespace in `ReCClient.cs`. This method
configures and builds a static `IServiceProvider` for creating `ReCClient`
instances. It includes XML documentation detailing its purpose, usage,
and parameters, and warns that it should only be called once during
application startup.

The method accepts an `Action<HttpClient>` parameter for `HttpClient`
configuration and throws an `InvalidOperationException` if the static
provider is already built. It is marked `[Obsolete]` to encourage the
use of a local service collection instead of the static provider.

Additionally, the XML documentation for the `ReCClient` creation method
was updated to reference the new `BuildStaticClient` method.
2025-12-06 00:13:50 +01:00
Developer 02
470902911e Refactor HTTP client setup in BuildStaticClient
Simplified the HTTP client configuration in the `BuildStaticClient` method of the `ReC.Client` namespace. Replaced the explicit use of `Services.AddHttpClient` with a call to `Services.AddRecClient(apiUri)`. This change improves code readability and reusability by encapsulating the HTTP client setup logic in a dedicated method or extension.
2025-12-06 00:12:36 +01:00
Developer 02
3f7ebdb632 Simplify ReCClient instantiation in Create method
Refactored the `Create` method in the `ReCClient` class to directly resolve a `ReCClient` instance from the dependency injection container using `Provider.GetRequiredService<ReCClient>()`.

Removed the intermediate step of retrieving an `IHttpClientFactory` and manually creating a `ReCClient` object. This change reduces boilerplate code and assumes `ReCClient` is already registered in the container, improving maintainability.
2025-12-06 00:10:50 +01:00
Developer 02
23ef1a5797 Add scoped ReCClient and update project dependencies
Added `services.AddScoped<ReCClient>()` to both `AddRecClient`
method overloads in `DependencyInjection.cs` to ensure proper
scoped registration of `ReCClient`.

Updated `ReC.Client.csproj` to include necessary package
references for dependency injection and HTTP client support:
- `Microsoft.Extensions.DependencyInjection` (v10.0.0)
- `Microsoft.Extensions.Http` (v8.0.0)
- `System.Net.Http` (v4.3.4)
2025-12-06 00:10:08 +01:00
Developer 02
4a7f2a41fa Mark static provider methods as obsolete
The `BuildStaticClient` and `Create` methods in the `ReC.Client` namespace have been marked with the `[Obsolete]` attribute. These methods now include a message advising developers to use a local service collection instead of the static provider. This change serves as a warning that these methods are outdated and may be removed in future versions, encouraging a transition to a more modern design.
2025-12-06 00:02:30 +01:00
Developer 02
5f9e716ca6 Enhance ReC.Client library with new features and cleanup
- Added comprehensive XML documentation for `DependencyInjection`
  and `ReCClient` classes to improve code readability.
- Introduced overload for `AddRecClient` to allow flexible
  `HttpClient` configuration.
- Added static `BuildStaticClient` and `Create` methods for
  simplified client instantiation.
- Marked synchronous `InvokeRecAction` method as obsolete to
  encourage asynchronous usage.
- Updated `ReC.Client.csproj`:
  - Added `<DocumentationFile>` property for XML doc generation.
  - Downgraded `Microsoft.Extensions.Http` to version 8.0.0.
  - Removed `System.Text.Json` package reference.
- Removed `System.Text.Json` dependency from `ReCClient.cs`.
- Generated unique `HttpClient` name for `ReCClient` instances.
- Performed general code cleanup and improved method remarks.
2025-12-05 23:59:11 +01:00
Developer 02
91c8b98f44 Refactor ReCClient.Static to Create() method
Replaced the `ReCClient.Static` property with a `ReCClient.Create()` method to improve clarity and align with best practices. The new method retains the same functionality, including throwing an `InvalidOperationException` if the `Provider` is not built. The logic for retrieving the `IHttpClientFactory` and creating a `ReCClient` instance remains unchanged.
2025-12-05 23:54:16 +01:00
Developer 02
10fc56b262 Refactor: Rename ServiceProvider to Provider
Renamed the static field `ServiceProvider` to `Provider` across
the `ReC.Client` namespace in `ReCClient.cs` for consistency
and clarity. Updated all occurrences, including declarations,
usages, and exception messages. Adjusted conditional compilation
blocks to reflect the new name. Ensured the `BuildStaticClient`
method and `Static` property use the renamed field appropriately.
2025-12-05 23:52:25 +01:00
Developer 02
71368e5c85 Refactor ServiceProvider with conditional compilation
Updated the `ServiceProvider` field in the `ReC.Client` namespace to use conditional compilation for framework-specific behavior:
- Added `NET8_0_OR_GREATER` directive to define `ServiceProvider` as nullable (`IServiceProvider?`) for .NET 8.0 or greater.
- Retained non-nullable `IServiceProvider` for other frameworks.
- Removed redundant `#if nullable` directives to simplify the code.
- Streamlined initialization logic for better clarity and maintainability.
2025-12-05 23:51:03 +01:00
Developer 02
fb649a5c68 Add static DI-based ReCClient initialization
Introduced dependency injection to the `ReCClient` class by adding `Microsoft.Extensions.DependencyInjection`. Added a static mechanism for creating and managing a `ReCClient` instance, including a `BuildStaticClient` method to configure the HTTP client and a `Static` property to retrieve the client. Implemented error handling to ensure proper initialization of the static service provider.
2025-12-05 23:47:28 +01:00
Developer 02
f4abac1103 Mark InvokeRecAction as obsolete with guidance
Added the `[Obsolete]` attribute to the `InvokeRecAction` method
in the `ReC.Client` namespace to discourage its use. The attribute
recommends using the `InvokeRecActionAsync` method instead to
avoid potential deadlocks and improve performance.
2025-12-05 23:39:22 +01:00
Developer 02
4c8e9be695 Add synchronous InvokeRecAction method
Added a synchronous version of the `InvokeRecAction` method to
the `ReC.Client` namespace in `ReCClient.cs`. This method
invokes the `POST api/RecAction/invoke/{profileId}` endpoint
synchronously using `GetAwaiter().GetResult()` and returns a
boolean indicating success.

Also included XML documentation comments for the new method,
detailing its purpose, parameters, and return value.
2025-12-05 23:37:53 +01:00
Developer 02
b190e4f5e9 Update InvokeRecActionAsync to return success status
The method `InvokeRecActionAsync` in the `ReC.Client` namespace was updated to return a `bool` instead of `Task`. This change allows the method to indicate whether the HTTP request was successful.

- Updated the return type from `Task` to `Task<bool>`.
- Modified the `<returns>` XML documentation to reflect the new behavior.
- Replaced `resp.EnsureSuccessStatusCode()` with `resp.IsSuccessStatusCode` to return the success status directly.
2025-12-05 23:36:45 +01:00
Developer 02
4a1b221478 Rename 'ct' to 'cancellationToken' for clarity
Updated the parameter name in the `InvokeRecActionAsync` method
to `cancellationToken` for better readability and alignment
with standard naming conventions. Adjusted the method signature
and internal usage to reflect this change.
2025-12-05 23:31:17 +01:00
Developer 02
475acc0a56 Remove InvokeRecActionFakeAsync method
Removed the `InvokeRecActionFakeAsync` method, which was used to invoke a fake RecAction via the `api/RecAction/invoke/fake` endpoint. This method is no longer needed or relevant.

The `InvokeRecActionAsync` method remains in place to handle RecActions for specific profile IDs via the `api/RecAction/invoke/{profileId}` endpoint.
2025-12-05 23:29:43 +01:00
Developer 02
f8dd16454f Add AddRecClient method and conditional using directive
Added a `using System.Net.Http;` directive within a `#if NETFRAMEWORK` block in `DependencyInjection.cs` to ensure compatibility with .NET Framework.

Introduced the `AddRecClient` extension method in the `DependencyInjection` class to enable custom configuration of `HttpClient` instances via an `Action<HttpClient>` delegate.
2025-12-05 23:24:44 +01:00
Developer 02
8cad1df95d Add DependencyInjection support for ReCClient
Introduced a `DependencyInjection` class in the `ReC.Client`
namespace to enable dependency injection for `ReCClient`.

Added an `AddRecClient` extension method for `IServiceCollection`
to register an `HttpClient` with a configurable `apiUri` as the
base address.

Included conditional `using System;` for .NET Framework
compatibility and added `Microsoft.Extensions.DependencyInjection`
to support DI services.
2025-12-05 23:23:17 +01:00
Developer 02
4ee79d6cd8 Refactor ReCClient for cleaner and more flexible design
- Removed unused `System.Xml.Linq` namespace import.
- Simplified code by removing `#if NETFRAMEWORK` directives.
- Added `ClientName` field to uniquely identify HTTP clients.
- Updated constructor to use named HTTP client with `ClientName`.
- Removed unused `_jsonOptions` field for better code clarity.
2025-12-05 23:17:45 +01:00
Developer 02
c0c0650fee Refactor ReCClient to use IHttpClientFactory
Updated ReCClient to use IHttpClientFactory for improved
resource management and testability. Added package
references for `Microsoft.Extensions.Http`, `System.Net.Http`,
and `System.Text.Json` to support HTTP client functionality
and JSON serialization. Included `System.Xml.Linq` for
potential XML-related operations. Updated constructor logic
to create `HttpClient` instances via the factory pattern.
2025-12-05 23:11:35 +01:00
Developer 02
73059d4554 Add InvokeRecActionFakeAsync method to ReCClient
A new asynchronous method `InvokeRecActionFakeAsync` was added to the `ReC.Client` namespace in `ReCClient.cs`. This method sends a POST request to the `api/RecAction/invoke/fake` endpoint. It includes XML documentation comments and accepts an optional `CancellationToken` parameter. The method ensures the response has a successful status code.
2025-12-05 23:03:03 +01:00
Developer 02
e774afc85e Add InvokeRecActionAsync method to ReCClient class
Added the `InvokeRecActionAsync` method to the `ReCClient` class in the `ReC.Client` namespace. This method performs an asynchronous HTTP POST request to the `api/RecAction/invoke/{profileId}` endpoint. It accepts a `profileId` parameter to specify the profile ID and an optional `CancellationToken` for task cancellation. The method ensures the HTTP response indicates success by calling `EnsureSuccessStatusCode()`. Included XML documentation comments for clarity and maintainability.
2025-12-05 23:02:45 +01:00
Developer 02
1fc4570210 Add ReCClient class and update project dependencies
Added `ReCClient` class to handle HTTP requests with JSON
serialization/deserialization support. Updated `ReC.Client.csproj`
to include `System.Net.Http` (v4.3.4) and `System.Text.Json`
(v9.0.11) as package references. Introduced conditional `using`
directives for `NETFRAMEWORK` compatibility.
2025-12-05 22:45:19 +01:00
5 changed files with 196 additions and 0 deletions

View File

@ -15,6 +15,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReC.Application", "src\ReC.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReC.Client", "src\ReC.Client\ReC.Client.csproj", "{DA3A6BDD-8045-478F-860B-D1F0EB97F02B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8EC462FD-D22E-90A8-E5CE-7E832BA40C5D}"
ProjectSection(SolutionItems) = preProject
assets\icon.png = assets\icon.png
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU

BIN
assets/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

View File

@ -0,0 +1,41 @@
using Microsoft.Extensions.DependencyInjection;
#if NETFRAMEWORK
using System;
using System.Net.Http;
#endif
namespace ReC.Client
{
/// <summary>
/// Provides extension methods for setting up the ReC client in an <see cref="IServiceCollection"/>.
/// </summary>
public static class DependencyInjection
{
/// <summary>
/// Adds and configures the <see cref="HttpClient"/> for the <see cref="ReCClient"/> to the specified <see cref="IServiceCollection"/>
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
/// <param name="apiUri">The base URI of the ReC API.</param>
/// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns>
public static IHttpClientBuilder AddRecClient(this IServiceCollection services, string apiUri)
{
services.AddScoped<ReCClient>();
return services.AddHttpClient(ReCClient.ClientName, client =>
{
client.BaseAddress = new Uri(apiUri);
});
}
/// <summary>
/// Adds and configures the <see cref="HttpClient"/> for the <see cref="ReCClient"/> to the specified <see cref="IServiceCollection"/>
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
/// <param name="configureClient">An action to configure the <see cref="HttpClient"/>.</param>
/// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns>
public static IHttpClientBuilder AddRecClient(this IServiceCollection services, Action<HttpClient> configureClient)
{
services.AddScoped<ReCClient>();
return services.AddHttpClient(ReCClient.ClientName, configureClient);
}
}
}

View File

@ -2,10 +2,36 @@
<PropertyGroup>
<TargetFrameworks>net462;net8.0</TargetFrameworks>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(MSBuildProjectName).xml</DocumentationFile>
<PackageId>ReC.Client</PackageId>
<Authors>Digital Data GmbH</Authors>
<Company>Digital Data GmbH</Company>
<Product>ReC.Client</Product>
<Copyright>Copyright 2025</Copyright>
<PackageIcon>icon.png</PackageIcon>
<RepositoryUrl>http://git.dd:3000/AppStd/Rec.git</RepositoryUrl>
<PackageTags>digital data rec api</PackageTags>
<Version>1.0.0-beta</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<Description>Client-Bibliothek für die Interaktion mit der ReC.API, die typisierten HTTP-Zugriff und DI-Integration bietet.</Description>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' != 'net462'">
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Include="..\..\assets\icon.png">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>
</Project>

124
src/ReC.Client/ReCClient.cs Normal file
View File

@ -0,0 +1,124 @@
using Microsoft.Extensions.DependencyInjection;
#if NETFRAMEWORK
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
#endif
namespace ReC.Client
{
/// <summary>
/// A client for interacting with the ReC API.
/// </summary>
public class ReCClient
{
private readonly HttpClient _http;
/// <summary>
/// A unique name for the HttpClient used by the ReCClient.
/// </summary>
public static readonly string ClientName = Guid.NewGuid().ToString();
/// <summary>
/// Initializes a new instance of the <see cref="ReCClient"/> class.
/// </summary>
/// <param name="httpClientFactory">The factory to create HttpClients.</param>
public ReCClient(IHttpClientFactory httpClientFactory)
{
_http = httpClientFactory.CreateClient(ClientName);
}
/// <summary>
/// Asynchronously invokes a ReC action for a specific profile.
/// </summary>
/// <remarks>
/// This method sends a POST request to the <c>api/RecAction/invoke/{profileId}</c> endpoint.
/// </remarks>
/// <param name="profileId">The ID of the profile to invoke the action for.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>A <see cref="Task{TResult}"/> that represents the asynchronous operation. The task result is <see langword="true"/> if the request was successful; otherwise, <see langword="false"/>.</returns>
public async Task<bool> InvokeRecActionAsync(int profileId, CancellationToken cancellationToken = default)
{
var resp = await _http.PostAsync($"api/RecAction/invoke/{profileId}", content: null, cancellationToken);
return resp.IsSuccessStatusCode;
}
/// <summary>
/// Synchronously invokes a ReC action for a specific profile.
/// </summary>
/// <remarks>
/// This method sends a POST request to the <c>api/RecAction/invoke/{profileId}</c> endpoint.
/// This is the synchronous version of <see cref="InvokeRecActionAsync(int, CancellationToken)"/>.
/// </remarks>
/// <param name="profileId">The ID of the profile to invoke the action for.</param>
/// <returns><see langword="true"/> if the request was successful; otherwise, <see langword="false"/>.</returns>
[Obsolete("Use InvokeRecActionAsync instead to avoid potential deadlocks and improve performance.")]
public bool InvokeRecAction(int profileId)
{
var resp = _http.PostAsync($"api/RecAction/invoke/{profileId}", content: null).GetAwaiter().GetResult();
return resp.IsSuccessStatusCode;
}
#region Static
private static readonly IServiceCollection Services = new ServiceCollection();
#if NET8_0_OR_GREATER
private static IServiceProvider? Provider = null;
#else
private static IServiceProvider Provider = null;
#endif
/// <summary>
/// Configures and builds the static <see cref="IServiceProvider"/> for creating <see cref="ReCClient"/> instances.
/// </summary>
/// <remarks>
/// This method should only be called once during application startup.
/// </remarks>
/// <param name="apiUri">The base URI of the ReC API.</param>
/// <exception cref="InvalidOperationException">Thrown if the static provider has already been built.</exception>
[Obsolete("Use a local service collection instead of the static provider.")]
public static void BuildStaticClient(string apiUri)
{
if(Provider != null)
throw new InvalidOperationException("Static Provider is already built.");
Services.AddRecClient(apiUri);
Provider = Services.BuildServiceProvider();
}
/// <summary>
/// Configures and builds the static <see cref="IServiceProvider"/> for creating <see cref="ReCClient"/> instances.
/// </summary>
/// <remarks>
/// This method should only be called once during application startup.
/// </remarks>
/// <param name="configureClient">An action to configure the <see cref="HttpClient"/>.</param>
/// <exception cref="InvalidOperationException">Thrown if the static provider has already been built.</exception>
[Obsolete("Use a local service collection instead of the static provider.")]
public static void BuildStaticClient(Action<HttpClient> configureClient)
{
if (Provider != null)
throw new InvalidOperationException("Static Provider is already built.");
Services.AddRecClient(configureClient);
Provider = Services.BuildServiceProvider();
}
/// <summary>
/// Creates a new <see cref="ReCClient"/> instance using the statically configured provider.
/// </summary>
/// <returns>A new instance of the <see cref="ReCClient"/>.</returns>
/// <exception cref="InvalidOperationException">Thrown if <see cref="BuildStaticClient(string)"/> has not been called yet.</exception>
[Obsolete("Use a local service collection instead of the static provider.")]
public static ReCClient Create()
{
if (Provider == null)
throw new InvalidOperationException("Static Provider is not built. Call BuildStaticClient first.");
return Provider.GetRequiredService<ReCClient>();
}
#endregion
}
}