help
Root Question Message
AddControllers().AddApplicationPart
, but no luck there. I'm thinking it has to do with the pattern I'm using for registering services in separate assemblies.namespace Core
{
public class Startup
{
private readonly IConfiguration _config;
public Startup(IConfiguration config)
{
_config = config;
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddApiServices(_config);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app)
{
app.ConfigureApi(_config);
}
}
}
namespace Api.Extensions
{
public static class ApiStartupExt
{
public static void AddApiServices(this IServiceCollection builder, IConfiguration config)
{
builder.AddControllers().AddApplicationPart(typeof(ApiStartupExt).Assembly);
builder.AddSwaggerGen(c => c.SwaggerDoc("v0", new OpenApiInfo
{
Title = "test_service",
Version = "v0"
}));
}
public static void ConfigureApi(this IApplicationBuilder app, IConfiguration config)
{
if (!config["Environment"]!.Equals("Production"))
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "test_service v0");
c.RoutePrefix = string.Empty;
});
}
// Map rest services to port 43000
// In my actual application I also have GRPC endpoints on the same host
// so I need to use the UseWhen method to avoid some issues there
app.UseWhen(context => context.Connection.LocalPort == config.GetValue<int>("Rest:Port"),
builder =>
{
builder.UsePathBase(new PathString("/api"));
builder.UseRouting();
builder.UseEndpoints(endpoints => endpoints.MapControllers());
}
);
}
}
}
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/1.1 POST http://localhost:43000/api/WeatherForecast - 0
dbug: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[0]
Wildcard detected, all requests with hosts will be allowed.
dbug: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[1]
POST requests are not supported
dbug: Microsoft.AspNetCore.Server.Kestrel.Connections[9]
Connection id "0HMNNSN1PNNGU" completed keep alive response.
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished HTTP/1.1 POST http://localhost:43000/api/WeatherForecast - 0 - 404 0 - 11.6784ms
if(env.IsProduction())
and variation like !env.IsProduction()
or IsDevelopment()
["foo"]!.
operatordotnet new webapi
have a boiler plate with a clean if
as an exampledotnet new webapi -n PleaseDeleteMeLater
ApiStartupExt
as an ApplicationPart
WeatherForecastController
typeof(...).Assembly
?...
do?<PackageReference Include="Microsoft.AspNet.Mvc" Version="5.2.9" />
is that normal ?FrameworkReference
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
.Mvc
in package name / code needs to be checked <ItemGroup>
- <PackageReference Include="Microsoft.AspNet.Mvc" Version="5.2.9" />
+ <FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
IConfiguration
AddApiServices
and ConfigureApi
ConfigureApi
is basically POST DI container buildIOptions