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

Non-Dapr endpoints invocation supported?

Open ZenwalkerD opened this issue 1 year ago • 6 comments

Ask your question here

Dear All,

In the Dapr documentation i noticed that a service can invoke a Non-Dapr endpoints like https://reqres.in/api/users?page=2 (external APIs).

But when i checked the Dotnet Dapr documentation; i could not find any code related to that.

I tried below code and it does not work..

 var client = DaprClient.CreateInvokeHttpClient("https://reqres.in");
 var response = client.GetAsync(client.BaseAddress + "api/users?page=1").Result;
 var result = response.Content.ReadAsStringAsync().Result;

Please let me know if the support is planned or where can i find a documentation?

ZenwalkerD avatar Jun 20 '24 07:06 ZenwalkerD

Using Dapr for service invocation against Dapr endpoints gives you service discoverability. If you don't need the discoverability and know the port of what you're trying to access, it gives you a mechanism to attempt invoking a REST endpoint using an HTTP-based invocation.

In your example, you're going a step further and accessing a completely external API - might I ask why you're bothering with the DaprClient and not just using an HttpClient to invoke it like you would any other REST endpoint?

WhitWaldo avatar Jun 24 '24 05:06 WhitWaldo

I tried below code and it does not work..

@ZenwalkerD Can you be more specific about what didn't work? Did the call fail, and in what way? Could you provide a repro code/project that demonstrates the issue?

philliphoff avatar Jun 25 '24 16:06 philliphoff

In your example, you're going a step further and accessing a completely external API - might I ask why you're bothering with the DaprClient and not just using an HttpClient to invoke it like you would any other REST endpoint?

Because of Daprs feature of resiliency. Dapr provides retries and other policies. So i was hoping to make use of all those features in invoking external network calls. Otherwise i have to rely on some libraries like Polly.net.

ZenwalkerD avatar Jul 01 '24 06:07 ZenwalkerD

I tried below code and it does not work..

@ZenwalkerD Can you be more specific about what didn't work? Did the call fail, and in what way? Could you provide a repro code/project that demonstrates the issue? This is what i am getting when i deploy my app on Azure ACA:

{"errorCode":"ERR_DIRECT_INVOKE","message":"failed to invoke, id: https, err: rpc error: code = Unimplemented desc = "}

Code is very simple. Plain ASP NET CORE WEB API project with a GET controller having above provided code. Assuming that all dependencies are added for Dapr and others.

ZenwalkerD avatar Jul 01 '24 06:07 ZenwalkerD

@ZenwalkerD You'll need to provide more context; for example, how was the application and Dapr sidecar started, what do the Dapr sidecar logs show (with --log-level debug), what is the full address of the GET call being made, etc.

philliphoff avatar Jul 01 '24 16:07 philliphoff

@ZenwalkerD You'll need to provide more context; for example, how was the application and Dapr sidecar started, what do the Dapr sidecar logs show (with --log-level debug), what is the full address of the GET call being made, etc.

Sorry for the delay in response.

I am now running on local via Dapr CLI tool.

I have below yaml config:

version: 1
common: # optional section for variables shared across apps
  env: # any environment variable shared across apps
    ASPNETCORE_ENVIRONMENT: Development
apps:
  - appID: webapp # optional
    appDirPath: ./FrontEndApi/ # REQUIRED
    appProtocol: http
    appPort: 8080
    command: ["dotnet", "run", "--project", "WebApi-Dapr.csproj", "--urls", "http://localhost:5000"]
    daprdLogDestination: console
    logLevel: debug
  - appID: backendapi # optional
    appDirPath: ./BackendAPI/ # REQUIRED
    appProtocol: http
    appPort: 8080
    command: ["dotnet", "run", "--project", "BackendAPI.csproj","--urls", "http://localhost:5001"]

when i run command: dapr run -f . (dir where yaml exist)

everything runs normal. I am able to access my app and execute the API. I do not see any errors being logged into file nor console.

Btw i tried changing the API invocation code as shown:

 HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "https://reqres.in/api/users?page=1");
 var response = await m_DaprClient.InvokeMethodWithResponseAsync(httpRequestMessage);
 var result = await response.Content.ReadAsStringAsync();

With above code; no error logs were thrown by dapr.

However; when i changed back to original code in my first post here; then i got this error:

time="2024-08-06T23:22:57.9957978+05:30" level=debug msg="HTTP service invocation failed to complete with error: invokeError (statusCode='500') msg='{\"errorCode\":\"ERR_DIRECT_INVOKE\",\"message\":\"failed to invoke, id: https, err: couldn't find service: https\"}'" app_id=webapp instance=md3kgf9c scope=dapr.runtime.http type=log ver=1.13.5

ZenwalkerD avatar Aug 06 '24 17:08 ZenwalkerD