Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / C#
Tip/Trick

How to Generate Basic Swagger yaml Description for WCF Automatically on Build Time with Swagger4WCF .NET 4.0+

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
4 Jun 2017CPOL1 min read 41.8K   4   8
How to generate basic swagger yaml description for WCF automatically on build time with Swagger4WCF .NET 4.0+

Introduction

It can be very boring to manually write yaml description for swagger and maintain it especially when your WCF services are very simple. 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).

How to Use Swagger4WCF

There are only a few step required to test it:

  1. Create a library with WCF interface:
    C#
    /// <summary>
    /// Represent a book
    /// </summary>
    [DataContract]
    public class Book
    {
        /// <summary>
        /// Book id
        /// </summary>
        [DataMember]
        public int Id { get; set; }
    
        /// <summary>
        /// Book name
        /// </summary>
        [DataMember]
        public string Name { get; set; }
    }
    
    C#
    /// <summary>
    /// Service crud for book
    /// </summary>
    [ServiceContract]
    public interface IBookService
    {
        /// <summary>
        /// Get list of book
        /// </summary>
        /// <returns>list of book</returns>
        [OperationContract]
        [WebGet(RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
        Book[] GetBooksList();
    
        /// <summary>
        /// Get book by id
        /// </summary>
        /// <param name="id">book id</param>
        /// <returns>book</returns>
        [OperationContract]
        [WebGet(RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
        Book GetBookById(string id);
    
        /// <summary>
        /// Add book
        /// </summary>
        /// <param name="name">name of book</param>
        /// <returns>id of book</returns>
        [OperationContract]
        [WebInvoke(Method = "PUT",
        RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        void AddBook(string name);
    
        /// <summary>
        /// Update book
        /// </summary>
        /// <param name="id">if of book</param>
        /// <param name="name">name of book</param>
        /// <returns>id of book</returns>
        [OperationContract]
        [WebInvoke(Method = "POST",
        RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        void UpdateBook(string id, string name);
    
        /// <summary>
        /// Delete book
        /// </summary>
        /// <param name="id">id of book</param>
        /// <returns>id of book</returns>
        [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 looks for).

    Image 1

  3. Install nuget package on project where WCF interfaces are declared:

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

    Image 2

  4. Build your project will trigger Swagger4WCF:

    Image 3

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:

Image 4

Then paste the content of your generated yaml:

Image 5

Image 6

How It Works in the Background

Swagger4WCF uses 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 detects WebGet/WebInvoke to provide Verb/Method in serialization style in yaml.

Conclusion

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
France France
After working in electronics, I fall in love with software development which is a real passion for me, particulary architecture concerns for productivity.

Comments and Discussions

 
QuestionProblem with generating yaml for Task<T> type methods and repeated description Pin
Ekaterina Rusanova12-May-20 4:19
Ekaterina Rusanova12-May-20 4:19 
QuestionSwagger4WCF No Output Pin
ILOVETOFU26-Sep-18 21:30
ILOVETOFU26-Sep-18 21:30 
AnswerRe: Swagger4WCF No Output Pin
Member 140081604-Oct-18 13:35
Member 140081604-Oct-18 13:35 
GeneralRe: Swagger4WCF No Output Pin
Member 1404114213-Dec-18 18:48
Member 1404114213-Dec-18 18:48 
QuestionBuilding error occurs after adding the package "Swagger4WCF" Pin
rijalpp5-Oct-17 17:23
rijalpp5-Oct-17 17:23 
AnswerRe: Building error occurs after adding the package "Swagger4WCF" Pin
AlpayKAPTAS26-Feb-18 1:25
AlpayKAPTAS26-Feb-18 1:25 
AnswerRe: Building error occurs after adding the package "Swagger4WCF" Pin
ian2664-Apr-18 15:36
ian2664-Apr-18 15:36 
AnswerRe: Building error occurs after adding the package "Swagger4WCF" Pin
ian2664-Apr-18 18:11
ian2664-Apr-18 18:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.