72 lines
3.2 KiB
Markdown
72 lines
3.2 KiB
Markdown
# Service Base Classes
|
|
|
|
> Part of `Mango.Nop.Services`. See `Mango.Nop.Services/README.md` for project overview.
|
|
> For logging bridge see `docs/LOGGING/README.md`.
|
|
|
|
## MgBackgroundServiceBase
|
|
|
|
Abstract hosted background service. Inherits `Microsoft.Extensions.Hosting.BackgroundService`.
|
|
|
|
**Constructor:** `(ILogger, IServiceProvider, int executeIntervalMs)`
|
|
|
|
| Feature | Detail |
|
|
|---|---|
|
|
| Loop pattern | `ExecuteAsync` loops: `Task.Delay(ExecuteIntervalMs)` -> `OnExecuteAsync()`, with pause support |
|
|
| `OnExecuteAsync(CancellationToken)` | Abstract — subclass implements the actual work |
|
|
| `Pause(bool)` | Pauses/resumes the loop without stopping the service |
|
|
| `ExecuteIntervalMs` | Configurable interval between iterations |
|
|
| Exception handling | Catches and logs errors per iteration, never crashes the loop |
|
|
| Logging | Uses nopCommerce `Nop.Services.Logging.ILogger` (**not** `Mango.Nop.Core.Loggers.ILogger`) |
|
|
|
|
**Interface:** `IMgBackgroundService : IHostedService, IDisposable`
|
|
|
|
## MgSessionServiceBase\<TSessionItem\>
|
|
|
|
In-memory session management using `ConcurrentDictionary<string, TSessionItem>`.
|
|
|
|
| Method | Signature | Purpose |
|
|
|---|---|---|
|
|
| `GetOrCreateSessionItem` | `(string sessionId) -> TSessionItem?` | Get existing or create new via `Activator.CreateInstance` |
|
|
| `TryAddSessionItem` | `(TSessionItem) -> bool` | Add if not exists, throws if duplicate |
|
|
| `TryGetSessionItem` | `(string sessionId, out TSessionItem) -> bool` | Try-pattern lookup |
|
|
| `TryRemoveSessionItem` | `(string sessionId, out TSessionItem) -> bool` | Remove and return |
|
|
| `TryGetSessionItemBySignlaRConnectionId` | `(string connectionId, out TSessionItem?) -> bool` | Find session by SignalR connection |
|
|
| `Count` | `() -> int` | Active session count |
|
|
|
|
**Interface:** `IMgSessionService<TSessionItem> where TSessionItem : IMgSessionItem`
|
|
|
|
## MgSessionItemBase
|
|
|
|
Base session item. Primary constructor: `(string sessionKey)`.
|
|
|
|
| Property | Type | Purpose |
|
|
|---|---|---|
|
|
| `SessionId` | `string` | Unique session identifier |
|
|
| `SignaRConnectionId` | `string?` | Associated SignalR connection ID |
|
|
| `RequestCount` | `int` | Request counter |
|
|
|
|
**Interface:** `IMgSessionItem` — `SessionId`, `SignaRConnectionId`, `RequestCount`
|
|
|
|
## MgEventConsumerBase
|
|
|
|
Abstract nopCommerce event consumer. Subscribes to:
|
|
|
|
| Event | Handler |
|
|
|---|---|
|
|
| `EntityUpdatedEvent<Product>` | `HandleEventAsync(...)` — virtual, empty default |
|
|
| `EntityInsertedEvent<Product>` | `HandleEventAsync(...)` — virtual, empty default |
|
|
| `CustomerRegisteredEvent` | `HandleEventAsync(...)` — virtual, empty default |
|
|
| `OrderPlacedEvent` | `HandleEventAsync(...)` — virtual, empty default |
|
|
| `PageRenderingEvent` | `HandleEventAsync(...)` — virtual, empty default |
|
|
| `ProductSearchEvent` | `HandleEventAsync(...)` — virtual, empty default |
|
|
|
|
Built-in helper: `CheckAndUpdateProductManageInventoryMethodToManageStock(Product)` — ensures product has `ManageStock` inventory method.
|
|
|
|
**Constructor:** `(IMgDbContextBase ctx, IHttpContextAccessor, IEnumerable<IAcLogWriterBase> logWriters)`
|
|
|
|
## MgLockServiceBase
|
|
|
|
Simple in-process lock using `SemaphoreSlim(1)`. Implements `IMgLockService` (defined in `Mango.Nop.Core.Services`).
|
|
|
|
Used by `MgDbContextBase.TransactionSafe*` variants for global serialization.
|