Skip to content

Commit ec41387

Browse files
authored
Add a sample for adding and configuring DI services together (#18429)
* dotnet new webapi * Cleanup * Basic sample
1 parent 782f3f3 commit ec41387

9 files changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
namespace ServiceOptionsSample.Controllers
4+
{
5+
[ApiController]
6+
[Route("api/MyService")]
7+
public class MyServiceController : ControllerBase
8+
{
9+
private readonly IMyService myService;
10+
11+
public MyServiceController(IMyService myService)
12+
{
13+
this.myService = myService;
14+
}
15+
16+
[HttpGet]
17+
public IActionResult GetMyValue() =>
18+
Ok(new { MyValue = myService.GetMyValue() });
19+
}
20+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace ServiceOptionsSample
2+
{
3+
public interface IMyService
4+
{
5+
string GetMyValue();
6+
}
7+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.Extensions.Options;
2+
3+
namespace ServiceOptionsSample
4+
{
5+
public class MyService : IMyService
6+
{
7+
private readonly MyServiceOptions myServiceOptions;
8+
9+
public MyService(IOptions<MyServiceOptions> myServiceOptionsAccessor)
10+
{
11+
myServiceOptions = myServiceOptionsAccessor.Value;
12+
}
13+
14+
public string GetMyValue() =>
15+
myServiceOptions.MyValue;
16+
}
17+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace ServiceOptionsSample
2+
{
3+
public class MyServiceOptions
4+
{
5+
public string MyValue { get; set; } = "This is the default value";
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using Microsoft.Extensions.DependencyInjection.Extensions;
3+
using ServiceOptionsSample;
4+
5+
namespace Microsoft.Extensions.DependencyInjection
6+
{
7+
public static class MyServiceServiceCollectionExtensions
8+
{
9+
public static void AddMyService(this IServiceCollection services)
10+
{
11+
services.TryAddSingleton<IMyService, MyService>();
12+
}
13+
14+
public static void AddMyService(this IServiceCollection services, Action<MyServiceOptions> setupAction)
15+
{
16+
services.AddMyService();
17+
services.Configure(setupAction);
18+
}
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.Hosting;
3+
4+
namespace ServiceOptionsSample
5+
{
6+
public class Program
7+
{
8+
public static void Main(string[] args)
9+
{
10+
CreateHostBuilder(args).Build().Run();
11+
}
12+
13+
public static IHostBuilder CreateHostBuilder(string[] args) =>
14+
Host.CreateDefaultBuilder(args)
15+
.ConfigureWebHostDefaults(webBuilder =>
16+
{
17+
webBuilder.UseStartup<Startup>();
18+
});
19+
}
20+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
<PropertyGroup>
3+
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
</PropertyGroup>
5+
</Project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Hosting;
5+
6+
namespace ServiceOptionsSample
7+
{
8+
public class Startup
9+
{
10+
public void ConfigureServices(IServiceCollection services)
11+
{
12+
services.AddMyService(options =>
13+
{
14+
options.MyValue = "This value was set in ConfigureServices";
15+
});
16+
17+
services.AddControllers();
18+
}
19+
20+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
21+
{
22+
if (env.IsDevelopment())
23+
{
24+
app.UseDeveloperExceptionPage();
25+
}
26+
27+
app.UseHttpsRedirection();
28+
29+
app.UseRouting();
30+
31+
app.UseEndpoints(endpoints =>
32+
{
33+
endpoints.MapControllers();
34+
});
35+
}
36+
}
37+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)