refit icon indicating copy to clipboard operation
refit copied to clipboard

How to switch host URL at runtime?

Open opcodewriter opened this issue 10 years ago • 10 comments

How to change the host URL at runtime? The only option I see is basically recreate the API service instance:

RestService.For<IMyApi>()

The issue with this this is supposed to be an dependency injected singleton.... There might be instances of which already have the IMyApi instance...

opcodewriter avatar Sep 22 '16 22:09 opcodewriter

Then inject a singleton factory

On Thu, 22 Sep 2016 at 19:09 opcodewriter [email protected] wrote:

How to change the host URL at runtime? The only option I see is basically recreate the API service instance:

RestService.For()

The issue with this this is supposed to be an dependency injected singleton.... There might be instances of which already have the IMyApi instance...

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/paulcbetts/refit/issues/266, or mute the thread https://github.com/notifications/unsubscribe-auth/AGr-6v9UsXuv_XF2WLSp61E-4_sHBH0Zks5qsvy0gaJpZM4KEana .

ahmedalejo avatar Sep 23 '16 06:09 ahmedalejo

Note that this isn´t a Refit limitation, .NET´s HttpClient doesn´t allow changing the BaseAddress after the first request made with that instance

A workarround is to use a DelegatingHandler that mangles the RequestUri of the HttpRequestMessage

On Fri, 23 Sep 2016 at 03:01 Ahmed Alejo [email protected] wrote:

Then inject a singleton factory

On Thu, 22 Sep 2016 at 19:09 opcodewriter [email protected] wrote:

How to change the host URL at runtime? The only option I see is basically recreate the API service instance:

RestService.For()

The issue with this this is supposed to be an dependency injected singleton.... There might be instances of which already have the IMyApi instance...

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/paulcbetts/refit/issues/266, or mute the thread https://github.com/notifications/unsubscribe-auth/AGr-6v9UsXuv_XF2WLSp61E-4_sHBH0Zks5qsvy0gaJpZM4KEana .

ahmedalejo avatar Sep 23 '16 18:09 ahmedalejo

.NET´s HttpClient doesn´t allow changing the BaseAddress after the first request made with that instance

Yeah, of course, it makes sense. Thanks @ahmedalejo !

opcodewriter avatar Sep 24 '16 11:09 opcodewriter

Thanks again @ahmedalejo Any other ways to achieve this other than using DelegatingHandler? It seems a bit overkill for this task, not sure. What if Refit had a way to provide a HttpClient instance, like a factory or something?

opcodewriter avatar Apr 22 '17 20:04 opcodewriter

BaseAddress can be set on a HttpClient instance at any time. But the issue is that I can't get the HttpClient instance from the object generated from RestService.For<IApi>(httpClient)

opcodewriter avatar Apr 22 '17 20:04 opcodewriter

If you add a get property of type HttpClient named Client on your IApi it should expose it to you.

clairernovotny avatar Apr 22 '17 23:04 clairernovotny

@onovotny Wow, thanks! Can you please share some thoughts about how does that magic works? How does Refit know how to populate that property? I don't see it mentioned in the documentation.

opcodewriter avatar Apr 23 '17 06:04 opcodewriter

It's not magic, it's just set on the instance by the generated code....but unless your interface exposes it, you'll never see it.

clairernovotny avatar Apr 23 '17 11:04 clairernovotny

It is possible to make calls to endpoints with different base urls via SendAsync method from same instance of HttpClient via providing Uri via HttpRequestMessage. BaseAddress should not be used as only one instance of HttpClient is meant to be used through out the lifecycle of an app (MVC,WebApi)

nareshpatel avatar Oct 13 '17 20:10 nareshpatel

Changing BaseAddress will throw InvalidOperationException Additional information: This instance has already started one or more requests. Properties can only be modified before sending the first request.

yoav2 avatar Oct 18 '17 09:10 yoav2