Реализация WCF SwaggerWCF - PullRequest
       14

Реализация WCF SwaggerWCF

0 голосов
/ 23 января 2019

Я настраиваю службу WCF и хочу реализовать SwaggerWCF для документации.

я настроен как SwaggerWCF документы. Но когда я хочу получить доступ к документу как http://localhost/RegistrationService/api-docs,, он перенаправляет меня на http://localhost/RegistrationService/api-docs/index.html?url=/RegistrationService/api-docs/swagger.json. и это дает ошибку 404.0.

Я добавил атрибуты Swagger в свою модель и методы. Далее я делюсь своей конфигурацией.

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="no@e.mail" />
      <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)));
        }

Почему я получаю ошибку 404?

Ответы [ 2 ]

0 голосов
/ 06 февраля 2019

Я имею в виду, что у вас есть служба DummyService, где конфигурация конечной точки для этой службы? Я вижу только вашу конечную точку для SwaggerWcf.ISwaggerWcfEndpoint.

И вам следует изменить свою конфигурацию в соответствии с вашим сервисом отдыха.

Ниже моя измененная конфигурация.

Сервисный интерфейс. Пожалуйста, обратите внимание на мой SwaggerWcfPath, это книги, соответствующие веб-сайту 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 параметр - это имя вашего svc, мой - 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();

    }
}

Мой web.config. Service.BookStore - это сервис, который активируется через Store.svc (параметр 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 голосов
/ 25 января 2019

Ваша конфигурация RouteTable.Routes - это api-docs, вы должны установить для нее «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" />

Мой контракт

[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);


}

Мой сервис.

[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();

    }
}

Результат enter image description here* * 1023

...