Skip to content

Commit 07136d7

Browse files
Releases/1.1.0 alpha1 (#6)
* Changes the HttpApplicationExtensions to use IServiceCollection instead of ServiceCollection. * Add new UseServiceProvider() and AddDefaultAspNetServices() API methods to use an existing IServiceProvider and IServiceCollection. * Updates the readme to indicate new AddDefaultAspNetServices() and UseServiceProvider() methods. * Fix the build previously failed
1 parent 440a46b commit 07136d7

File tree

8 files changed

+473
-47
lines changed

8 files changed

+473
-47
lines changed

AspNet.WebForms.DependencyInjection.IntegrationTests/App_Start/ServicesConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace PosInformatique.AspNet.WebForms.DependencyInjection.IntegrationTests
1313

1414
public static class ServicesConfig
1515
{
16-
public static void RegisterServices(ServiceCollection serviceCollection)
16+
public static void RegisterServices(IServiceCollection serviceCollection)
1717
{
1818
serviceCollection.AddSingleton<IDogRepository, DogRepository>();
1919
serviceCollection.AddTransient<IDogManager, DogManager>();

AspNet.WebForms.DependencyInjection.Tests/HttpApplicationExtensionsTest.cs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,125 @@ public void AddServiceCollection_WithExistingServiceCollectionAndWithNullService
175175
.And.ParamName.Should().Be("serviceCollection");
176176
}
177177

178+
[Fact]
179+
public void UseServiceProvider()
180+
{
181+
var application = new HttpApplicationMock();
182+
183+
var service = Mock.Of<IService>();
184+
185+
var serviceProvider = new Mock<IServiceProvider>(MockBehavior.Strict);
186+
serviceProvider.Setup(sp => sp.GetService(typeof(IService)))
187+
.Returns(service);
188+
189+
var returnedApplication = HttpApplicationExtensions.UseServiceProvider(application, serviceProvider.Object);
190+
191+
returnedApplication.Should().BeSameAs(application);
192+
193+
// Checks the WebObjectActivator has been defined correctly
194+
HttpRuntime.WebObjectActivator.Should().BeOfType<ServiceProviderAdapter>();
195+
196+
// Checks the services
197+
HttpRuntime.WebObjectActivator.GetService(typeof(IService)).Should().BeSameAs(service);
198+
199+
// Checks the next provider in the adapter has been defined correctly
200+
HttpRuntime.WebObjectActivator.GetFieldValue<IServiceProvider>("nextProvider").Should().BeSameAs(this.existingProvider);
201+
202+
// Checks the adapter has been registered on the hosting environment infrastructure
203+
typeof(HostingEnvironment).GetStaticValue<object>("_theHostingEnvironment").GetFieldValue<Hashtable>("_registeredObjects").ContainsKey(HttpRuntime.WebObjectActivator);
204+
typeof(HostingEnvironment).GetStaticValue<object>("_theHostingEnvironment").GetFieldValue<Hashtable>("_registeredObjects").ContainsValue(HttpRuntime.WebObjectActivator);
205+
}
206+
207+
[Fact]
208+
public void UseServiceProvider_WithNullApplication_ExceptionThrown()
209+
{
210+
Action act = () =>
211+
{
212+
HttpApplicationExtensions.UseServiceProvider<HttpApplication>(null, null);
213+
};
214+
215+
act.Should().Throw<ArgumentNullException>()
216+
.And.ParamName.Should().Be("application");
217+
}
218+
219+
[Fact]
220+
public void UseServiceProvider_WithNullServiceProvider_ExceptionThrown()
221+
{
222+
Action act = () =>
223+
{
224+
HttpApplicationExtensions.UseServiceProvider(new HttpApplication(), null);
225+
};
226+
227+
act.Should().Throw<ArgumentNullException>()
228+
.And.ParamName.Should().Be("serviceProvider");
229+
}
230+
231+
[Fact]
232+
public void AddDefaultAspNetServices()
233+
{
234+
// Creates a HttpContext
235+
var application = new HttpApplicationMock();
236+
var request = new HttpRequest("The filename", "http://theurl", "");
237+
var response = new HttpResponse(new StringWriter());
238+
var session = FormatterServices.GetUninitializedObject(typeof(HttpSessionState));
239+
240+
HttpContext.Current = new HttpContext(request, response)
241+
{
242+
ApplicationInstance = application,
243+
Items =
244+
{
245+
{ "AspSession", session }
246+
}
247+
};
248+
249+
try
250+
{
251+
var collection = new ServiceCollection();
252+
253+
var returnedCollection = HttpApplicationExtensions.AddDefaultAspNetServices(collection, application);
254+
255+
returnedCollection.Should().BeSameAs(collection);
256+
257+
// Builds the service provider and check the registered services
258+
var serviceProvider = collection.BuildServiceProvider();
259+
260+
// Checks the services
261+
serviceProvider.GetService(typeof(HttpApplication)).Should().BeSameAs(application);
262+
serviceProvider.GetService(typeof(HttpApplicationMock)).Should().BeSameAs(application);
263+
serviceProvider.GetService(typeof(HttpRequest)).Should().BeSameAs(request);
264+
serviceProvider.GetService(typeof(HttpResponse)).Should().BeSameAs(response);
265+
serviceProvider.GetService(typeof(HttpSessionState)).Should().BeSameAs(session);
266+
}
267+
finally
268+
{
269+
HttpContext.Current = null;
270+
}
271+
}
272+
273+
[Fact]
274+
public void AddDefaultAspNetServices_WithNullServiceCollection_ExceptionThrown()
275+
{
276+
Action act = () =>
277+
{
278+
HttpApplicationExtensions.AddDefaultAspNetServices<HttpApplication>(null, null);
279+
};
280+
281+
act.Should().Throw<ArgumentNullException>()
282+
.And.ParamName.Should().Be("services");
283+
}
284+
285+
[Fact]
286+
public void AddDefaultAspNetServices_WithNullHttpApplication_ExceptionThrown()
287+
{
288+
Action act = () =>
289+
{
290+
HttpApplicationExtensions.AddDefaultAspNetServices<HttpApplication>(Mock.Of<IServiceCollection>(), null);
291+
};
292+
293+
act.Should().Throw<ArgumentNullException>()
294+
.And.ParamName.Should().Be("application");
295+
}
296+
178297
public void Dispose()
179298
{
180299
HttpRuntime.WebObjectActivator = null;

AspNet.WebForms.DependencyInjection.Tests/ServiceProviderAdapterTest.cs

Lines changed: 96 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ namespace PosInformatique.AspNet.WebForms.DependencyInjection.Tests
99
public class ServiceProviderAdapterTest
1010
{
1111
[Fact]
12-
public void GetService_InServiceCollectionWithInjection()
12+
public void GetService_UsingServiceCollection_InServiceCollectionWithInjection()
1313
{
1414
var serviceCollection = new ServiceCollection();
1515
serviceCollection.AddSingleton<IService, Service>();
1616
serviceCollection.AddSingleton<IDependentService, DependentService>();
1717

18-
var serviceProviderAdapter = new ServiceProviderAdapter(null, serviceCollection);
18+
var serviceProviderAdapter = new ServiceProviderAdapter(serviceCollection, null);
1919

2020
var result = serviceProviderAdapter.GetService(typeof(IService)).As<Service>();
2121

@@ -30,7 +30,7 @@ public void GetService_InServiceCollectionWithInjection()
3030
}
3131

3232
[Fact]
33-
public void GetService_NotInServiceCollectionUsingNextProvider()
33+
public void GetService_UsingServiceCollection_NotInServiceCollectionUsingNextProvider()
3434
{
3535
var serviceCollection = new ServiceCollection();
3636

@@ -40,36 +40,36 @@ public void GetService_NotInServiceCollectionUsingNextProvider()
4040
nextServiceProvider.Setup(sp => sp.GetService(typeof(IService)))
4141
.Returns(service);
4242

43-
var serviceProviderAdapter = new ServiceProviderAdapter(nextServiceProvider.Object, serviceCollection);
43+
var serviceProviderAdapter = new ServiceProviderAdapter(serviceCollection, nextServiceProvider.Object);
4444

4545
var result = serviceProviderAdapter.GetService(typeof(IService)).As<Service>();
4646

4747
result.Should().BeSameAs(service);
4848
}
4949

5050
[Fact]
51-
public void GetService_NotInServiceCollectionAndNotInTheNextProvider()
51+
public void GetService_UsingServiceCollection_NotInServiceCollectionAndNotInTheNextProvider()
5252
{
5353
var serviceCollection = new ServiceCollection();
5454

5555
var nextServiceProvider = new Mock<IServiceProvider>(MockBehavior.Strict);
5656
nextServiceProvider.Setup(sp => sp.GetService(typeof(DependentService)))
5757
.Returns(null);
5858

59-
var serviceProviderAdapter = new ServiceProviderAdapter(nextServiceProvider.Object, serviceCollection);
59+
var serviceProviderAdapter = new ServiceProviderAdapter(serviceCollection, nextServiceProvider.Object);
6060

6161
var result = serviceProviderAdapter.GetService(typeof(DependentService)).As<DependentService>();
6262

6363
result.Should().NotBeNull();
6464
}
6565

6666
[Fact]
67-
public void GetService_WithInstantiationWithInjection()
67+
public void GetService_UsingServiceCollection_WithInstantiationWithInjection()
6868
{
6969
var serviceCollection = new ServiceCollection();
7070
serviceCollection.AddSingleton<IDependentService, DependentService>();
7171

72-
var serviceProviderAdapter = new ServiceProviderAdapter(null, serviceCollection);
72+
var serviceProviderAdapter = new ServiceProviderAdapter(serviceCollection, null);
7373

7474
var result = serviceProviderAdapter.GetService(typeof(Service)).As<Service>();
7575

@@ -84,12 +84,12 @@ public void GetService_WithInstantiationWithInjection()
8484
}
8585

8686
[Fact]
87-
public void GetService_WithInstantiationInternalConstructor()
87+
public void GetService_UsingServiceCollection_WithInstantiationInternalConstructor()
8888
{
8989
var serviceCollection = new ServiceCollection();
9090
serviceCollection.AddSingleton<IDependentService, DependentService>();
9191

92-
var serviceProviderAdapter = new ServiceProviderAdapter(null, serviceCollection);
92+
var serviceProviderAdapter = new ServiceProviderAdapter(serviceCollection, null);
9393

9494
var result = serviceProviderAdapter.GetService(typeof(ServiceWithInternalConstructor)).As<ServiceWithInternalConstructor>();
9595

@@ -102,12 +102,12 @@ public void GetService_WithInstantiationInternalConstructor()
102102
}
103103

104104
[Fact]
105-
public void GetService_WithInstantiationInternalClass()
105+
public void GetService_UsingServiceCollection_WithInstantiationInternalClass()
106106
{
107107
var serviceCollection = new ServiceCollection();
108108
serviceCollection.AddSingleton<IDependentService, DependentService>();
109109

110-
var serviceProviderAdapter = new ServiceProviderAdapter(null, serviceCollection);
110+
var serviceProviderAdapter = new ServiceProviderAdapter(serviceCollection, null);
111111

112112
var result = serviceProviderAdapter.GetService(typeof(ServiceInternal)).As<ServiceInternal>();
113113

@@ -120,12 +120,12 @@ public void GetService_WithInstantiationInternalClass()
120120
}
121121

122122
[Fact]
123-
public void Stop()
123+
public void Stop_UsingServiceCollection()
124124
{
125125
var serviceCollection = new ServiceCollection();
126126
serviceCollection.AddSingleton<IDependentService, DependentService>();
127127

128-
var serviceProviderAdapter = new ServiceProviderAdapter(null, serviceCollection);
128+
var serviceProviderAdapter = new ServiceProviderAdapter(serviceCollection, null);
129129

130130
var result = serviceProviderAdapter.GetService(typeof(IDependentService)).As<DependentService>();
131131

@@ -142,6 +142,88 @@ public void Stop()
142142
.And.ObjectName.Should().Be("PosInformatique.AspNet.WebForms.DependencyInjection.ServiceProviderAdapter");
143143
}
144144

145+
[Fact]
146+
public void GetService_UsingServiceProvider_InServiceCollectionWithInjection()
147+
{
148+
var service1 = Mock.Of<IService>();
149+
var service2 = Mock.Of<IService>();
150+
151+
var serviceProvider = new Mock<IServiceProvider>(MockBehavior.Strict);
152+
serviceProvider.SetupSequence(sp => sp.GetService(typeof(IService)))
153+
.Returns(service1)
154+
.Returns(service2);
155+
156+
var serviceProviderAdapter = new ServiceProviderAdapter(serviceProvider.Object, null);
157+
158+
serviceProviderAdapter.GetService(typeof(IService)).Should().BeSameAs(service1);
159+
160+
// Calls again to check the internal cache have not problem
161+
serviceProviderAdapter.GetService(typeof(IService)).Should().BeSameAs(service2);
162+
}
163+
164+
[Fact]
165+
public void GetService_UsingServiceProvider_NotInServiceCollectionUsingNextProvider()
166+
{
167+
var serviceProvider = new Mock<IServiceProvider>(MockBehavior.Strict);
168+
serviceProvider.Setup(sp => sp.GetService(typeof(IService)))
169+
.Returns(null);
170+
171+
var service = new Service(null);
172+
173+
var nextServiceProvider = new Mock<IServiceProvider>(MockBehavior.Strict);
174+
nextServiceProvider.Setup(sp => sp.GetService(typeof(IService)))
175+
.Returns(service);
176+
177+
var serviceProviderAdapter = new ServiceProviderAdapter(serviceProvider.Object, nextServiceProvider.Object);
178+
179+
var result = serviceProviderAdapter.GetService(typeof(IService)).As<Service>();
180+
181+
result.Should().BeSameAs(service);
182+
}
183+
184+
[Fact]
185+
public void GetService_UsingServiceProvider_NotInServiceCollectionAndNotInTheNextProvider()
186+
{
187+
var serviceProvider = new Mock<IServiceProvider>(MockBehavior.Strict);
188+
serviceProvider.Setup(sp => sp.GetService(typeof(DependentService)))
189+
.Returns(null);
190+
191+
var nextServiceProvider = new Mock<IServiceProvider>(MockBehavior.Strict);
192+
nextServiceProvider.Setup(sp => sp.GetService(typeof(DependentService)))
193+
.Returns(null);
194+
195+
var serviceProviderAdapter = new ServiceProviderAdapter(serviceProvider.Object, nextServiceProvider.Object);
196+
197+
var result = serviceProviderAdapter.GetService(typeof(DependentService));
198+
199+
result.Should().NotBeNull();
200+
}
201+
202+
[Fact]
203+
public void Stop_UsingServiceProvider()
204+
{
205+
var dependentService = Mock.Of<IDependentService>();
206+
207+
var serviceProvider = new Mock<IServiceProvider>(MockBehavior.Strict);
208+
serviceProvider.As<IDisposable>();
209+
serviceProvider.Setup(sp => sp.GetService(typeof(IDependentService)))
210+
.Returns(dependentService);
211+
212+
var serviceProviderAdapter = new ServiceProviderAdapter(serviceProvider.Object, null);
213+
214+
serviceProviderAdapter.GetService(typeof(IDependentService)).As<DependentService>();
215+
216+
// Stop() the adapter, it is mean we dispose the provider BUT the no
217+
serviceProviderAdapter.Stop(true);
218+
219+
// Verify the Dispose() method is not called for a IServiceProvider which implement the IDisposable interface
220+
serviceProvider.As<IDisposable>().Verify(sp => sp.Dispose(), Times.Never);
221+
222+
// Check the ObjectDisposedException if calling the GetService() method
223+
serviceProviderAdapter.Invoking(spa => spa.GetService(typeof(IDependentService))).Should().ThrowExactly<ObjectDisposedException>()
224+
.And.ObjectName.Should().Be("PosInformatique.AspNet.WebForms.DependencyInjection.ServiceProviderAdapter");
225+
}
226+
145227
public interface IService
146228
{
147229
}

AspNet.WebForms.DependencyInjection/AspNet.WebForms.DependencyInjection.csproj

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,25 @@
1313
<PackageTags>aspnet webforms ioc dependencyinjection</PackageTags>
1414
<Description>PosInformatique.AspNet.WebForms.DependencyInjection is a library to add the IoC container support of Microsoft.Extensions.DependencyInjection for ASP .NET Web Forms</Description>
1515
<Authors>Gilles TOURREAU</Authors>
16-
<PackageReleaseNotes>1.0.0 - Initial version</PackageReleaseNotes>
16+
<PackageReleaseNotes>
17+
1.1.0 - Using the IServiceCollection in the public API instead of ServiceCollection.
18+
Add new UseServiceProvider() and AddDefaultAspNetServices() API methods to use an existing IServiceProvider and IServiceCollection.
19+
1.0.0 - Initial version
20+
</PackageReleaseNotes>
1721
<RepositoryUrl>https://github.com/PosInformatique/PosInformatique.AspNet.WebForms.DependencyInjection.git</RepositoryUrl>
1822
<Version>1.0.0-alpha2</Version>
1923
<AssemblyVersion>1.0.0.2</AssemblyVersion>
2024
<PackageLicenseFile>LICENSE</PackageLicenseFile>
2125
</PropertyGroup>
2226

27+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
28+
<CodeAnalysisRuleSet>AspNet.WebForms.DependencyInjection.ruleset</CodeAnalysisRuleSet>
29+
</PropertyGroup>
30+
31+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
32+
<CodeAnalysisRuleSet>AspNet.WebForms.DependencyInjection.ruleset</CodeAnalysisRuleSet>
33+
</PropertyGroup>
34+
2335
<ItemGroup>
2436
<AdditionalFiles Include="..\stylecop.json" Link="stylecop.json" />
2537
</ItemGroup>

0 commit comments

Comments
 (0)