Burgyn / MMLib.SwaggerForOcelot

This repo contains swagger extension for ocelot.
MIT License
354 stars 94 forks source link

Failed to load API definition issue #266

Closed ljubomir-bacovic closed 1 year ago

ljubomir-bacovic commented 1 year ago

Hello,

I successfully installed Swagger for Ocelot and it runs fine on my localhost. However, when I publish to IIS server, I get this error.

I tried to find if anyone else had the same problem, but noticed that anyone who reported this issue, had it on localhost. I am puzzled why it works locally, but not on the server?

The gateway and microservices work, I can access them normally through Postman.

Burgyn commented 1 year ago

Hi @ljubomir-bacovic,

please provide more information. Some logs from your gateway, logs from browsers, ...

Thanks.

ljubomir-bacovic commented 1 year ago

The log from gateway:

RequestPath: /swagger/docs/v1/scheduling

An unhandled exception has occurred while executing the request.

Exception: System.Net.Http.HttpRequestException: Response status code does not indicate success: 404 (Not Found). at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode() at System.Net.Http.HttpClient.GetStringAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) at MMLib.SwaggerForOcelot.Repositories.DownstreamSwaggerDocsRepository.GetSwaggerJsonAsync(RouteOptions route, SwaggerEndPointOptions endPoint, String docsVersion) at MMLib.SwaggerForOcelot.Middleware.SwaggerForOcelotMiddleware.Invoke(HttpContext context, ISwaggerEndPointProvider swaggerEndPointRepository, IDownstreamSwaggerDocsRepository downstreamSwaggerDocs) at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.InvokeCore(HttpContext context, PathString matchedPath, PathString remainingPath) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

ljubomir-bacovic commented 1 year ago

Browser just shows 500 error fetching API definition.

image

Burgyn commented 1 year ago

This is still very general. Please provide your gateway configuration and try to explain how you host in IIS.

If you use Virtual directories look here: https://github.com/Burgyn/MMLib.SwaggerForOcelot#virtual-directory

Thank you.

ljubomir-bacovic commented 1 year ago

I don't use virtual directory. At this point, I host two sites, Gateway, which is publicly accessible through https, and one microservice that is accessed by gateway via http at the port 5001.

ljubomir-bacovic commented 1 year ago

My gateway configuration:

{ "Routes": [ { "UpstreamPathTemplate": "/api/scheduling/{everything}", "UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE" ], "DownstreamPathTemplate": "/api/{everything}", "DownstreamScheme": "http", "SwaggerKey": "scheduling", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5001 } ] } ] }

ljubomir-bacovic commented 1 year ago

Swagger endpoints:

{ "SwaggerEndPoints": [ { "Key": "scheduling", "Config": [ { "Name": "MyCorp.Services.Scheduling.API", "Version": "v1", "Url": "http://localhost:5001/swagger/v1/swagger.json" } ] } ] }

ljubomir-bacovic commented 1 year ago

@Burgyn any idea?

Burgyn commented 1 year ago

At the moment, I can't think of anything you might have a problem with. It normally works on IIS. Please check all addresses and ports to see if they match. Also check the logs in the browser (network tab) to see what kind of query the SwaggerUI is sending internally, if there has been any change in the base address.

ljubomir-bacovic commented 1 year ago

This is my Network tab

image

Burgyn commented 1 year ago

Hey @ljubomir-bacovic .

I really want to help you, but without relevant information and your proper investigation it will not work.

Check all logs and configurations properly (ocelot.json and Startup.cs). Check if you can get the swagger.json for the downstream service directly through the browser http://localhost:5001/swagger/v1/swagger.json ...

Friendly advice. If you are asking for help somewhere then really try to give as much information which can help. Also try to format things sensibly. If something is code (even JSON) then format it as code until you can read things. Thank you very much. See markdown docs

ljubomir-bacovic commented 1 year ago

@Burgyn

I tried to explain as much as I could. Also, I spent two full days investigating this issue and trying to understand it. There is not much information online, since locally everything works perfectly when I start from Visual Studio. Also, the gateway and the API work on the server as well, we use them to develop frontend. But we would like to have Swagger deployed, so that the frontend team can understand the API better.

Burgyn commented 1 year ago

From this url http://localhost:5001/swagger/v1/swagger.json you get correct swagger.json? If so, try looking at the logs from the gateway to see what address it was trying to forward it to.

I can't help more. So far I haven't had a problem with IIS.

ljubomir-bacovic commented 1 year ago

No, when I try this on the server, I get 404 error.

ljubomir-bacovic commented 1 year ago

So that means that the issue is that Swagger is not working on API level. I will try to investigate that.

ljubomir-bacovic commented 1 year ago

Yes, that was the issue. Thanks a lot for your help. It works like a charm now!

Burgyn commented 1 year ago

That's good to hear.