Skip to content
This repository has been archived by the owner on Nov 25, 2019. It is now read-only.

Commit

Permalink
Throw an exception if BeginScope returns null instead of falling back…
Browse files Browse the repository at this point in the history
… to defaults silently
  • Loading branch information
youssefm committed Aug 14, 2012
1 parent d0fc458 commit 22cdd31
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 18 deletions.
15 changes: 5 additions & 10 deletions src/System.Web.Http/Dispatcher/DefaultHttpControllerActivator.cs
Expand Up @@ -4,7 +4,6 @@
using System.Net.Http;
using System.Threading;
using System.Web.Http.Controllers;
using System.Web.Http.Dependencies;
using System.Web.Http.Internal;
using System.Web.Http.Properties;

Expand Down Expand Up @@ -112,16 +111,12 @@ private static IHttpController GetInstanceOrActivator(HttpRequestMessage request
Contract.Assert(request != null);
Contract.Assert(controllerType != null);

IDependencyScope scope = request.GetDependencyScope();
if (scope != null)
// If dependency resolver returns controller object then use it.
IHttpController instance = (IHttpController)request.GetDependencyScope().GetService(controllerType);
if (instance != null)
{
// If dependency resolver returns controller object then use it.
IHttpController instance = (IHttpController)scope.GetService(controllerType);
if (instance != null)
{
activator = null;
return instance;
}
activator = null;
return instance;
}

// Otherwise create a delegate for creating a new instance of the type
Expand Down
7 changes: 6 additions & 1 deletion src/System.Web.Http/HttpRequestMessageExtensions.cs
Expand Up @@ -55,7 +55,12 @@ public static IDependencyScope GetDependencyScope(this HttpRequestMessage reques
IDependencyScope result;
if (!request.Properties.TryGetValue<IDependencyScope>(HttpPropertyKeys.DependencyScope, out result))
{
result = request.GetConfiguration().DependencyResolver.BeginScope();
IDependencyResolver dependencyResolver = request.GetConfiguration().DependencyResolver;
result = dependencyResolver.BeginScope();
if (result == null)
{
throw Error.InvalidOperation(SRResources.DependencyResolver_BeginScopeReturnsNull, dependencyResolver.GetType().Name);
}
request.Properties[HttpPropertyKeys.DependencyScope] = result;
request.RegisterForDispose(result);
}
Expand Down
11 changes: 10 additions & 1 deletion src/System.Web.Http/Properties/SRResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/System.Web.Http/Properties/SRResources.resx
Expand Up @@ -413,4 +413,7 @@ The request for '{0}' has found the following matching controllers:{2}</value>
<data name="ValidationAttributeOnNonPublicProperty" xml:space="preserve">
<value>Non-public property '{0}' on type '{1}' is attributed with one or more validation attributes. Validation attributes on non-public properties are not supported. Consider using a public property for validation instead.</value>
</data>
<data name="DependencyResolver_BeginScopeReturnsNull" xml:space="preserve">
<value>A dependency resolver of type '{0}' returned an invalid value of null from its BeginScope method. If the container does not have a concept of scope, consider returning a scope that resolves in the root of the container instead.</value>
</data>
</root>
Expand Up @@ -132,7 +132,7 @@ public void Create_MixupInstanceCreationAndDependencyScope()
}

[Fact]
public void Create_MakesInstanceOfControllerForNullDependencyScope()
public void Create_ThrowsForNullDependencyScope()
{
// Arrange
var config = new HttpConfiguration();
Expand All @@ -144,12 +144,18 @@ public void Create_MakesInstanceOfControllerForNullDependencyScope()
var descriptorSimpleController = new HttpControllerDescriptor(config, "Simple", typeof(SimpleController));
var activator = new DefaultHttpControllerActivator();

// Act
IHttpController simpleController = activator.Create(request, descriptorSimpleController, typeof(SimpleController));
// Act & Assert
InvalidOperationException exception = Assert.Throws<InvalidOperationException>(
() => activator.Create(request, descriptorSimpleController, typeof(SimpleController)));

Assert.Equal(
"An error occurred when trying to create a controller of type 'SimpleController'. Make sure that the controller has a parameterless public constructor.",
exception.Message);
Assert.NotNull(exception.InnerException);
Assert.Equal(
"A dependency resolver of type 'IDependencyResolverProxy' returned an invalid value of null from its BeginScope method. If the container does not have a concept of scope, consider returning a scope that resolves in the root of the container instead.",
exception.InnerException.Message);

// Assert
Assert.NotNull(simpleController);
Assert.IsType<SimpleController>(simpleController);
mockResolver.Verify();
}

Expand Down

0 comments on commit 22cdd31

Please sign in to comment.