How to generate basic swagger yaml description for WCF automatically on build time with Swagger4WCF .NET 4.0+

0
41

Introduction

It can be very boring to write manually yaml description for swagger and maintain it especially when your WCF services are very simple. There is a nuget package called Swagger4WCF that automatically generate yaml description for swagger 2.0 for each interfaces matching with attributes used by WCF (ServiceContract/OperationContract/WebGet/WebInvoke). 

How to use Swagger4WCF

There is only few step required to test it : 

  1. Create a library with WCF interface
     
    [DataContract]
    public class Book
    { [DataMember] public int Id { get; set; } [DataMember] public string Name { get; set; }
    }
    

     

    [ServiceContract]
    public interface IBookService
    { [OperationContract] [WebGet(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] Book[] GetBooksList(); [OperationContract] [WebGet(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] Book GetBookById(string id); [OperationContract] [WebInvoke(Method = "PUT", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] void AddBook(string name); [OperationContract] [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] void UpdateBook(string id, string name); [OperationContract] [WebInvoke(Method = "DELETE", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] void DeleteBook(string id);
    }
    
  2. Configure your project to generate documentation

    In properties of project > build tab > XML documentation file (do not specify path of documentation, it will provide default path that Swagger4WCF look for).

  3. install nuget package on project where WCF interfaces are declared

    https://www.nuget.org/packages/Swagger4WCF

  4. build your project will trigger Swagger4WCF

You can see yaml in output directory!

Test the generated yaml in swagger editor online

 Copy content of the generated yaml and Rendez vous to swagger online editor :

http://editor.swagger.io/#/

Clean swagger editor to get started

Then paste the cotent of your generated yaml

How it work in background

 Swagger4WCF use NuPack postbuild pattern to trigger at build time.

https://www.codeproject.com/Tips/1190360/How-to-setup-a-managed-postbuild-without-scripting

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 detect WebGet/WebInvoke to provide Verb/Method dans serialisation style in yaml.

Conclusion

Swagger4WCF may help your to save time when developing web application, it is easy to test, use or remove. It can be a nice starter for new one in swagger technology because it required only to install a nuget package. Unfortunately, it is not enough mature for advanced WCF configuration but I’m sure it can be improve.

LEAVE A REPLY