question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow HttpRequestMessage to accept relative URLs in service invocation

See original GitHub issue

Background

The Dapr SDK allows invoking service methods using an HttpRequestMessage parameter. However, this overload requires a fully qualified URL to the Dapr sidecar. Otherwise, an InvalidOperationException is thrown.

HttpRequestMessage httpRequest = new(input.HttpMethod, input.MethodName);
return await this.daprClient.InvokeMethodAsync<object?>(httpRequest);

Here is the exception:

System.InvalidOperationException
  HResult=0x80131509
  Message=An invalid request URI was provided. Either the request URI must be an absolute URI or BaseAddress must be set.
  Source=System.Net.Http
  StackTrace:
   at System.Net.Http.HttpClient.PrepareRequestMessage(HttpRequestMessage request)
   at System.Net.Http.HttpClient.CheckRequestBeforeSend(HttpRequestMessage request)
   at System.Net.Http.HttpClient.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at Dapr.Client.DaprClientGrpc.<InvokeMethodWithResponseAsync>d__27.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Dapr.Client.DaprClientGrpc.<InvokeMethodAsync>d__29`1.MoveNext()
    ...
    [Call Stack Truncated]

Generally speaking, code that already has a fully configured DaprClient should not need to provide redundant endpoint information for the Dapr sidecar. In many cases, it may not be practical for code to know what it is - for example, when getting the DaprClient from dependency injection, in which case the configuration comes from the config system.

Proposal

Remove the requirement in InvokeMethodAsync for the HttpRequestMessage parameter to use a fully qualified URL. The DaprClient implementation can construct the real URL itself using its own internally tracked endpoint. This will make this particular method much easier to use.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
rynowakcommented, Jul 13, 2022

Thanks for the feedback @cgillum

Irrespective of this actual proposal … I’d recommend using https://github.com/dapr/dotnet-sdk/blob/master/src/Dapr.Client/DaprClient.cs#L75 to create an HttpClient and just using the standard .NET APIs. There’s a wealth of existing libraries and knowledge for how to use HttpClient.

0reactions
rynowakcommented, Jul 13, 2022

Looking at some of the code in the SDK, it’s my understanding that the endpoint is inferred using the DAPR_GRPC_PORT environment variable. Ideally, I wouldn’t need to redundantly look this up myself if the SDK already knows how to look it up for me.

This is the case, if you omit the endpoint argument then the SDK will do it for you.

You’re not wrong. 😉 However, it seemed to me that the issues I’m running into could be encountered by app developers as well. I believe the patterns I’m following are pretty general. (the “framework” I’m building is actually just a throwaway POC, so these kinds of issues are not blockers, but I thought it might be valuable to raise them anyways).

Alright cool 😆 - feel free to reach out if you want to spend some time talking in details.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Get Absolute URL from Relative path (refactored method)
ToAbsolute(relativeUrl) allows the method to be declared as an extension in a static class. /// <summary> /// Converts the provided app-relative ...
Read more >
The Dapr service invocation building block
The HttpRequestMessage now has the following properties set: Url = http://127.0.0.1:3500/v1.0/invoke/orderservice/method/submit. HttpMethod = ...
Read more >
C# Only http and https schemes are allowed - HttpClient
Hello, I have a problem. using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri(CFG.Baseaddress); client.
Read more >
Creating and Using HTTP Client SDKs in .NET 6 - InfoQ
In this article, the author explains the process behind developing HTTP Client SDKs in .NET 6. The article also contains a sample SDK ......
Read more >
[Solved] Http WebRequest with relative url
You are dealing with invalid URI created by default from string, because URL is relative, but by default is is assumed it was...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found