Skip to content

Commit

Permalink
Merge pull request #35 from MrDave1999/sample/plugin-istartup
Browse files Browse the repository at this point in the history
docs: Create a sample plugin that includes a Program.cs file
  • Loading branch information
MrDave1999 committed May 18, 2024
2 parents bd6d283 + 82b0e78 commit 6242e6c
Show file tree
Hide file tree
Showing 14 changed files with 171 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/dotnetcore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ jobs:
dotnet build ./samples/Plugins/JsonPlugin/Example.JsonPlugin.csproj -c Release
dotnet build ./samples/Plugins/OldJsonPlugin/Example.OldJsonPlugin.csproj -c Release
dotnet build ./samples/Plugins/DependencyInjectionPlugin/Example.DependencyInjectionPlugin.csproj -c Release
dotnet build ./samples/Plugins/ProgramPlugin/Example.ProgramPlugin.csproj -c Release
dotnet test ./samples/Test/Example.Test.csproj -c Release
16 changes: 11 additions & 5 deletions CPlugin.Net.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.HostWebApi", "sampl
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.SharedEntities", "samples\SharedEntities\Example.SharedEntities.csproj", "{F66A1430-3F32-4E25-8966-54D502D216DE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.DependencyInjectionPlugin", "samples\Plugins\DependencyInjectionPlugin\Example.DependencyInjectionPlugin.csproj", "{28065D77-B890-47DE-B695-04E388176925}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.DependencyInjectionPlugin", "samples\Plugins\DependencyInjectionPlugin\Example.DependencyInjectionPlugin.csproj", "{28065D77-B890-47DE-B695-04E388176925}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.JsonPlugin", "samples\Plugins\JsonPlugin\Example.JsonPlugin.csproj", "{C5B8EF73-7DB5-441F-AE38-0988751A896B}"
EndProject
Expand All @@ -27,6 +27,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.AppointmentPlugin",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.PersonPlugin", "samples\Plugins\PersonPlugin\Example.PersonPlugin.csproj", "{18534944-583B-4924-AC5B-E0655FD92AAC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.ProgramPlugin", "samples\Plugins\ProgramPlugin\Example.ProgramPlugin.csproj", "{BA75A525-7013-4F0D-85FD-416E744098FD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.Test", "samples\Test\Example.Test.csproj", "{1E64908D-DC48-4B83-BB25-CF36821EB37F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestProject.JsonPlugin", "tests\Plugins\TestProject.JsonPlugin\TestProject.JsonPlugin.csproj", "{0F27C776-F284-4C94-86C9-0FF089245E13}"
Expand Down Expand Up @@ -84,6 +86,10 @@ Global
{F66A1430-3F32-4E25-8966-54D502D216DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F66A1430-3F32-4E25-8966-54D502D216DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F66A1430-3F32-4E25-8966-54D502D216DE}.Release|Any CPU.Build.0 = Release|Any CPU
{28065D77-B890-47DE-B695-04E388176925}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{28065D77-B890-47DE-B695-04E388176925}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28065D77-B890-47DE-B695-04E388176925}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28065D77-B890-47DE-B695-04E388176925}.Release|Any CPU.Build.0 = Release|Any CPU
{C5B8EF73-7DB5-441F-AE38-0988751A896B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C5B8EF73-7DB5-441F-AE38-0988751A896B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C5B8EF73-7DB5-441F-AE38-0988751A896B}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -128,10 +134,10 @@ Global
{0BCD3305-F0D5-43E6-B879-EEF0827558A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0BCD3305-F0D5-43E6-B879-EEF0827558A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0BCD3305-F0D5-43E6-B879-EEF0827558A8}.Release|Any CPU.Build.0 = Release|Any CPU
{28065D77-B890-47DE-B695-04E388176925}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{28065D77-B890-47DE-B695-04E388176925}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28065D77-B890-47DE-B695-04E388176925}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28065D77-B890-47DE-B695-04E388176925}.Release|Any CPU.Build.0 = Release|Any CPU
{BA75A525-7013-4F0D-85FD-416E744098FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BA75A525-7013-4F0D-85FD-416E744098FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BA75A525-7013-4F0D-85FD-416E744098FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BA75A525-7013-4F0D-85FD-416E744098FD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
3 changes: 2 additions & 1 deletion samples/HostApplications/WebApi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"Plugins": [
"Example.AppointmentPlugin.dll",
"Example.PersonPlugin.dll",
"Example.DependencyInjectionPlugin.dll"
"Example.DependencyInjectionPlugin.dll",
"Example.ProgramPlugin.dll"
]
}
19 changes: 19 additions & 0 deletions samples/Plugins/ProgramPlugin/Example.ProgramPlugin.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<OutDir>$(WebApiProjectDir)</OutDir>
<NoDefaultLaunchSettingsFile>false</NoDefaultLaunchSettingsFile>
<!-- This constant indicates that the project is a plugin. -->
<DefineConstants>PLUGIN</DefineConstants>
<!-- If you want to deploy this project, overwrite the previous constant. -->
<!--
<DefineConstants></DefineConstants>
-->
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
<PackageReference Include="Swashbuckle.AspNetCore" />
</ItemGroup>

