man-group / dapr-sidekick-dotnet

Dapr Sidekick for .NET - a lightweight lifetime management component for Dapr
Apache License 2.0
175 stars 21 forks source link

HTTP Error 400. The request hostname is invalid when invoking api #26

Closed maaxxxxx closed 2 years ago

maaxxxxx commented 2 years ago

Expected Behavior

Expect Http 200 and view dapr logs in the VS console

Actual Behavior

HTTP Error 400. The request hostname is invalid when invoking an api thru the sidecar on a ASP.NET Web Api (.NET Framework 4.7.2)

Steps to Reproduce the Problem

Create a new ASP.NET Web Api in Visual Studio 2019 Add Man.Dapr.Sidekick and Man.Dapr.Sidekick.Extensions.Logging Nuget packages Add dapr setup in Global.asx Add tests methods in Controllers Call api: curl http://localhost:3500/v1.0/invoke/dotnetframeworkpublisher/method/api/test

The dapr metrics, health, and status looks good but no dapr logs in the console Everything works as expected with .Net Core

badgeratu commented 2 years ago

Can you please upload and share to GitHub a complete project that illustrates this issue?

maaxxxxx commented 2 years ago

Hi Simon,

Thanks for the quick reply. You can find the solution here: https://github.com/maaxxxxx/DotNetFrameworkPublisher.git

To reproduce:

  1. Run the project
  2. Go to http://localhost:58717/publisher/CreateOrder

Great project btw. Max

badgeratu commented 2 years ago

Hi Max, sorry for the delay in responding. I've taken a look at this and I think the issue is actually between Dapr and ASP.NET Web API. To eliminate Sidekick entirely, I ran Dapr standalone from the command-line:

dapr run --app-id dotnetframeworkpublisher --app-port 58717 --log-level debug --dapr-http-port 3500

Then when you run your application, Dapr Sidekick will automatically attach to this running Dapr instance which allows the Dapr Sidecar to discover your application.

You can then invoke your test endpoint directly through the Dapr Sidecar using the following URL through a browser (I used Microsoft Edge):

http://localhost:3500/v1.0/invoke/dotnetframeworkpublisher/method/publisher/test

This gives you the same HTTP Error 400. The request hostname is invalid you reported. However please note that this is not using Sidekick in any way - this is the Dapr Sidecar attempting to invoke your Controller method via its own HTTP interface.

Unfortunately I am not really familiar with ASP.NET Web API so can't provide much guidance here, and the Dapr sidecar logs aren't providing much help. If you can't think of a solution then I suggest you reach out to the Dapr maintainers to see if they can help.

As for the logs, with your setup you won't see any because the only logging provider we included was for Microsoft.Extensions.Logging (the one used by ASP.NET Core). However it is extensible, you just need to implement your own IDaprLoggerFactory and IDaprLogger and register it with Sidekick. Here's an example implementation - if you want to contribute one back for ASP.NET Web API that would be awesome!

akourbat commented 2 years ago

I am using the example from Dapr community call on Sidekick https://www.youtube.com/watch?v=Cv-8e5wV44g&t=1758s and it also blows up on me when I do

[HttpGet("dapr")]

        public async Task<IEnumerable<WeatherForecast>> GetViaSidecar(
            [FromServices] IDaprSidecarHost host,
            [FromServices] IDaprSidecarHttpClientFactory factory)
        {
            var appId = host.GetProcessOptions().AppId;
            if (appId == null)
            {
                return null;
            }
            var httpClient = factory.CreateInvokeHttpClient(appId);
            var result = await httpClient.GetStringAsync("/weatherforecast");

            return JsonConvert.DeserializeObject<IEnumerable<WeatherForecast>>(result);
        }

with System.Net.Http.HttpRequestException: Response status code does not indicate success: 400 (Bad Request). It looks like sidecar launched by sidekick is misbehaving for some reason. I used .net core 5 web-api template. status endpoint runs properly and shows no issues.

akourbat commented 2 years ago

Update, the issue seems to be the IIS. If I run the debug without it, it seems to work fine

badgeratu commented 2 years ago

@akourbat Yours is a different issue to that raised by @maaxxxxx . I'm going to close this issue as I don't believe it's related to Sidekick, please raise another one if you continue to have difficulty.