Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Making scoped HTTP clients a public service #58770

Answered by danrot
danrot asked this question in Q&A
Discussion options

Hey, I have a a problem regarding scoped HTTP clients and my testing setup... I have configured a scope client like the following (replaced some sensitive data):

framework:
 http_client:
 scoped_clients:
 <some_service>.client:
 base_uri: '%env(<SOME_SERVICE>_URL)%'

This works as expected, however, I am having some issues with my tests. I get errors like these:

ServiceNotFoundException: The "<some_service>.client.uri_template" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.

I've seen that issue before, so I thought that should be as easy as marking this service public (since using dependency injection is not really an option in my test setup):

when@test:
 services:
 <some_service>.client.uri_template:
 public: true

However, then I get the following error:

The definition for "<some_service>.client.uri_template" has no class. If you intend to inject this service dynamically at runtime, please mark it as synthetic=true. If this is an abstract definition solely used by child definitions, please add abstract=true, otherwise specify a class to get rid of this error.

But now I am a bit confused... I don't really know how this service is built under the hood, but the way I remember it, is that just marking that service as public should not get rid of the class...

So my question is: how can I make that scoped HTTP client service public?

You must be logged in to vote

Ah, my bad, looks I have remembered that wrong... When defining services like this it indeed overrides the other stuff (like class) as well... What needs to be done is to make a public alias to avoid that the service is removed from the container:

when@test:
 services:
 test.<some_service>.client:
 alias: <some_service>.client
 public: true

Replies: 2 comments 2 replies

Comment options

Ah, my bad, looks I have remembered that wrong... When defining services like this it indeed overrides the other stuff (like class) as well... What needs to be done is to make a public alias to avoid that the service is removed from the container:

when@test:
 services:
 test.<some_service>.client:
 alias: <some_service>.client
 public: true
You must be logged in to vote
0 replies
Answer selected by danrot
Comment options

In my case scoped client <some_service>.client is not autowiring to the service where it's required, which is the same in both dev and test environment.
Instead a new HttpClient is created and wired to it. And if I explicitly bind the argument, it throws the same error you mentioned.
ServiceNotFoundException: The "<some_service>.client.uri_template" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.

What am I missing here?

You must be logged in to vote
2 replies
Comment options

If I am not mistaken, then the behavior you described is correct. Creating a scoped client will be another instance. So I am not sure why you would want to work around this.

Comment options

By the another instance I meant a new client, which is not the scoped client that I created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /