RESTful web service auto-generate WADL
Asked Answered
A

2

28

I have created a RESTful web service in C# and have deployed it to IIS. When I access the service HeadOffice.svc, I have the option to view the WSDL (HeadOffice.svc?wsdl). What I would like to do is have the option of viewing the WADL (e.g. HeadOffice.svc?wadl). Is this possible?

I have read around the place that the general opinion is that this is not the best practice. However, I need the WADL for a school assignment, so any help would be much appreciated.

Absquatulate answered 19/10, 2012 at 5:39 Comment(0)
S
25

Suppose you already know that WADL is not standard / not supported widely. And when somebody needs WADL, may be then better to use WS*/SOAP service + WSDL. So your task looks like very strange.

Anyway WADL is not supported "out of the box" in any REST implementation from Microsoft, neither WCF 3.5 Rest Starter Kit, neither WCF 4 REST, and ASP.NET WebAPI.

There are no reliable tools for WADL for .NET.

When your goal is to generate C# client code using WADL, believe me, you will spend more time as writing client code by yourself. And there are better solutions for that.

You can use new classes like HttpClient class or RestSharp or similar libraries to easily manually write your client and it will be even faster then googling for reliable WADL solution for .NET

Similar question on stackoverflow: Restful service in .NET with WADL instead of WSDL

UPDATE - Swagger: For some years swagger has established itself as such format. You can either start writing service definition using swagger's YAML in the Swagger editor or let generate swagger from existing services, for .NET using Swashbuckle library. The second is something we had with WSDL, and swagger editor let's you generate client and server boilerplates. Regardless you are generating your server or client or not fan of it, swagger is actually a very good contract exchange format for REST service, not ideal but good option.

Soho answered 19/10, 2012 at 7:5 Comment(4)
Thanks for the information! I intended to generate Java code form the WADL. However, it looks like I'll nee to teak a path other than that of the WADL.Absquatulate
That first link is completely useless, looks like you've linked to a project that doesn't even have any source code. What's the point?Voyles
Dmitri Nesteruk Point is that WADL like that link is dead for .NET and out dated. And it's better to write service access manually either then try to generate in in WSDL SOAP styleSoho
Just in case anyone wants to generate C# HttpCllient for OpenAPI (aka Swagger) API - you can use NSwagStudio or even use OpenAPI (Swagger) Connected Service  -  a Visual Studio 2017 extension to generate that with NSwag.Lincolnlincolnshire
D
0

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

  1. 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"/>
  1. 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>
  1. 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);

  2. 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()
{
}
  1. 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; }
    }
    

Reference:- https://github.com/abelsilva/swaggerwcf

That's it wcf for Swagger implemented. Please free if you face any issue.

Thanks, Abhi

Dividivi answered 26/7, 2018 at 12:43 Comment(2)
Please add source links when you copy and paste code from another siteJolynnjon
wow, I did not know someone had done a Swagger for WCF for this use case.Mourant

© 2022 - 2024 — McMap. All rights reserved.