-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added sample which uses the appsettings.json based configuration.
- Loading branch information
1 parent
a48b0b1
commit b699eaa
Showing
22 changed files
with
414 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System; | ||
|
||
namespace WebAppPluginsLibrary | ||
{ | ||
public class CustomPlugin | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
</Project> |
55 changes: 55 additions & 0 deletions
55
samples/WebAppWithAppSettings/Controllers/CalculatorController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Weikio.PluginFramework.Abstractions; | ||
using Weikio.PluginFramework.AspNetCore; | ||
using Weikio.PluginFramework.Samples.Shared; | ||
|
||
namespace WebAppWithAppSettings.Controllers | ||
{ | ||
[ApiController] | ||
[Route("[controller]")] | ||
public class CalculatorController : ControllerBase | ||
{ | ||
private readonly IEnumerable<Plugin> _plugins; | ||
private readonly IServiceProvider _serviceProvider; | ||
private readonly PluginProvider _pluginProvider; | ||
|
||
public CalculatorController(IEnumerable<Plugin> plugins, IServiceProvider serviceProvider, PluginProvider pluginProvider) | ||
{ | ||
_plugins = plugins; | ||
_serviceProvider = serviceProvider; | ||
_pluginProvider = pluginProvider; | ||
} | ||
|
||
[HttpGet] | ||
public string Get() | ||
{ | ||
var result = new StringBuilder(); | ||
|
||
result.AppendLine("All:"); | ||
foreach (var plugin in _plugins) | ||
{ | ||
result.AppendLine($"{plugin.Name}: {plugin.Version}, Tags: {string.Join(", ", plugin.Tags)}"); | ||
} | ||
|
||
var mathPlugins = _pluginProvider.GetByTag("MathOperator"); | ||
var value1 = 10; | ||
var value2 = 20; | ||
|
||
result.AppendLine($"Math operations with values {value1} and {value2}"); | ||
|
||
foreach (var mathPlugin in mathPlugins) | ||
{ | ||
var mathPluginInstance = _serviceProvider.Create<IOperator>(mathPlugin); | ||
|
||
var mathResult = mathPluginInstance.Calculate(value1, value2); | ||
result.AppendLine($"{mathPlugin.Name}: {mathResult}"); | ||
} | ||
|
||
return result.ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace WebAppWithAppSettings | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
CreateHostBuilder(args).Build().Run(); | ||
} | ||
|
||
public static IHostBuilder CreateHostBuilder(string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
.ConfigureWebHostDefaults(webBuilder => | ||
{ | ||
webBuilder.UseStartup<Startup>(); | ||
}); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
samples/WebAppWithAppSettings/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"profiles": { | ||
"WebAppWithAppSettings": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"launchUrl": "Calculator", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"applicationUrl": "https://localhost:5001;http://localhost:5000" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Weikio.PluginFramework.Samples.Shared; | ||
using Weikio.PluginFramework.TypeFinding; | ||
|
||
namespace WebAppWithAppSettings | ||
{ | ||
public class Startup | ||
{ | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
TypeFinderOptions.Defaults.TypeFinderCriterias.Add(TypeFinderCriteriaBuilder.Create().Implements<IOperator>().Tag("MathOperator")); | ||
TypeFinderOptions.Defaults.TypeFinderCriterias.Add(TypeFinderCriteriaBuilder.Create().Tag("All")); | ||
services.AddPluginFramework(); | ||
|
||
services.AddControllers(); | ||
} | ||
|
||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | ||
{ | ||
if (env.IsDevelopment()) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.UseRouting(); | ||
|
||
app.UseAuthorization(); | ||
|
||
app.UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapControllers(); | ||
}); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
samples/WebAppWithAppSettings/WebAppWithAppSettings.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Weikio.PluginFramework.AspNetCore\Weikio.PluginFramework.AspNetCore.csproj" /> | ||
<ProjectReference Include="..\Shared\Weikio.PluginFramework.Samples.SharedPlugins\Weikio.PluginFramework.Samples.SharedPlugins.csproj" /> | ||
<ProjectReference Include="..\Shared\Weikio.PluginFramework.Samples.Shared\Weikio.PluginFramework.Samples.Shared.csproj" /> | ||
<ProjectReference Include="..\WebAppPluginsLibrary\WebAppPluginsLibrary.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Content Update="Properties\launchSettings.json"> | ||
<CopyToPublishDirectory>Never</CopyToPublishDirectory> | ||
</Content> | ||
</ItemGroup> | ||
|
||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"PluginFramework": { | ||
"Catalogs": [ | ||
{ | ||
"Type": "Folder", | ||
"Path": "..\\Shared\\Weikio.PluginFramework.Samples.SharedPlugins\\bin\\debug\\netcoreapp3.1" | ||
}, | ||
{ | ||
"Type": "Assembly", | ||
"Path": ".\\bin\\Debug\\netcoreapp3.1\\WebAppPluginsLibrary.dll" | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/Weikio.PluginFramework.Abstractions/PluginFrameworkOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Weikio.PluginFramework.Abstractions | ||
{ | ||
/// <summary> | ||
/// Configures the options for Plugin Framework. | ||
/// </summary> | ||
public class PluginFrameworkOptions | ||
{ | ||
public bool UseConfiguration { get; set; } = true; | ||
public string ConfigurationSection { get; set; } = "PluginFramework"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Weikio.PluginFramework.Abstractions; | ||
|
||
namespace Weikio.PluginFramework.AspNetCore | ||
{ | ||
public class PluginProvider | ||
{ | ||
private readonly IEnumerable<IPluginCatalog> _catalogs; | ||
private readonly IServiceProvider _serviceProvider; | ||
|
||
public PluginProvider(IEnumerable<IPluginCatalog> catalogs, IServiceProvider serviceProvider) | ||
{ | ||
_catalogs = catalogs; | ||
_serviceProvider = serviceProvider; | ||
} | ||
|
||
public List<Plugin> GetByTag(string tag) | ||
{ | ||
var result = new List<Plugin>(); | ||
|
||
foreach (var pluginCatalog in _catalogs) | ||
{ | ||
var pluginsByTag = pluginCatalog.GetByTag(tag); | ||
result.AddRange(pluginsByTag); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public List<Plugin> GetPlugins() | ||
{ | ||
var result = new List<Plugin>(); | ||
foreach (var pluginCatalog in _catalogs) | ||
{ | ||
result.AddRange(pluginCatalog.GetPlugins()); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public Plugin Get(string name, Version version) | ||
{ | ||
foreach (var pluginCatalog in _catalogs) | ||
{ | ||
var result = pluginCatalog.Get(name, version); | ||
|
||
if (result != null) | ||
{ | ||
return result; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public List<T> GetTypes<T>() where T : class | ||
{ | ||
var result = new List<T>(); | ||
var catalogs = _serviceProvider.GetServices<IPluginCatalog>(); | ||
|
||
foreach (var catalog in catalogs) | ||
{ | ||
var plugins = catalog.GetPlugins(); | ||
|
||
foreach (var plugin in plugins.Where(x => typeof(T).IsAssignableFrom(x))) | ||
{ | ||
var op = plugin.Create<T>(_serviceProvider); | ||
|
||
result.Add(op); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
Oops, something went wrong.