Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpClient created by DaprClient.CreateInvokeHttpClient can't find the target service to invoke #937

Closed
bledbet opened this issue Aug 25, 2022 · 4 comments
Assignees
Labels
area/client kind/bug Something isn't working
Milestone

Comments

@bledbet
Copy link

bledbet commented Aug 25, 2022

Per the recommendation here:
https://docs.microsoft.com/en-us/dotnet/architecture/dapr-for-net-developers/service-invocation#invoke-http-services-using-httpclient

I am trying to use the HttpClient that is created by DaprClient.CreateInvokeHttpClient() to make a service invocation call in a self-hosted dapr environment. I was successful in modifying the "service_invocation" quickstart by modifying Program.cs in the checkout app, replacing the whole section before the 'for' loop that creates and configures an HttpClient with just

var client = DaprClient.CreateInvokeHttpClient("orderProcessor");

and that worked fine.

Now I am trying to add service invocation to a larger demo that already has some other working api endpoints, state store, and pubsub with working subscriber services, and I can't seem to get it to work with the HttpClient that is produced by tDaprClient.CreateInvokeHttpClient().

In my larger demo, if I build the HttpClient as the original "service_involcation" quickstart did, like so:

    private static HttpClient CreateHttpClientForDaprApp(string appId)
    {
        var daprBaseUrl = (Environment.GetEnvironmentVariable("BASE_URL") ?? "http://localhost") + ":" + (Environment.GetEnvironmentVariable("DAPR_HTTP_PORT") ?? "3500");

        var httpClient = new HttpClient();
        httpClient.BaseAddress = new Uri(daprBaseUrl);
        httpClient.DefaultRequestHeaders.Add("dapr-app-id", appId);
        httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

        return httpClient;
    }

my service invocation call works fine, but if I replace this with a call to DaprClient.CreateInvokeHttpClient(appId), I get 500 error responses like

{"errorCode":"ERR_DIRECT_INVOKE","message":"fail to invoke, id: orderapi, err: couldn't find service: orderapi"}

and

{"errorCode":"ERR_DIRECT_INVOKE","message":"fail to invoke, id: orderapi, err: timeout waiting for address for app id orderapi"}

The service that is trying to make the invocation call is a subscriber to an event, and what I've commonly seen is that the first time it tries to process the event it gets that first error response message above, and upon retry it gets the second response message.

The command being used to launch the Order API service is

dapr run --app-id orderApi --app-port 8001 --app-protocol http --app-ssl --dapr-http-port 9001 --components-path ..\dapr\components\ dotnet run

Any thoughts about what might be wrong?

@halspang
Copy link
Contributor

Thanks for opening this issue!

I wonder if it's something as simple as a casing mismatch? You launch it with orderApi but it's sayin git can't find orderapi. I tried this locally (using the standard DaprClient and casing did have an impact.

@halspang halspang added kind/bug Something isn't working area/client labels Aug 29, 2022
@bledbet
Copy link
Author

bledbet commented Aug 30, 2022

@halspang Yes, it does appear to be an issue with a casing mismatch. I had consistently used the casing "orderApi" throughout my code, and it behaves as though DaprClient modified the casing that I passed to it in the call to CreateInvokeHttpClient() such that it no longer matched.

I changed my code to consistently use "orderapi" throughout, and it works using the HttpClient that is created by DaprClient.CreateInvokeHttpClient().

I guess that doesn't explain why "orderProcessor" in the "service-invocation" quickstart worked though.

@TWEESTY
Copy link
Contributor

TWEESTY commented Jan 12, 2024

The "appId" is used to set the "BaseAddress" of the "HttpClient" instance. DaprClient does not change the case when creating the URI for the "BaseAddress". URI is not case-sensitive, that's the point.

I don't know how we can resolved this "bug", maybe at least we should throw a exception if "appId" contains an upper letter in order to inform that upper case is not managed with "HttpClient" (it will save time to a lot of developers).

Here the source file

@philliphoff
Copy link
Contributor

Closed with fix in #1233

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/client kind/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants