Register Instance for PerRequest mode.

434 views
Skip to first unread message

Eugene Chepurnykh

unread,
Oct 5, 2015, 10:05:53 AM10/5/15
to Autofac
Hi,

I've ran into troubles when I inserted instances on per request bases. Does anybody know why Autofac container can't allowed register instances?
It means that sentence like this:
rb.RegisterInstance(instance).As<interfaceOfInstance>().InstancePerRequest().
leads to InvalidOperationException with text like "The instance  registration '{0}' can support SingleInstance() sharing only".

I've investigated problem in sources and  saw very strange code:

var rb = new RegistrationBuilder<T, SimpleActivatorData, SingleRegistrationStyle>(
                new TypedService(typeof(T)),
                new SimpleActivatorData(activator),
                new SingleRegistrationStyle());

            rb.SingleInstance();

            builder.RegisterCallback(cr =>
            {
                if (!(rb.RegistrationData.Lifetime is RootScopeLifetime) ||
                    rb.RegistrationData.Sharing != InstanceSharing.Shared)
                    throw new InvalidOperationException(string.Format(
                        RegistrationExtensionsResources.InstanceRegistrationsAreSingleInstanceOnly, instance));

                activator.DisposeInstance = rb.RegistrationData.Ownership == InstanceOwnership.OwnedByLifetimeScope;

                RegistrationBuilder.RegisterSingleComponent(cr, rb);
            });

Does anybody can explain this behaviour?

Kaleb Pederson

unread,
Oct 5, 2015, 11:36:12 AM10/5/15
to aut...@googlegroups.com
When you call RegisterInstance you are required to pass it an instance. Because you're providing the instance, Autofac doesn't have a factory that it can use to create the object so it's constrained to use the same instance every time. In other words, you've provided the instance and Autofac doesn't know how to create instances nor can it create an instance for each request.

If you truly need that type to be InstancePerRequest you'll have to use something other than RegisterInstance, like Register(c => ...). If you don't really require InstancePerRequest semantics you can regitser the object as SingleInstance, which is the implicit scope associated with any calls to RegisterInstance.

--Kaleb

--
You received this message because you are subscribed to the Google Groups "Autofac" group.
To unsubscribe from this group and stop receiving emails from it, send an email to autofac+u...@googlegroups.com.
To post to this group, send email to aut...@googlegroups.com.
Visit this group at http://groups.google.com/group/autofac.
For more options, visit https://groups.google.com/d/optout.

Travis Illig

unread,
Oct 5, 2015, 12:39:04 PM10/5/15
to Autofac
It appears you've cross-posted from StackOverflow to the forums. In the future, please try to post questions just to StackOverflow so there isn't time spent answering the same question in two spots.

Eugene Chepurnykh

unread,
Oct 6, 2015, 4:00:57 AM10/6/15
to Autofac
Thanks for your answer!
Kaleb, it's not absolutely correct. When we have "request" we can tune container that inside this request container insert this instance in borders of the request. There are no need factory for this. 
Register(c=>...) behave absolutely the same as RegisterInstance. I've tried this. Of course, I know what is it SingleInstance but it is not my case. 
I've found a decision that seems like workaround. I registered type with InstancePerRequest modificator and check UserName in object that contain UserName and exist in each Request. In DelegatingHandler class I check field UserName in object that exist in each reques and set this field if need. 
I understood that we work IDependencyScope and easily get dependency for current scope but we can't set this scope.  We have HttpRequestMessage request and  request.GetDependencyScope() - return current scope for request. 
Thank again.:)
понедельник, 5 октября 2015 г., 18:36:12 UTC+3 пользователь Kaleb Pederson написал:

Eugene Chepurnykh

unread,
Oct 6, 2015, 4:11:19 AM10/6/15
to Autofac
I'm so sorry about this. I posted question in StackOverflow and waited for two days.  After this I thought that StackOverflow "not so popular" according to Autofac. And only after this I posted this question here. 
My apologize... 

понедельник, 5 октября 2015 г., 19:39:04 UTC+3 пользователь Travis Illig написал:

Travis Illig

unread,
Oct 6, 2015, 12:28:29 PM10/6/15
to Autofac
No worries, just don't forget time zones - in my time zone, you posted your SO question on Friday afternoon and waited two days (the weekend) with no response. You posted the forum question Monday morning... and got a response on both Monday morning. We're all pretty dedicated, we just don't all have the ability to work on the weekends. :)

Travis Illig

unread,
Oct 6, 2015, 12:29:50 PM10/6/15
to Autofac
I'm going to lock this topic so we can get to an answer on StackOverflow.
Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages