Skip to content

Commit

Permalink
fix null reference in Extensions.cs (#5)
Browse files Browse the repository at this point in the history
fix exception occuring when some depedencies are not present + test
  • Loading branch information
ptixed authored and mikeobrien committed Aug 13, 2018
1 parent 62a1008 commit 9aa8814
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 17 deletions.
11 changes: 10 additions & 1 deletion src/TestHarness/Controller.cs
@@ -1,4 +1,5 @@
using System.Net.Http;
using System;
using System.Net.Http;
using System.Web.Http;

namespace TestHarness
Expand Down Expand Up @@ -29,6 +30,7 @@ public class Model
}

[Route("test")]
[TestFilter]
public IHttpActionResult Get()
{
return Ok(new Model
Expand All @@ -40,5 +42,12 @@ public IHttpActionResult Get()
RequestUrl = _request.RequestUri.ToString()
});
}

[Route("test2")]
[ExceptionFilter]
public void Get2()
{
throw new Exception();
}
}
}
29 changes: 28 additions & 1 deletion src/TestHarness/Filter.cs
@@ -1,4 +1,5 @@
using System.Net.Http;
using System.Net;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using WebApi.StructureMap;
Expand All @@ -18,6 +19,16 @@ public override void OnActionExecuted(HttpActionExecutedContext actionExecutedCo
}
}

public class ExceptionFilter : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
var service = actionExecutedContext.GetService<ResponseInspectorService>();
actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.OK);
actionExecutedContext.Response.Headers.Add("ResponseStatus", service.InspectResponse());
}
}

public class DummyService
{
public DummyService(
Expand Down Expand Up @@ -52,4 +63,20 @@ public void SetHeaders()
_transientDependency.GetHashCode().ToString());
}
}

public class ResponseInspectorService
{
private readonly HttpResponseMessage _response;

public ResponseInspectorService(
HttpResponseMessage response)
{
_response = response;
}

public string InspectResponse()
{
return _response == null ? "NoResponse" : "ResponsePresent";
}
}
}
1 change: 0 additions & 1 deletion src/TestHarness/Global.asax.cs
Expand Up @@ -8,7 +8,6 @@ public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
GlobalConfiguration.Configuration.Filters.Add(new TestFilter());
GlobalConfiguration.Configuration.UseStructureMap<Registry>();
GlobalConfiguration.Configuration.MapHttpAttributeRoutes();
GlobalConfiguration.Configuration.EnsureInitialized();
Expand Down
14 changes: 14 additions & 0 deletions src/Tests/AcceptanceTests.cs
Expand Up @@ -55,5 +55,19 @@ public void should_build_object_graph_and_dispose_nested_containers()
throw;
}
}

[Test]
public void should_retrieve_depedency_from_http_context_when_no_reponse_present()
{
var client = new WebClient();
client.Headers.Add("content-type", "application/json");
client.Headers.Add("accept", "application/json");

var url = IISBootstrap.BuildUrl("test2");
var result = client.DownloadString(url);

result.ShouldBeEmpty();
client.ResponseHeaders["ResponseStatus"].ShouldEqual("NoResponse");
}
}
}
32 changes: 18 additions & 14 deletions src/WebApi.StructureMap/Extensions.cs
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Controllers;
Expand Down Expand Up @@ -63,29 +62,34 @@ public static T GetService<T>(this HttpRequestMessage message)

public static T GetService<T>(this HttpActionExecutedContext context)
{
return context.Request.GetDependencyScope().GetService<T>(context,
context.ActionContext, context.Response);
}
var scope = context.Request.GetDependencyScope();
var container = scope.GetService<IContainer>();

public static T GetService<T>(this HttpActionContext context)
{
return context.Request.GetDependencyScope().GetService<T>(context,
context.ActionDescriptor, context.ControllerContext,
context.ModelState);
var explicitArguments = new ExplicitArguments();
explicitArguments.SetWithActualType(context);
explicitArguments.SetWithActualType(context.ActionContext);
explicitArguments.SetWithActualType(context.Response);

return container.GetInstance<T>(explicitArguments);
}

private static T GetService<T>(this IDependencyScope scope,
params object[] arguments)
public static T GetService<T>(this HttpActionContext context)
{
var scope = context.Request.GetDependencyScope();
var container = scope.GetService<IContainer>();

var explicitArguments = new ExplicitArguments();
arguments.ForEach(x => explicitArguments.Set(x.GetType(), x));
explicitArguments.SetWithActualType(context);
explicitArguments.SetWithActualType(context.ActionDescriptor);
explicitArguments.SetWithActualType(context.ControllerContext);
explicitArguments.SetWithActualType(context.ModelState);

return container.GetInstance<T>(explicitArguments);
}

private static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
private static void SetWithActualType<T>(this ExplicitArguments explicitArguments, T instance)
{
foreach (var item in source) action(item);
explicitArguments.Set(instance == null ? typeof(T) : instance.GetType(), instance);
}
}
}

0 comments on commit 9aa8814

Please sign in to comment.