Skip to content
This repository was archived by the owner on Feb 5, 2024. It is now read-only.

Commit dbb57f6

Browse files
authored
Merge pull request #20 from akunzai/code-cleanup
Update dependencies
2 parents 10e3471 + 5ddf6bf commit dbb57f6

File tree

13 files changed

+85
-97
lines changed

13 files changed

+85
-97
lines changed

CHANGELOG.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@
22

33
All notable changes to this project will be documented in this file.
44

5-
## 2019-10-29
5+
## 2.1.0 (2021-06-12)
66

7-
### Spring.Extensions.DependencyInjection 2.0.0
7+
- Bump Microsoft.Extensions.DependencyInjection.Abstractions from 2.0.0 to 2.2.0
8+
- Bump System.Configuration.ConfigurationManager from 4.5.0 to 4.7.0
9+
10+
## 2.0.0 (2019-10-29)
811

912
- Perfect type matched service is preferred
1013
- Remove net461 target
1114

12-
## 2019-02-26
13-
14-
### Spring.Extensions.DependencyInjection 1.1.0
15+
## 1.1.0 (2019-02-26)
1516

1617
- Signing assembly with Strong Name
1718

18-
## 2018-11-24
19-
20-
### Spring.Extensions.DependencyInjection 1.0.0
19+
## 1.0.0 (2018-11-24)
2120

2221
- Initial Release

samples/SampleApp/Program.cs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
2+
using System.Runtime.InteropServices;
3+
using Microsoft.Extensions.Configuration;
24
using Microsoft.Extensions.DependencyInjection;
3-
using Microsoft.Extensions.Logging;
45
using SampleShared;
56
using Spring.Context.Support;
67
using Spring.Extensions.DependencyInjection;
@@ -11,30 +12,40 @@ public static class Program
1112
{
1213
public static void Main()
1314
{
14-
var factory = new SpringServiceProviderFactory(ContextRegistry.GetContext());
15+
var factory = new SpringServiceProviderFactory(options =>
16+
{
17+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
18+
{
19+
options.Parent = ContextRegistry.GetContext();
20+
}
21+
else
22+
{
23+
// Mono doesn't read app.config
24+
var context = new CodeConfigApplicationContext();
25+
context.ScanWithTypeFilter(t => t.Name.EndsWith("SpringConfiguration"));
26+
context.Refresh();
27+
options.Parent = context;
28+
}
29+
});
1530
var services = new ServiceCollection();
1631
ConfigureServices(services);
17-
var context = factory.CreateBuilder(services);
18-
var provider = factory.CreateServiceProvider(context);
19-
20-
using (provider as IDisposable)
21-
{
22-
var clock = provider.GetRequiredService<ISystemClock>();
23-
Console.WriteLine($"Current DateTime is {clock.Now:O}");
24-
}
25-
26-
Console.WriteLine("Press any key to exit ...");
27-
Console.ReadKey();
32+
var resolver = factory.CreateServiceProvider(factory.CreateBuilder(services));
33+
var clock = resolver.GetRequiredService<ISystemClock>();
34+
Console.WriteLine($"Current DateTime is {clock.Now:O}");
2835
}
2936

3037
private static void ConfigureServices(IServiceCollection services)
3138
{
32-
services.AddLogging(logging => logging.AddConsole());
33-
var dummyClock = Environment.GetEnvironmentVariable("SYSTEM_CLOCK");
34-
if (!string.IsNullOrWhiteSpace(dummyClock) && DateTime.TryParse(dummyClock, out var dummyDateTime))
39+
var configuration = new ConfigurationBuilder()
40+
.AddJsonFile("appsettings.json", optional: true)
41+
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production"}.json",
42+
optional: true)
43+
.Build();
44+
services.AddSingleton<IConfiguration>(configuration);
45+
if (configuration["SystemClock"]?.Equals("Dummy", StringComparison.OrdinalIgnoreCase) == true)
3546
{
36-
services.AddSingleton<ISystemClock>(_ => new DummySystemClock(dummyDateTime));
47+
services.AddSingleton<ISystemClock, DummySystemClock>();
3748
}
3849
}
3950
}
40-
}
51+
}

samples/SampleApp/SampleApp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
910
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
10-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
1111
</ItemGroup>
1212

1313
<ItemGroup>

