Skip to content

Commit

Permalink
Simplified WebApi example (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-fernandez-rodriguez authored Dec 12, 2024
1 parent 3c7efa7 commit b8479de
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 103 deletions.

This file was deleted.

64 changes: 42 additions & 22 deletions Registerly/samples/Registerly.Samples.WebApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,63 @@
using DeviantCoding.Registerly;
using DeviantCoding.Registerly.AttributeRegistration;
using DeviantCoding.Registerly.Strategies.Lifetime;
using Registerly.Samples.WebApi.Services;
using Microsoft.AspNetCore.Mvc;

// Start the application and use the provided http file to test it.

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

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

// Three ways to register the WeatherService class
// Two ways to register service classes:
// - Marking the class with an attribute
// - Manually registering a class

//builder.RegisterServicesByAttributes();
builder.RegisterServicesByAttributes();

builder.Register(classes => classes
.Where(c => c.Exactly<WeatherService>())
.FromAssemblyOf<FarewellerService>()
.Where(c => c.Exactly<FarewellerService>())
.Using<Singleton>());

// The following registration is more performant:
//builder.Register(classes => classes
// .FromAssemblyOf<WeatherService>()
// .Where(c => c.Exactly<WeatherService>())
// .Using<Singleton>());

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
app.MapControllers();

app.Run();



[ApiController]
[Route("chatterbox")]
public class ChatterboxController(IGreeterService greeterService, IFarewellerService farewellerService) : ControllerBase
{
app.UseSwagger();
app.UseSwaggerUI();
[HttpGet("greet")]
public string Greet() => greeterService.Greet();

[HttpGet("farewell")]
public string Farewell() => farewellerService.Farewell();
}

app.UseHttpsRedirection();

app.UseAuthorization();
public interface IGreeterService
{
string Greet();
}

[Singleton]
public class GreeterService : IGreeterService
{
public string Greet() => "Hello, World!";
}

app.MapControllers();
public interface IFarewellerService
{
string Farewell();
}

app.Run();
public class FarewellerService : IFarewellerService
{
public string Farewell() => "Goodbye, World!";
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,16 @@
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:59984",
"sslPort": 44371
}
"anonymousAuthentication": true
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5109",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7129;http://localhost:5109",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\DeviantCoding.Registerly.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
@Registerly.Samples.WebApi_HostAddress = http://localhost:5109

GET {{Registerly.Samples.WebApi_HostAddress}}/weatherforecast/
GET {{Registerly.Samples.WebApi_HostAddress}}/chatterbox/greet
Accept: application/json

###

GET {{Registerly.Samples.WebApi_HostAddress}}/chatterbox/farewell
Accept: application/json

###

This file was deleted.

12 changes: 0 additions & 12 deletions Registerly/samples/Registerly.Samples.WebApi/WeatherForecast.cs

This file was deleted.

0 comments on commit b8479de

Please sign in to comment.