Generator for an ASP.NET Core API project.
npm install generator-dgp-api-aspnetcore
bash
npm install yo -g
`
The _-g_ flags install it globally so you can run yeoman from anywhere.
Install the generator :
` bash
npm install generator-dgp-api-aspnetcore -g
`
Generate a new ASP.NET Core 6 API project
In a command prompt, navigate to the directory where you want to create the new project and type :
` bash
yo dgp-api-aspnetcore
`
Answer the questions :-)
If you prefer to skip the prompt and supply the parameters using command line arguments simply add the '--skip-prompt y' argument.
See the table below for other argument options.
| Parameter | Options | Default |
| ---------------- | ----------------- | ----------- |
| --skip-prompt | y/n | n |
| --delete-content | y/n | y |
| --name | project name text | Starter app |
| --database | mo/ms/p/n | p |
| --http-kestrel | port number | |
| --http-iis | port number | |
| --https-iis | port number | |
The ASP.NET Core solution
$3
Enter your application Id, which you can find in AppConfig, in _config\app.json. It will be used in the StartUp class -> ConfigureServices -> services.AddApplicationServices
...to do...
$3
The DependencyRegistration class is used to register the dependencies of the application, so that is uses the built-in dependency injection framework.
` csharp
services.AddTransient();
services.AddScoped();
services.AddSingleton();
`
Transient
Every time the service is injected, a new one is instantiated.
Scoped
The lifetime of the service is tied to the request scope. Only 1 instance is instantiated per request.
Singleton
Only one instance is instantiated for the whole application.
More info about the dependency injection in ASP.NET Core can be found at : https://docs.asp.net/en/latest/fundamentals/dependency-injection.html.
Serviceagent injection (2 different ways)
- Dynamic config injection based on serviceagents.json config file (currently only oauth2 support)
Serviceagents can be injected dynamically based on their configuration.
Practical example:
1. Add agent configuration to serviceagents.json
` json
{
"ServiceAgents": {
"TestAgent": {
"FriendlyName": "ACPaaS-TestEngine",
"AuthScheme": "none",
"Headers": {
"apikey": "0991fe70-ef89-5a87e-9354-14be7cef7c35",
"accept": "application/hal+json"
},
"Host": "api-gw-o.antwerpen.be",
"Path": "acpaas/testengine/v3",
"Scheme": "https"
}
}
}
`
2. Add class with corresponding name inheriting from ConfigInjectedAgentBase<>
` csharp
public class TestAgent: AgentBase, ITestAgent
{
public TestAgent(ILogger logger, HttpClient httpClient, IServiceProvider serviceProvider) : base(logger, httpClient, serviceProvider)
{
}
}
`
3. The serviceagent will be automatically configured with all the options provided in the config file and is ready to be injected.
` csharp
public ExamplesController(ITestAgent agent)
{
}
`
Remark: Currently only OAuth2 scheme is implemented. If you are planning to use serviceagents with Bearer or Basic authentication please fill in a feature request. (RequestHeaderHelper.cs).
- Manual registration in DI container and registration of delegating handlers
1. Add agent configuration to serviceagents.json
` json
{
"ServiceAgents": {
"TestAgent": {
"FriendlyName": "ACPaaS-TestEngine",
"AuthScheme": "none",
"Headers": {
"apikey": "0991fe70-ef89-5a87e-9354-14be7cef7c35",
"accept": "application/hal+json"
},
"Host": "api-gw-o.antwerpen.be",
"Path": "acpaas/testengine/v3",
"Scheme": "https"
}
}
}
`
2. Add class with corresponding name inheriting from AgentBase<>
` csharp
public class TestAgent: AgentBase, ITestAgent
{
public TestAgent(ILogger logger, HttpClient httpClient, IServiceProvider serviceProvider) : base(logger, httpClient, serviceProvider)
{
}
}
`
3. Add agent to DI container in DependencyRegistration.cs file. This way of registering gives you the opportunity to configure each agent separately with handler or custom headers.
` csharp
services.AddHttpClient(nameof(TestAgent))
.AddHttpMessageHandler()
.AddHttpMessageHandler()
.AddHttpMessageHandler()
.AddTypedClient();
``