twitchax / AspNetCore.Proxy

ASP.NET Core Proxies made easy.
MIT License
524 stars 83 forks source link

NullReferenceException when run proxy in .Net core 2.2 #83

Closed moustafa11911 closed 3 years ago

moustafa11911 commented 3 years ago

Hello, I am getting this exception when run proxy

System.NullReferenceException: Object reference not set to an instance of an object. at AspNetCore.Proxy.Basic.GetEndpointFromComputerAsync(HttpContext context, EndpointComputerToValueTask computer) at AspNetCore.Proxy.HttpExtensions.ExecuteHttpProxyOperationAsync(HttpContext context, HttpProxy httpProxy) at AspNetCore.Proxy.Basic.ExecuteProxyOperationAsync(HttpContext context, Proxy proxy) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication1 application)`

the proxy code

app.RunProxy(proxy =>
proxy.UseHttp((context, args) =>
{
    if (context.Request.Path.StartsWithSegments("/api/yy"))
          return Configuration["AppSettings:ApiUrl"];
}));

I install latest package V 4.2.0 on my project which run using .NetCore 2.2 waiting your help as this is a showstopper issue. Thanks.

twitchax commented 3 years ago

This is likely caused by the fact that you need to return a string in the else clause. Can you try returning a string in the else clause?

moustafa11911 commented 3 years ago

thanks for reply, I use a default string at the end and the same issue exists

app.RunProxy(proxy =>
proxy.UseHttp((context, args) =>
{
    if (context.Request.Path.StartsWithSegments("/api/yy"))
          return Configuration["AppSettings:ApiUrl"];

    return Configuration["AppSettings:DefultApiUrl"];
}));
twitchax commented 3 years ago

Interesting. Can you try debug-printing those strings before you return them? Is it possible the Configuration is empty?

moustafa11911 commented 3 years ago

I checked the configuration and it is as the following

Configuration["AppSettings:ApiUrl"] => https://localhost:5001 Configuration["AppSettings:DefultApiUrl"] => https://localhost:3001

twitchax commented 3 years ago

Ok, thanks!

Did you print those values from within that closure? Basically, I want to know if the code inside that closure ever gets hit. If it does, then something along context.Request.Path is null.

If it doesn't, then it is clear the problem is here. Which means that, somehow, context.GetRouteData().Values is null.

twitchax commented 3 years ago

It looks like GetRouteData() can be null, so that may be the culprit. I will push a patch, and you can try it out!

twitchax commented 3 years ago

Can you please try the updated package?

moustafa11911 commented 3 years ago

Ok, will try it now

moustafa11911 commented 3 years ago

Fixed now, thanks a lot for your great support.