</Project>
4 changes: 4 additions & 0 deletions samples/Plugins/ProgramPlugin/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
global using Microsoft.AspNetCore.Mvc;
global using CPlugin.Net;
global using Example.ProgramPlugin;
global using Example.Contracts;
25 changes: 25 additions & 0 deletions samples/Plugins/ProgramPlugin/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
builder.Services.AddServices();

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseAuthorization();

app.MapControllers();

app.Run();
13 changes: 13 additions & 0 deletions samples/Plugins/ProgramPlugin/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"profiles": {
"Example.ProgramPlugin": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:50778;http://localhost:50779"
}
}
}
12 changes: 12 additions & 0 deletions samples/Plugins/ProgramPlugin/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Example.ProgramPlugin;

public static class ServiceCollectionExtensions
{
public static IServiceCollection AddServices(this IServiceCollection services)
{
services
.AddSingleton<SummaryService>();

return services;
}
}
17 changes: 17 additions & 0 deletions samples/Plugins/ProgramPlugin/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#if PLUGIN

[assembly: Plugin(typeof(Startup))]

namespace Example.ProgramPlugin;

public class Startup : IWebStartup
{
public string Name => "summary";
public string Description => "Expose endpoints.";

public void ConfigureServices(IServiceCollection services)
{
services.AddServices();
}
}
#endif
10 changes: 10 additions & 0 deletions samples/Plugins/ProgramPlugin/SummaryController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Example.ProgramPlugin;

[ApiController]
[Route("[controller]")]
public class SummaryController : ControllerBase
{
[HttpGet]
public IEnumerable<string> Get(SummaryService service)
=> service.GetAll();
}
21 changes: 21 additions & 0 deletions samples/Plugins/ProgramPlugin/SummaryService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Example.ProgramPlugin;

public class SummaryService
{
private static readonly string[] Summaries =
[
"Freezing",
"Bracing",
"Chilly",
"Cool",
"Mild",
"Warm",
"Balmy",
"Hot",
"Sweltering",
"Scorching"
];

public IEnumerable<string> GetAll()
=> Summaries;
}
8 changes: 8 additions & 0 deletions samples/Plugins/ProgramPlugin/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions samples/Plugins/ProgramPlugin/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
19 changes: 19 additions & 0 deletions samples/Test/WebApi/Get.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,23 @@ public async Task Get_WhenServiceNameIsObtained_ShouldReturnsHttpStatusCodeOk()
httpResponse.StatusCode.Should().Be(HttpStatusCode.OK);
result.Should().Be(expectedServiceName);
}

[Test]
public async Task Get_WhenSummariesAreObtained_ShouldReturnsHttpStatusCodeOk()
{
// Arrange
using var factory = new WebApplicationFactory<Program>();
var client = factory.CreateClient();
int expectedSummaries = 10;

// Act
var httpResponse = await client.GetAsync("/Summary");
var result = await httpResponse
.Content
.ReadFromJsonAsync<string[]>();

// Asserts
httpResponse.StatusCode.Should().Be(HttpStatusCode.OK);
result.Should().HaveCount(expectedSummaries);
}
}

0 comments on commit 6242e6c

Please sign in to comment.