1

Hi I have a situation where I need to update a service that I have injected.

When I first launch the app I have something like

public class MyService:IMyService
{
  public string ValueAPopulatedBeforeLogin { get; set; }
  public string ValueBPopulatedBeforeLogin { get; set; }
  public string ValueCPopulatedAfterLogin { get; set; }
 }
container<IMyService,MyService>();
  1. Start the app
  2. ValueA and B are populated ValueC IS NOT
  3. Login now valueC is populated

Now I need to update MyService but doing

 container.ClearCache(typeOf(IMyService));
 container.UseInstance(MyService);  //with new ValueC updated value

however the new property has not been updated.

How do you update an already injected service? Is this possible?

thanks

Update

  1. I launch the app and IMyService is registed in the container
  2. The "MyService" implementation has been invoked. (I guess the fact that has been invoked is key here)
  3. Now any injected services have some values populated
  4. I logged on and I get additional values and I would like to replaces all services inside my container
  5. I apply the "IfAlreadyRegister" syntax you suggested and If I resolve it again all the new additional properties are there.
  6. But when I go the service again the already injected services have not changed!!
  7. However if I resolve it again they are there..

Which leads to a conclusion and hopefully I am wrong - I cannot change the already injected services (constructor) but I would need to resolve them again.

Hope its clear enough to understand what I want to do

developer9969
  • 4,628
  • 6
  • 40
  • 88

1 Answers1

2

It is not clear from the question, because the code sample is incomplete. But probably, just probably, you need something like:

container.ClearCache(typeOf(IMyService));
container.RegisterInstance<IMyService>(
    new MyService { ValueCPopulatedAfterLogin  = "blah" }, 
    ifAlreadyRegister: IfAlreadyRegister.Replace);
dadhi
  • 4,807
  • 19
  • 25
  • Hi thanks for your reply - apologies if not clear. Your code works but not in my scenario. I launch the app and IMyService is registed and the "MyService" implementation has been invoked. I logged on - I apply "IfAlreadyRegister" when MyService is invoked for the 2nd time after logged on - the properties have not changed. Is it because the constructor obviously is only invoked once - Am I missing something . see edit question for clarity – developer9969 Nov 10 '22 at 06:20
  • @developer9969 Is MyService singleton? Are you planning to inject the properties into the same instance? – dadhi Nov 15 '22 at 18:29
  • hi that is not a singleton and yes I was planning to inject the properties into the same instance.. and thanks for replying.. – developer9969 Nov 15 '22 at 19:00
  • @developer9969 I don't understand. The same instance resolved from the container means that it is either singleton or scoped. – dadhi Nov 16 '22 at 10:55
  • 1
    thank you - I relooked at things and you were right!. problem solved – developer9969 Nov 17 '22 at 17:04