zcz527 / autofac

Automatically exported from code.google.com/p/autofac
Other
0 stars 0 forks source link

Optional/Enumerable arguments throwing scope resolution exception #300

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
This might be an issue or by-design so feel free to disregard if this isn't 
pertinent.

I have the following constructor on a component:

public MyCompenent(
  HttpContextBase iisContext = null,
  OperationContext wcfContext = null)
{ ... }

My goal here is to create a component that can do things with the current 
context, whether that context comes from IIS or WCF. In this scenario, I expect 
one of these arguments to be null and the other to be non-null.

I'm using AutofacWebTypesModule to register the HttpContextBase component and 
AutofacWebServiceHostFactory to resolve the WCF service.

When I try to resolve this component during a WCF call, I get the following 
exception from Autofac: "No scope matching the expression 
'value(Autofac.Builder.RegistrationBuilder`3+<>c__DisplayClass0[System.Web.HttpC
ontextWrapper,Autofac.Builder.SimpleActivatorData,Autofac.Builder.SingleRegistra
tionStyle]).lifetimeScopeTag.Equals(scope.Tag)' is visible from the scope in 
which the instance was requested."

This is somewhat expected since I'm inside the WCF scope and not the IIS scope. 
However, since I have defaulted the argments of my constructor to null I would 
expect to just get a null for any component that can't be resolved rather than 
an exception.

The same problem happens if I rewrite the constructor like this:

public MyCompenent(
  IEnumerable<HttpContextBase> iisContexts,
  IEnumerable<OperationContext> wcfContexts)
{ ... }

What is the expected output? 
I expect a null (or empty enumerator) to be passed to my argument.

What do you see instead?
Autofac throws an exception.

What version of Autofac are you using? 
2.4.4.705

Original issue reported on code.google.com by frustrat...@gmail.com on 23 Feb 2011 at 5:06

GoogleCodeExporter commented 8 years ago
I just verified that this exception also gets thrown if I explicitly register 
MyComponent using ResolveOptional to resolve the HttpContextBase. I would 
expect a null from ResolveOptional in that case.

Original comment by frustrat...@gmail.com on 23 Feb 2011 at 5:14

GoogleCodeExporter commented 8 years ago
Thanks for getting in touch, apologies about the slow response.

The Autofac behavior in this case is by design; we follow one general principle 
that a component is never "hidden" because of missing dependencies or invalid 
scope. Although it is a hindrance in this scenario, it makes many others much 
more predictable and easy to debug.

In this case I think the best option is to create two components, one wrapping 
the WCF context and the other wrapping the HTTP context.

Register the WCF one by default, then use the overloaded constructor of 
AutofacDependencyResolver to override the WCF one with the HTTP one by 
registering it on a per-request basis.

Hope this helps!

Cheers,
Nick

Original comment by nicholas...@gmail.com on 9 Mar 2011 at 9:30