Nasir
Nasir

Reputation: 59

Web Api documentation with swashbuckle

We are currently trying to start writing WebApi services for our products switching from traditional WCF SOAP based services. The challenge we have got is how to provide the api documentation. I came across the SwaggerUi/swash buckle. One limitation we have is we do not want to host the WebApi services in IIS but in a Windows Service. I am new to Web Api so I might be doing things the wrong way.

So for testing, I am hosting the web api in a console application. I can use the HttpClient to invoke the Get method on the Web Api but I can't access the same if I type the url in a web browser (is this normal for self hosted web api?).

So I installed the Swashbuckle.core nuget package and included the following code in the Startup class (Owin selfhosted).

 var config = new HttpConfiguration();

        config
            .EnableSwagger(c =>
            {
                c.IncludeXmlComments(GetXmlCommentsPath());
                c.SingleApiVersion("v1", "WebApi");
                c.ResolveConflictingActions(x => x.First());
            })
            .EnableSwaggerUi();

 private static string GetXmlCommentsPath()
    {
        var path = $@"{AppDomain.CurrentDomain.BaseDirectory}\WebApiHost.XML";

        return path;
    }

When I browse to the following location http://localhost:5000/swagger/ui/index I get "page cannot be displayed" in IE. Similar for chrome. Is there anything special that needs to be done when hosting a WebApi in a console/windows service application to get the documentation automatically?

(I have enabled Xml documentation for the project)

I have now attached a test project. Please follow the link below:

Test project

Regards,

Nas

Upvotes: 0

Views: 1548

Answers (1)

strickt01
strickt01

Reputation: 4048

Your problem is not with Swashbuckle, which is configured correctly. Instead it is with the fact that your OWin web app has closed by the time the browser navigates to the swagger URI. Your using statement means that the web app is shut down at the end of it - well before Chrome has opened and navigated to the swagger path. You need to ensure the web app is still running - then your path will be valid (although in your source code you have different ports 9000 and 5000 in your url variables).

Upvotes: 1

Related Questions