Project-Level Service Integration
Files Referenced in this Document
Table of Contents
- Introduction
- Project Service Architecture
- Lifecycle Management
- Variable Monitoring and Event Handling
- Business Logic Coordination
- Best Practices
- Conclusion
Introduction
MyPluginNameProjectService is the core entry point for project-level business logic in the CMS plugin. It inherits from BaseProjectService and provides unified lifecycle management, variable monitoring, and cross-module coordination capabilities for the plugin. This document details its design principles and implementation methods, helping developers understand how to extend project-level functionalities.
Project Service Architecture
The project service acts as a bridge connecting the underlying infrastructure (like variable channels, databases) and upper-layer business logic (like external APIs, UI notifications).
Diagram sources
Lifecycle Management
The MyPluginNameProjectService manages its lifecycle by overriding base class methods, ensuring correct initialization and release of resources.
Start (StartAsync)
The StartAsync method is called when the plugin starts. Main tasks include:
- Initialize Logger: Obtain logger instance.
- Create Variable Channel Listener: Use
IVariableChannelFactoryto create listeners for variable changes. - Subscribe to Events: Subscribe to
TagChangedevents to respond to variable updates. - Start Services: Call
_channelListener.StartAsync()to start monitoring.
public override async Task StartAsync(CancellationToken cancellationToken)
{
// ... Initialization code ...
_channelListener = _variableChannelFactory.CreateListener();
_channelListener.TagChanged += OnTagValueChanged;
await _channelListener.StartAsync(cancellationToken);
// ...
}
Stop (StopAsync)
The StopAsync method is called when the plugin stops. Main tasks include:
- Stop Services: Call
_channelListener.StopAsync(). - Unsubscribe: Unsubscribe from
TagChangedevents. - Resource Release: Dispose of
_channelListenerand other resources.
Section sources
Variable Monitoring and Event Handling
One of the core functions of the project service is to monitor system variable changes and trigger corresponding business logic.
Listener Configuration
Listeners are created via IVariableChannelFactory, supporting monitoring of all variables or specific variable sets.
Event Handling Logic
The OnTagValueChanged method is the callback for variable changes. It receives TagValueChangedEventArgs, containing the variable name and new value.
- Filter: Filter based on
e.Name(e.g., check if it is an "OEE" variable). - Process: Execute specific business logic, such as data calculation, state update.
- Coordinate: Call external APIs or update databases.
private async Task OnTagValueChanged(object sender, TagValueChangedEventArgs e)
{
// Filter logic
if (e.Name == "OEE")
{
// Business processing
}
}
Section sources
Business Logic Coordination
MyPluginNameProjectService serves as a "coordinator", orchestrating different service components to complete complex business processes.
Coordinating External API Calls
As shown in the ExecuteExternalApiAsync method, the project service resolves IMyPluginNameExternalApi from the service provider and calls external interfaces. It handles parameter construction, exception capture, and logging.
Coordinating Data Persistence
Although not explicitly shown in the snippets, the project service can inject Repositories to perform database CRUD operations, persisting variable change records or business operation results.
Authentication Status
The service declares AuthRequired => true, indicating that accessing this service (if exposed as an entry point) or operations performed by it may require specific permission contexts.
Section sources
Best Practices
- Exception Handling: Ensure all asynchronous operations (especially event handlers) have try-catch blocks to prevent background thread exceptions from crashing the application.
- Resource Management: Strictly pair
StartAsyncandStopAsyncto ensure no resource leaks (like unclosed handles or unreleased listeners). - Dependency Injection: Obtain dependent services via
IServiceProvideror constructor injection, following the dependency inversion principle. - Logging: Record key operation steps and error details to facilitate troubleshooting.
- Single Responsibility: Keep the project service focused on coordination and lifecycle management; complex business logic should be delegated to specific Domain Services.
Conclusion
MyPluginNameProjectService provides a standardized entry point for project-level extensions in the CMS plugin. Through correct implementation of lifecycle methods and event handling mechanisms, developers can easily integrate variable monitoring, external communication, and data processing capabilities, building robust and scalable plugin applications.