0

I am using a library called SwaggerWcf to generate Swagger definitions for my application hosting a WCF REST API. I have finally made it work as I want it with the following code:

  var swaggerHost = new WebServiceHost(typeof(SwaggerWcfEndpoint));
                var endpoint =
                    new WebHttpEndpoint(
                        ContractDescription.GetContract(typeof(ISwaggerWcfEndpoint)),
                        new EndpointAddress("http://localhost/docs"))
                    {
                        AutomaticFormatSelectionEnabled = true,
                        FaultExceptionEnabled = true
                    };

                // required to generate the swagger content
                var e = new SwaggerWcfEndpoint();

                swaggerHost.AddServiceEndpoint(endpoint);

                var apiHost = new WebServiceHost(typeof(RestDataInterface));
                var endpoint2 =
                    new WebHttpEndpoint(
                        ContractDescription.GetContract(typeof(IRestDataInterface)),
                        new EndpointAddress("http://localhost/api"))
                    {
                        AutomaticFormatSelectionEnabled = true,
                        FaultExceptionEnabled = true
                    };


            apiHost.AddServiceEndpoint(endpoint2);

            swaggerHost.Open();
            apiHost.Open();

My question now is: is this the right way of doing it? As you can see I am creating two WebServiceHost instances. One for swagger and one for my actual API. While this seems to work fine, am I missing something? This API is intended to be fast but does not need to handle many concurrent users.

Thank you

Smith5727
  • 402
  • 4
  • 15
  • You also need to add webhttpBehavior to the behavior of the endpoint. For detailed information about webhttpBehavior, you can refer to this link:https://learn.microsoft.com/en-us/dotnet/api/system.servicemodel.description.webhttpbehavior?view=netframework-4.8 – Ding Peng Sep 11 '20 at 01:48
  • Why would I need that? This above works exactly like I want it to. Either way that doesnt answer my initial concern about running two WebServiceHost. – Smith5727 Sep 11 '20 at 14:03

0 Answers0