0
votes

I m setting up a WCF service and i want to implement SwaggerWCF for the documantation.

i configured like SwaggerWCF docs. But when i want to access doc as http://localhost/RegistrationService/api-docs, it redirects me to http://localhost/RegistrationService/api-docs/index.html?url=/RegistrationService/api-docs/swagger.json . and it is giving 404.0 error.

I added Swagger Attributes in my Model and methods.In the following I am sharing my configuration.

My app.config

<configSections>
    <section name="swaggerwcf" type="SwaggerWcf.Configuration.SwaggerWcfSection, SwaggerWcf" />
  </configSections>
  <swaggerwcf>
    <tags>
      <tag name="LowPerformance" visible="false" />
    </tags>
    <settings>
      <setting name="InfoDescription" value="Sample Service to test SwaggerWCF" />
      <setting name="InfoVersion" value="0.0.1" />
      <setting name="InfoTermsOfService" value="Terms of Service" />
      <setting name="InfoTitle" value="SampleService" />
      <setting name="InfoContactName" value="Abel Silva" />
      <setting name="InfoContactUrl" value="http://github.com/abelsilva" />
      <setting name="InfoContactEmail" value="[email protected]" />
      <setting name="InfoLicenseUrl" value="https://github.com/abelsilva/SwaggerWCF/blob/master/LICENSE" />
      <setting name="InfoLicenseName" value="Apache License" />
    </settings>
  </swaggerwcf>

Global Asax

 protected void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.Add(new ServiceRoute("api-docs", new WebServiceHostFactory(), typeof(SwaggerWcfEndpoint)));
        }

Why am i getting 404 error?

3

3 Answers

1
votes

I mean you have a service DummyService, where is the endpoint configuration for that service? I only see your endpoint for SwaggerWcf.ISwaggerWcfEndpoint.

And you should modify your configuration according to your rest service.

Below is my modified configuration.

Service Interface. Please pay attention to my SwaggerWcfPath,it is books corresponding to the webget uritemplate ""/books?filter={filtername}""

namespace ServiceInterface
{

[ServiceContract]
 public interface IStore
{


    [SwaggerWcfPath("/books", "Retrieve all books from the store")]

    [WebGet(UriTemplate = "/books?filter={filtername}", BodyStyle = WebMessageBodyStyle.Bare)]

   // [OperationContract]

    Book[] ReadBooks(string filtername=null);


}
}

MyService SwaggerWcf's parameter is the name of your svc, mine is Store.svc

    [AspNetCompatibilityRequirements(RequirementsMode =   AspNetCompatibilityRequirementsMode.Allowed)]
   [SwaggerWcf("Store.svc")]
public class BookStore : IStore
{
       [SwaggerWcfTag("Books")]

    [SwaggerWcfResponse(HttpStatusCode.OK, "Book found, value in the response body")]

   [SwaggerWcfResponse(HttpStatusCode.NoContent, "No books", true)]

    public Book[] ReadBooks(string filtername=null)

    {

        WebOperationContext woc = WebOperationContext.Current;



        if (woc == null)

            return null;



        if (Store.Books.Any())

        {

            woc.OutgoingResponse.StatusCode = HttpStatusCode.OK;

            return string.IsNullOrEmpty(filtername)

                ? Store.Books.ToArray()

                : Store.Books.Where(b => b.Author.Name.Contains(filtername) || b.Title.Contains(filtername)).ToArray();

        }



        woc.OutgoingResponse.StatusCode = HttpStatusCode.OK;

        return Store.Books.ToArray();

    }
}

My web.config. Service.BookStore is the service which is activated through Store.svc(parameter of SwaggerWcf)

 <service name="SwaggerWcf.SwaggerWcfEndpoint">
    <endpoint address="" binding="webHttpBinding" contract="SwaggerWcf.ISwaggerWcfEndpoint"></endpoint>
  </service>


<service name="Service.BookStore">
    <endpoint address="" binding="webHttpBinding" contract="ServiceInterface.IStore" behaviorConfiguration="web" ></endpoint>
 <endpointBehaviors>

    <behavior name="web">
      <webHttp automaticFormatSelectionEnabled="true"/>
    </behavior>
  </endpointBehaviors>
0
votes

Your configuration of RouteTable.Routes is api-docs, you should set it to "RegistrationService/api-docs".

My Application_Start

RouteTable.Routes.Add(new ServiceRoute("RegistrationService/api-docs", new WebServiceHostFactory(), typeof(SwaggerWcfEndpoint)));

My web.config.

 <directoryBrowse enabled="true"/>
<modules runAllManagedModulesForAllRequests="true"/>

 <service name="SwaggerWcf.SwaggerWcfEndpoint">
    <endpoint address="" binding="webHttpBinding"      contract="SwaggerWcf.ISwaggerWcfEndpoint"></endpoint>
  </service>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  multipleSiteBindingsEnabled="true" />

My Contract

[ServiceContract]
public interface IStore
{


    [SwaggerWcfPath("Get books", "Retrieve all books from the store")]

    [WebGet(UriTemplate = "/books?filter={filterText}", BodyStyle = WebMessageBodyStyle.Bare)]

    [OperationContract]

    Book[] ReadBooks(string filterText = null);


}

My service.

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[SwaggerWcf("/v1/rest")]
public class BookStore : IStore
{

    [SwaggerWcfTag("Books")]

    [SwaggerWcfResponse(HttpStatusCode.OK, "Book found, value in the response body")]

    [SwaggerWcfResponse(HttpStatusCode.NoContent, "No books", true)]

    public Book[] ReadBooks(string filterText = null)

    {

        WebOperationContext woc = WebOperationContext.Current;



        if (woc == null)

            return null;



        if (Store.Books.Any())

        {

            woc.OutgoingResponse.StatusCode = HttpStatusCode.OK;

            return string.IsNullOrEmpty(filterText)

                ? Store.Books.ToArray()

                : Store.Books.Where(b => b.Author.Name.Contains(filterText) || b.Title.Contains(filterText)).ToArray();

        }



        woc.OutgoingResponse.StatusCode = HttpStatusCode.OK;

        return Store.Books.ToArray();

    }
}

The result enter image description here

0
votes

add the decorator for the call path in the service

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[SwaggerWcf("/api")]

"/api" replace the correct name