dotnet-sdk icon indicating copy to clipboard operation
dotnet-sdk copied to clipboard

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

Open bledbet opened this issue 3 years ago • 2 comments

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?

bledbet avatar Aug 25 '22 20:08 bledbet

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 avatar Aug 29 '22 17:08 halspang

@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.

bledbet avatar Aug 30 '22 19:08 bledbet

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

TWEESTY avatar Jan 12 '24 15:01 TWEESTY

Closed with fix in #1233

philliphoff avatar Feb 16 '24 17:02 philliphoff