voss...@gmail.com
unread,Jan 5, 2015, 10:30:30 AM1/5/15Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Sign in to report message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to aut...@googlegroups.com
I unexpectedly found registrations using `InstancePerRequest` with Wcf Integration fail with:
> No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself.
I was looking for this registration for the sake of having every resolution of the same type be the same instance when a service is resolved.
Considering the simplified (contrived) example,
```
int i = 0;
builder.Register<int>(c => i++);
builder.RegisterType<EchoService>();
// [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
// public class EchoService
// public EchoService(int i, int j){}
```
I want `i` and `j` to both be `0` the first time EchoService is created, (i, j) == (1,1) the next time and so on.
With `InstancePerDepenancy()` for the `int` registration we get: (0,1), (2,3), ...
With `SingleInstance()` for the `int` registration we get: (0,0), (0,0), ...
`InstancePerLifetimeScope()` does give the desired behavior: (0, 1), (2, 3), ...
As far as I know `InstancePerLifetimeScope()` will work for me, but it was not intuitive that per request wouldn't work and I needed to use per lifetime scope. I was also just looking to future proof: I do not currently do any custom work with lifetime scope but that is not to say I won't in the future.
I suppose the complication if `InstancePerRequest()` was implemented would be with InstanceContextMode.PerSession.
```
int i = 0;
builder.Register<int>(c => i++).InstancePerRequest();
builder.RegisterType<EchoService>();
// [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
// public class EchoService
// public EchoService(int i, Func<int> j)
```
Clearly `i` will be zero for the entire session, but what will `j()` be in the case of multiple requests? Is it possible recognize each call so it would increment, or should it be zero the entire session?