【问题标题】:Using Swagger with WCF REST将 Swagger 与 WCF REST 一起使用
【发布时间】:2013-06-08 00:46:14
【问题描述】:

我有一个基于 WCF REST 的服务,我想将 Swagger 添加到其中。我已经安装了 Swagger.NET 包,我的项目使用的是 .NET 4.0。我还启用了 XML 文档等...但现在我不确定从这里出发的最佳路线。

当我启动http://localhost/SwaggerUI/index.html 时,我会看到带有http://YOUR-URL-HERE:PORT/api/swagger 的默认页面。我不确定这里应该放什么网址。我确实通过以下方式启用了该服务的帮助:<standardEndpoint name="" helpEnabled="true" />,这确实给了我丑陋的页面:http://localhost/api/help——当我将它插入 Swagger 时,我得到了结果:

200 : OK http://localhost/api/help

最好的方法是什么,我没有使用 WebApi,但如果有帮助的话,我有很多可用的功能。

【问题讨论】:

    标签: wcf rest swagger


    【解决方案1】:

    目前没有针对 WCF 的 Swagger 实现。您要么需要按照 https://github.com/wordnik/swagger-core/wiki 的规范来实现自己的规范,要么耐心等待有人为您实现。

    我目前正在进行实施,但还没有准备好迎接黄金时段。基本规范看似简单,但要使其适应 WCF 却是一项挑战。

    【讨论】:

    • 你好,superstator,我想你的实现还没有准备好迎接黄金时段?
    • 实际上,我们刚刚发布了它!我不知道黄金时段,但可以在这里找到:github.com/superstator/Swaggeratr
    • @superstator:SampleService 项目文件夹缺少 swagger-ui/dist/...关于如何获取完整的“dist”文件夹内容的任何线索?
    • @AdrianNasui 您应该只需要进行子模块更新。不过,我忘记了确切的命令行。
    • 我得到了一个新版本的 swagger-ui 并把它放在那里,所以通过了那部分。但是,当为 SampleService 运行 swagger 时,我得到一个仅包含“[ base url:]”的空页面。有什么建议吗?
    【解决方案2】:

    截至 2015 年 12 月,现在已经实现了 Swagger for Wcf。你应该检查一下 - WCF 的 Swagger - SwaggerWcf

    【讨论】:

      【解决方案3】:

      我尝试在我的应用程序中为 WCF 实现 swagger,因为与我们为 Web API 植入的实现不同(更简单)。我将逐步告诉你它的不同之处以及它是如何工作的:-

      **1. Why Swagger4Wcf**
      ----------------------
      
      •Manually writing yaml description for swagger and maintain it especially WCF services are boring. 
      •There is a nuget package called Swagger4WCF that automatically generates yaml description for swagger 2.0 for each interface matching attributes used by WCF (ServiceContract/OperationContract/WebGet/WebInvoke).
      
      2. How Swagger Works in the Background
      --------------------------------------
      
      Swagger4WCF uses NuPack post build pattern to trigger at build time.
      https://www.codeproject.com/Tips/1190360/How-to-setup-a-managed-postbuild-without-scripting
      3.At build time, it will detect assemblies present in output directory, open them with mono.cecil (to reflect assemblies) to generate expected yaml description for swagger 2.0.
      Swagger4WCF detects **WebGet/WebInvoke** to provide Verb/Method in serialization style in yaml.
      
      Steps to implement Swagger in your application:-
      
      1. Install SwaggerWcf package
      
      2. Configure WCF routes
      We have to add the route in the Application_Start method inside Global.asax
      
           protected void Application_Start(object sender, EventArgs e)
                  {
                      RouteTable.Routes.Add(new ServiceRoute("v1/rest", new WebServiceHostFactory(), typeof(BookStore)));
                      RouteTable.Routes.Add(new ServiceRoute("api-docs", new WebServiceHostFactory(), typeof(SwaggerWcfEndpoint)));
                  }
      
      
      
      Note: Edit Web.config and add the following (if it doesn't exist yet) inside the system.serviceModel block
      
          <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
      
      3. Configure WCF response auto types (optional)
      We have to add the following to Web.config. This will allow the WCF service to accept requests and send replies based on the Content-Type headers.
      
      
      
      
      
      
            <behavior name="webHttpBehavior">
                    <webHttp defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true"/>
                  </behavior>
                </endpointBehaviors>
                <serviceBehaviors>
                  <behavior>
                    <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
                    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
                    <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
                    <serviceDebug includeExceptionDetailInFaults="false"/>
                  </behavior>
      
      
      
      4. Decorate WCF services interfaces
      For each method, we have to configure the WebInvoke or WebGet attribute, and add a SwaggerWcfPath attribute.
      
      
      
       [SwaggerWcfPath("Get book", "Retrieve a book from the store using its id")]
              [WebGet(UriTemplate = "/books/{id}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json,
                  ResponseFormat = WebMessageFormat.Json)]
              [OperationContract]
              Book ReadBook(string id);
      
      
      
      
      5. Decorate WCF services class
      
      •   Add the SwaggerWcf and AspNetCompatibilityRequirements attributes to the class providing the base path for the service.
      •   For each method, add the SwaggerWcfTag to categorize the method and theSwaggerWcfResponse for each possible response from the service.
      
      
      
      [SwaggerWcfTag("Books")]
      [SwaggerWcfResponse(HttpStatusCode.OK, "Book found, value in the response body")]
      [SwaggerWcfResponse(HttpStatusCode.NoContent, "No books", true)]
      public Book[] ReadBooks()
      {
      }
      
      
      
      6. Decorate data types used in WCF services
      
      
      
       [DataContract]
          [Description("Book with title, first publish date, author and language")]
          [SwaggerWcfDefinition(ExternalDocsUrl = "http://en.wikipedia.org/wiki/Book", ExternalDocsDescription = "Description of a book")]
          public class Book
          {
              [DataMember]
              [Description("Book ID")]
              public string Id { get; set; }
      
              [DataMember]
              [Description("Book Title")]
              public string Title { get; set; }
      
              [DataMember]
              [Description("Book First Publish Date")]
              public int FirstPublished { get; set; }
      
              [DataMember]
              [Description("Book Author")]
              public Author Author { get; set; }
      
              [DataMember]
              [Description("Book Language")]
              public Language Language { get; set; }
          }
      
       That's it wcf for Swagger implemented.
      Please free if you face any issue.
      
      Thanks,
      Abhi
      

      【讨论】:

      • 我刚刚使用 AbeSilva 的 SwaggerWcf(nuget 包)在 wcf 中实现了 swagger。最新版本的 nugget 包目前无法使用,我们可以试用 0.1.23 版本。 (安装包 swaggerwcf -version 0.1.23)
      • 能不能分享一下你集成的方式我也需要
      • 我已经分享了上面例子中的步骤。请一步一步来,你将能够实现。如有任何疑问,请联系我们。
      • 0.1.23版本更新这么多,你怎么回去那么远?其他的也不行?我同意我尝试了最新的 0.2.15,但它对我不起作用。
      • 我已经提到最新版本无法正常工作,所以请坚持使用正常的版本。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      • 2013-11-06
      • 1970-01-01
      相关资源
      最近更新 更多