Hope this helps, rushed reply so apologies if I missed the point :)
Nick
> --
> You received this message because you are subscribed to the Google Groups "Autofac" group.
> To post to this group, send email to aut...@googlegroups.com.
> To unsubscribe from this group, send email to autofac+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/autofac?hl=en.
>
I think my problem is I have no idea on how to configure deal with
InstancePerHttpRequest() scope.
So here's my general layout usually
1. Use ContainerBuilder to do container.Register all the classes
2. Then call container.Build()
3. Pass it to DependencyResolver.SetResolver(new
AutofacDependencyResolver(container)); in global.asax.cs
This is working fine and dandy since I use the default InstancePerDependency.
The trouble starts when I use InstancePerHttpRequest() in part one and
all hell break loose. Unfortunately all the examples I've seen about
asp.net mvc3 integration never use InstancePerHttpRequest() when they
register their classes.
This is what I tried after your suggestion
public static class DomainEvents
{
public static ILifetimeScope Container { get; set; } //as before
//Raises the given domain event
public static void Raise<T>(T args) where T : IDomainEvent
{
if (Container != null)
foreach (var handler in
Container.Resolve<IEnumerable<Handles<T>>>())
handler.Handle(args);
}
}
then in global.asax.cs Application_Start() method, I do
using (var scope = container.BeginLifetimeScope())
{
DomainEvents.Container = scope;
}
Now this throws
" Cannot access a disposed object.
Object name: 'Instances cannot be resolved and nested lifetimes cannot
be created from this LifetimeScope as it has already been disposed.'.
-- at Autofac.Core.Lifetime.LifetimeScope.CheckNotDisposed()"
I must be missing something stupid and obvious but I haven't been able
to finger where the problem is.
I've try to wrap them via constructor using Lazy<> but it returns No
scope matching the expression,
builder.Register(c => new BlogValidator(() =>
DependencyResolver.Current.GetService<IDocumentStorage<Blog>>())).Keyed<IValidator>(typeof(Blog)).SingleInstance();
and
builder.Register(c => new
BlogValidator(c.Resolve<IDocumentStorage<Blog>>())).Keyed<IValidator>(typeof(Blog)).InstancePerLifetimeScope();
seems to behave the same way (I put a logger on BlogValidator
constructor) that BlogValidator is only created once in multiple
requests.
One of the dependency is an InstancePerHttpRequest().
So the SingleInstance() is supposed to behave this way. I don't
understand why InstancePerLifetimeScope() also behaves the same. Is it
a hanging reference?
builder.Register(c => new BlogValidator(() =>
DependencyResolver.Current.GetService<IDocumentStorage<Blog>>())).Keyed<IValidator>(typeof(Blog)).SingleInstance();
On Thu, May 5, 2011 at 1:44 AM, Nicholas Blumhardt
So if I have 10 fields in my web form, FluentValidator try to retrieve
BlogValidator object ten times. Perhaps I should mark it with
InstancePerHttpRequest().
On Thu, May 5, 2011 at 10:55 AM, Dody Gunawinata
--
You received this message because you are subscribed to the Google Groups "Autofac" group.
To view this discussion on the web visit https://groups.google.com/d/msg/autofac/-/tVdOe8Xe0zsJ.