samples/SampleApp/App.config renamed to samples/SampleApp/app.config

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
</configSections>
99
<spring>
1010
<context>
11-
<resource uri="objects.xml" />
12-
<!--<resource uri="config://spring/objects" />-->
11+
<resource uri="config://spring/objects" />
1312
</context>
1413
<objects xmlns="http://www.springframework.net" xmlns:context="http://www.springframework.net/context">
1514
<context:component-scan base-assemblies="SampleShared">
Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using Microsoft.Extensions.DependencyInjection;
33
using Microsoft.Extensions.Hosting;
4-
using Microsoft.Extensions.Logging;
54
using SampleShared;
65
using Spring.Context.Support;
76
using Spring.Extensions.DependencyInjection;
@@ -12,32 +11,23 @@ public static class Program
1211
{
1312
public static void Main()
1413
{
15-
var hostBuilder = new HostBuilder()
16-
.UseServiceProviderFactory(new SpringServiceProviderFactory(options =>
17-
{
18-
var context = new CodeConfigApplicationContext();
19-
context.ScanAllAssemblies();
20-
context.Refresh();
21-
options.Parent = context;
22-
}))
23-
.ConfigureServices(services =>
24-
{
25-
services.AddLogging(logging => logging.AddConsole());
26-
var dummyClock = Environment.GetEnvironmentVariable("SYSTEM_CLOCK");
27-
if (!string.IsNullOrWhiteSpace(dummyClock) && DateTime.TryParse(dummyClock, out var dummyDateTime))
14+
var host = Host.CreateDefaultBuilder()
15+
.UseServiceProviderFactory(new SpringServiceProviderFactory(options =>
2816
{
29-
services.AddSingleton<ISystemClock>(_ => new DummySystemClock(dummyDateTime));
30-
}
31-
});
32-
33-
using (var host = hostBuilder.Build())
34-
{
35-
var clock = host.Services.GetRequiredService<ISystemClock>();
36-
Console.WriteLine($"Current DateTime is {clock.Now:O}");
37-
}
17+
var context = new CodeConfigApplicationContext();
18+
context.ScanWithTypeFilter(t => t.Name.EndsWith("SpringConfiguration"));
19+
context.Refresh();
20+
options.Parent = context;
21+
})).ConfigureServices((context, services) =>
22+
{
23+
if (context.Configuration["SystemClock"]?.Equals("Dummy", StringComparison.OrdinalIgnoreCase) == true)
24+
{
25+
services.AddSingleton<ISystemClock, DummySystemClock>();
26+
}
27+
}).Build();
3828

39-
Console.WriteLine("Press any key to exit ...");
40-
Console.ReadKey();
29+
var clock = host.Services.GetRequiredService<ISystemClock>();
30+
Console.WriteLine($"Current DateTime is {clock.Now:O}");
4131
}
4232
}
43-
}
33+
}

samples/SampleGenericHostApp/SampleGenericHostApp.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
<ItemGroup>
99
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
10-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
1110
</ItemGroup>
1211

1312
<ItemGroup>

samples/SampleShared/DummySystemClock.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ namespace SampleShared
44
{
55
public class DummySystemClock : ISystemClock
66
{
7-
public DummySystemClock(DateTime now)
8-
{
9-
Now = now;
10-
}
11-
12-
public DateTime Now { get; }
7+
public DateTime Now { get; set; } = new DateTime(2000, 1, 1);
138
}
149
}

samples/SampleShared/SampleShared.csproj

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,4 @@
1010
<PackageReference Include="System.Text.Encodings.Web" Version="5.0.1" />
1111
</ItemGroup>
1212

13-
<ItemGroup>
14-
<None Update="objects.xml">
15-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
16-
</None>
17-
</ItemGroup>
18-
1913
</Project>

samples/SampleShared/objects.xml

Lines changed: 0 additions & 6 deletions
This file was deleted.

samples/SampleWebHostApp/Startup.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.AspNetCore.Builder;
33
using Microsoft.AspNetCore.Hosting;
44
using Microsoft.AspNetCore.Http;
5+
using Microsoft.Extensions.Configuration;
56
using Microsoft.Extensions.DependencyInjection;
67
using SampleShared;
78
using Spring.Context.Support;
@@ -11,37 +12,42 @@ namespace SampleWebHostApp
1112
{
1213
public class Startup
1314
{
15+
public Startup(IConfiguration configuration)
16+
{
17+
Configuration = configuration;
18+
}
19+
20+
public IConfiguration Configuration { get; }
21+
1422
public IServiceProvider ConfigureServices(IServiceCollection services)
1523
{
1624
var factory = new SpringServiceProviderFactory(options =>
1725
{
1826
var context = new CodeConfigApplicationContext();
19-
context.ScanAllAssemblies();
27+
context.ScanWithTypeFilter(t => t.Name.EndsWith("SpringConfiguration"));
2028
context.Refresh();
2129
options.Parent = context;
2230
});
23-
var dummyClock = Environment.GetEnvironmentVariable("SYSTEM_CLOCK");
24-
if (!string.IsNullOrWhiteSpace(dummyClock) && DateTime.TryParse(dummyClock, out var dummyDateTime))
31+
if (Configuration["SystemClock"]?.Equals("Dummy", StringComparison.OrdinalIgnoreCase) == true)
2532
{
26-
services.AddSingleton<ISystemClock>(_ => new DummySystemClock(dummyDateTime));
33+
services.AddSingleton<ISystemClock, DummySystemClock>();
2734
}
28-
var containerBuilder = factory.CreateBuilder(services);
29-
return factory.CreateServiceProvider(containerBuilder);
35+
return factory.CreateServiceProvider(factory.CreateBuilder(services));
3036
}
3137

3238
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
3339
public void Configure(
3440
IApplicationBuilder app,
3541
IHostingEnvironment env,
36-
IServiceProvider provider)
42+
IServiceProvider services)
3743
{
3844
if (env.IsDevelopment())
3945
{
4046
app.UseDeveloperExceptionPage();
4147
}
4248
app.Run(async context =>
4349
{
44-
var clock = provider.GetRequiredService<ISystemClock>();
50+
var clock = services.GetRequiredService<ISystemClock>();
4551
await context.Response.WriteAsync($"Current DateTime is {clock.Now:O}").ConfigureAwait(false);
4652
});
4753
}

0 commit comments

Comments
 (0)