将Swagger与WCF REST一起使用

sch*_*opy 4 rest wcf swagger

我有一个基于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
Run Code Online (Sandbox Code Playgroud)

什么是最好的继续方式,我没有使用WebApi,但如果有帮助,可以使用许多功能.

小智 19

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


sup*_*tor 9

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

我目前正在进行实施,但它还没有为黄金时段做好准备.基本规范看似简单,但适合WCF是一个明确的挑战.

  • 我们刚刚发布了它!我不知道黄金时段,但它可以在这里找到:https://github.com/superstator/Swaggeratr (3认同)

小智 5

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

\n\n
**1. Why Swagger4Wcf**\n----------------------\n\n\xe2\x80\xa2Manually writing yaml description for swagger and maintain it especially WCF services are boring. \n\xe2\x80\xa2There 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).\n\n2. How Swagger Works in the Background\n--------------------------------------\n\nSwagger4WCF uses NuPack post build pattern to trigger at build time.\nhttps://www.codeproject.com/Tips/1190360/How-to-setup-a-managed-postbuild-without-scripting\n3.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.\nSwagger4WCF detects **WebGet/WebInvoke** to provide Verb/Method in serialization style in yaml.\n\nSteps to implement Swagger in your application:-\n\n1. Install SwaggerWcf package\n\n2. Configure WCF routes\nWe have to add the route in the Application_Start method inside Global.asax\n\n     protected void Application_Start(object sender, EventArgs e)\n            {\n                RouteTable.Routes.Add(new ServiceRoute("v1/rest", new WebServiceHostFactory(), typeof(BookStore)));\n                RouteTable.Routes.Add(new ServiceRoute("api-docs", new WebServiceHostFactory(), typeof(SwaggerWcfEndpoint)));\n            }\n\n\n\nNote: Edit Web.config and add the following (if it doesn\'t exist yet) inside the system.serviceModel block\n\n    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>\n\n3. Configure WCF response auto types (optional)\nWe 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.\n\n\n\n\n\n\n      <behavior name="webHttpBehavior">\n              <webHttp defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true"/>\n            </behavior>\n          </endpointBehaviors>\n          <serviceBehaviors>\n            <behavior>\n              <!-- To avoid disclosing metadata information, set the values below to false before deployment -->\n              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>\n              <!-- 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 -->\n              <serviceDebug includeExceptionDetailInFaults="false"/>\n            </behavior>\n\n\n\n4. Decorate WCF services interfaces\nFor each method, we have to configure the WebInvoke or WebGet attribute, and add a SwaggerWcfPath attribute.\n\n\n\n [SwaggerWcfPath("Get book", "Retrieve a book from the store using its id")]\n        [WebGet(UriTemplate = "/books/{id}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json,\n            ResponseFormat = WebMessageFormat.Json)]\n        [OperationContract]\n        Book ReadBook(string id);\n\n\n\n\n5. Decorate WCF services class\n\n\xe2\x80\xa2   Add the SwaggerWcf and AspNetCompatibilityRequirements attributes to the class providing the base path for the service.\n\xe2\x80\xa2   For each method, add the SwaggerWcfTag to categorize the method and theSwaggerWcfResponse for each possible response from the service.\n\n\n\n[SwaggerWcfTag("Books")]\n[SwaggerWcfResponse(HttpStatusCode.OK, "Book found, value in the response body")]\n[SwaggerWcfResponse(HttpStatusCode.NoContent, "No books", true)]\npublic Book[] ReadBooks()\n{\n}\n\n\n\n6. Decorate data types used in WCF services\n\n\n\n [DataContract]\n    [Description("Book with title, first publish date, author and language")]\n    [SwaggerWcfDefinition(ExternalDocsUrl = "http://en.wikipedia.org/wiki/Book", ExternalDocsDescription = "Description of a book")]\n    public class Book\n    {\n        [DataMember]\n        [Description("Book ID")]\n        public string Id { get; set; }\n\n        [DataMember]\n        [Description("Book Title")]\n        public string Title { get; set; }\n\n        [DataMember]\n        [Description("Book First Publish Date")]\n        public int FirstPublished { get; set; }\n\n        [DataMember]\n        [Description("Book Author")]\n        public Author Author { get; set; }\n\n        [DataMember]\n        [Description("Book Language")]\n        public Language Language { get; set; }\n    }\n\n That\'s it wcf for Swagger implemented.\nPlease free if you face any issue.\n\nThanks,\nAbhi\n
Run Code Online (Sandbox Code Playgroud)\n