1

I would like to inject an object of type GraphClient into my MVC service classes at runtime. This object has a parameter of type Uri which is basically the Uri of the current tenant we wish to look at. Where I'm getting hungup is that this works perfectly for the initial load however as I switch to different tenants the uri persists with the first uri registered. Which makes sense. However I'm wondering, is there a way to tell Unity to do the parameter injection of this dependency at instantiation every time?

The following is a trimmed up version of my code.

var container = new UnityContainer();
container.RegisterType(typeof(GraphClient), new InjectionConstructor(RootUri));
protected static Uri RootUri
{
 get
 {
 SessionWrapper wrapper = new SessionWrapper();
 cString = wrapper.CurrentTenantUri;
 return new Uri(cString);
 }
}
abatishchev
101k88 gold badges303 silver badges443 bronze badges
asked Dec 18, 2014 at 17:43

1 Answer 1

1

You can use an InjectionFactory to accomplish what you are after. This will get executed every time a GraphClient is needing to be instantiated.

container.RegisterType(typeof(GraphClient), 
 new InjectionFactory(c => new GraphClient(RootUri)));
answered Dec 18, 2014 at 20:11
Sign up to request clarification or add additional context in comments.

1 Comment

Yahtzee! Thanks Tyler!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.