dapr / dotnet-sdk

Dapr SDK for .NET
Apache License 2.0
1.11k stars 336 forks source link

Non-Dapr endpoints invocation supported? #1311

Open ZenwalkerD opened 4 months ago

ZenwalkerD commented 4 months ago

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?

WhitWaldo commented 4 months ago

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?

philliphoff commented 4 months ago

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?

ZenwalkerD commented 4 months ago

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 commented 4 months ago

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.

philliphoff commented 4 months ago

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

ZenwalkerD commented 3 months ago

@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