为什么选择 Swagger4Wcf
•手动为swagger编写yaml描述并维护它,尤其是WCF服务很无聊。
•有一个名为 Swagger4WCF 的 nuget 包,它会为 WCF 使用的每个接口匹配属性自动生成 swagger 2.0 的 yaml 描述 (ServiceContract/OperationContract/WebGet/WebInvoke)。
2。 Swagger 如何在后台工作
Swagger4WCF 使用 NuPack 后构建模式在构建时触发。
https://www.codeproject.com/Tips/1190360/How-to-setup-a-managed-postbuild-without-scripting
- 在构建时,它将检测输出目录中存在的程序集,使用 mono.cecil 打开它们(以反映程序集)以生成预期的 yaml 描述 swagger 2.0。
Swagger4WCF 检测 WebGet/WebInvoke 以在 yaml 中提供序列化样式的 Verb/Method。
在您的应用程序中实现 Swagger 的步骤:
安装 SwaggerWcf 包
配置 WCF 路由
我们必须在 Global.asax 中的 Application_Start 方法中添加路由
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)));
}
注意:编辑 Web.config 并在 system.serviceModel 块中添加以下内容(如果尚不存在)
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
- 配置 WCF 响应自动类型(可选)
我们必须将以下内容添加到 Web.config。这将允许 WCF 服务接受请求并根据 Content-Type 标头发送回复。
<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>
-
装饰 WCF 服务接口
对于每个方法,我们都要配置 WebInvoke 或 WebGet 属性,并添加一个 SwaggerWcfPath 属性。
[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);
装饰 WCF 服务类
• 将 SwaggerWcf 和 AspNetCompatibilityRequirements 属性添加到为服务提供基本路径的类。
• 对于每个方法,添加 SwaggerWcfTag 以对方法进行分类,并为来自服务的每个可能响应添加 SwaggerWcfResponse。
[SwaggerWcfTag("Books")]
[SwaggerWcfResponse(HttpStatusCode.OK, "Book found, value in the response body")]
[SwaggerWcfResponse(HttpStatusCode.NoContent, "No books", true)]
public Book[] ReadBooks()
{
}
-
装饰 WCF 服务中使用的数据类型
[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; }
}
参考:-https://github.com/abelsilva/swaggerwcf
这就是 Swagger 实现的 wcf。
如果您遇到任何问题,请免费。
谢谢,
阿比