mrdave1999 / cplugin.net Goto Github PK
View Code? Open in Web Editor NEWA simple library that helps to implement a plugin-based architecture
Home Page: https://mrdave1999.github.io/CPlugin.Net
License: MIT License
A simple library that helps to implement a plugin-based architecture
Home Page: https://mrdave1999.github.io/CPlugin.Net
License: MIT License
Implementations:
This exception occurs when the Plugins key does not exist in a configuration file as a json or .env.
This behavior should be changed for two reasons:
Plugins
key found in a configuration file and that's it.Plugins
section.If the consumer forgets to add the Plugins
key in the configuration file, then the expected behavior is that no plugins are loaded. This is not a bug.
This class allows to search for the subtypes that implement the contract from a plug-in.
It contains only one method:
public static class TypeFinder
{
static IEnumerable<TSupertype> FindSubtypesOf<TSupertype>();
}
This method will search for classes that implement the contract specified by TSupertype
(a generic parameter) and returns the instances that implement the contract.
Code:
CPlugin.Net/src/Attributes/PluginAttribute.cs
Lines 53 to 57 in a2bc8f4
[assembly: Plugin(typeof(TestService))]
namespace MyPluginProject;
public class TestService(ILogger<TestService> logger) : ITestService
{
public int Print()
{
logger.LogInformation("TestService Plugin");
return 0;
}
}
namespace MyHostApplication;
public class PluginStartup
{
public void ConfigureServices(WebApplicationBuilder builder)
{
var envConfiguration = new CPluginEnvConfiguration();
PluginLoader.Load(envConfiguration);
var testServices = TypeFinder.FindSubtypesOf<ITestService>();
foreach (ITestService testService in testServices)
testService.Print();
}
}
If the host application is executed, the code will fail with an exception, because the method called FindSubtypesOf uses Activator.CreateInstance and the TestService type is required to have a constructor without parameters in order to create the instance.
Use the Microsoft.Extensions.DependencyInjection package to create the instances and resolve the dependencies of a service.
This solves the limitation of TypeFinder.FindSubtypesOf.
public class PluginStartup
{
public void ConfigureServices(WebApplicationBuilder builder)
{
var envConfiguration = new CPluginEnvConfiguration();
PluginLoader.Load(envConfiguration);
builder
.Services
.AddSubtypesOf<ITestService>(ServiceLifetime.Transient);
}
public void Configure(IApplicationBuilder app)
{
var testServices = app
.Services
.GetServices<ITestService>();
foreach (ITestService testService in testServices)
testService.Print();
}
}
Given the following appsettings.json
:
{
"Plugins": [ ]
}
GetPluginFiles method throws an InvalidOperationException even though the section exists.
The exception should not happen because in reality the "section" does exist in the json file.
It is not necessary to interrupt the consumer's application for this case. The library can check if the file name has the extension .dll, if not, it adds it by default. This should be the behavior.
API documentation is published here: https://mrdave1999.github.io/CPlugin.Net/api/CPlugin.Net.html
This avoids copying the assemblies to the plugin output directory:
CPlugin.Net/samples/Plugins/Directory.Build.props
Lines 19 to 27 in 3d81cb5
CPlugin.Net/samples/Plugins/Directory.Build.props
Lines 29 to 32 in 3d81cb5
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.