Skip to content

Commit

Permalink
Added sample which uses the appsettings.json based configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikoskinen committed Oct 14, 2020
1 parent a48b0b1 commit b699eaa
Show file tree
Hide file tree
Showing 22 changed files with 414 additions and 105 deletions.
8 changes: 8 additions & 0 deletions samples/WebAppPluginsLibrary/CustomPlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace WebAppPluginsLibrary
{
public class CustomPlugin
{
}
}
7 changes: 7 additions & 0 deletions samples/WebAppPluginsLibrary/WebAppPluginsLibrary.csproj
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 samples/WebAppWithAppSettings/Controllers/CalculatorController.cs
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();
}
}
}
20 changes: 20 additions & 0 deletions samples/WebAppWithAppSettings/Program.cs
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 samples/WebAppWithAppSettings/Properties/launchSettings.json
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"
}
}
}
40 changes: 40 additions & 0 deletions samples/WebAppWithAppSettings/Startup.cs
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 samples/WebAppWithAppSettings/WebAppWithAppSettings.csproj
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>
9 changes: 9 additions & 0 deletions samples/WebAppWithAppSettings/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
14 changes: 14 additions & 0 deletions samples/WebAppWithAppSettings/appsettings.json
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"
}
]
}
}
14 changes: 14 additions & 0 deletions src/PluginFramework.sln
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WinFormsPluginsLibrary", ".
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Weikio.PluginFramework.Configuration", "Weikio.PluginFramework.Configuration\Weikio.PluginFramework.Configuration.csproj", "{882BB58E-D256-4BBF-8C5F-80C4FA39B775}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebAppWithAppSettings", "..\samples\WebAppWithAppSettings\WebAppWithAppSettings.csproj", "{A5FAE1A5-74A0-4A83-9520-DCABF6610ADE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebAppPluginsLibrary", "..\samples\WebAppPluginsLibrary\WebAppPluginsLibrary.csproj", "{38CCE0F7-F998-4766-A14A-44C047E8D0AA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -173,6 +177,14 @@ Global
{882BB58E-D256-4BBF-8C5F-80C4FA39B775}.Debug|Any CPU.Build.0 = Debug|Any CPU
{882BB58E-D256-4BBF-8C5F-80C4FA39B775}.Release|Any CPU.ActiveCfg = Release|Any CPU
{882BB58E-D256-4BBF-8C5F-80C4FA39B775}.Release|Any CPU.Build.0 = Release|Any CPU
{A5FAE1A5-74A0-4A83-9520-DCABF6610ADE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A5FAE1A5-74A0-4A83-9520-DCABF6610ADE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A5FAE1A5-74A0-4A83-9520-DCABF6610ADE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A5FAE1A5-74A0-4A83-9520-DCABF6610ADE}.Release|Any CPU.Build.0 = Release|Any CPU
{38CCE0F7-F998-4766-A14A-44C047E8D0AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{38CCE0F7-F998-4766-A14A-44C047E8D0AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{38CCE0F7-F998-4766-A14A-44C047E8D0AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{38CCE0F7-F998-4766-A14A-44C047E8D0AA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -200,6 +212,8 @@ Global
{A7EC9D91-CED0-43E7-BAB3-3B72230BFC0B} = {FF3A507D-D242-44F6-8940-49E0B51B6F02}
{6D392453-FC9E-45AD-A9A7-7596A93B3E6C} = {FF3A507D-D242-44F6-8940-49E0B51B6F02}
{E92B0C5D-FFAC-4AB9-B069-869AEED565B0} = {FF3A507D-D242-44F6-8940-49E0B51B6F02}
{A5FAE1A5-74A0-4A83-9520-DCABF6610ADE} = {FF3A507D-D242-44F6-8940-49E0B51B6F02}
{38CCE0F7-F998-4766-A14A-44C047E8D0AA} = {FF3A507D-D242-44F6-8940-49E0B51B6F02}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AAEBC2B4-FB52-4272-8C25-571FB3CA01E5}
Expand Down
11 changes: 11 additions & 0 deletions src/Weikio.PluginFramework.Abstractions/PluginFrameworkOptions.cs
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";
}
}
79 changes: 79 additions & 0 deletions src/Weikio.PluginFramework.AspNetCore/PluginProvider.cs
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;
}
}
}
Loading

0 comments on commit b699eaa

Please sign in to comment.