Wednesday, February 28, 2018

How to use Swagger in Web API

In this blog, we will discuss about web api documentation and will demonstrate basic steps how to setup swagger in web api project and also show how to test web api by submitting http request GET/POST/PUT via swagger UI.

Please read my previous blog when we discussed how to create a web api in ASP.NET MVC Application and describe basic steps to create an asp.net web api.

Swagger is API developer tools for the Open API Specification (OAS), enabling development across the entire API life cycle, from design and documentation, to test and deployment.

Definition -
Swagger is a simple yet powerful representation of your RESTful API. With the largest ecosystem of API tooling on the planet, thousands of developers are supporting Swagger in almost every modern programming language and deployment environment. With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability.”


To add Swagger into WEBAPI Project, you need to perform only 2 steps

1.   Install SwashBuclkle open source project via Nuget.


Install SwashBuclkle Nuget Package

2.   Configure Swagger Config File :

 After package installed, you will find a new file ‘SwaggerConfig.cs’ under App_Start Folder

    
Swagger Config

public class SwaggerConfig
    {
        public static void Register()
        {
            var thisAssembly = typeof(SwaggerConfig).Assembly;

            GlobalConfiguration.Configuration
                .EnableSwagger(c =>
                    {             
                        c.SingleApiVersion("v1""DemoWebAPI");                 
                    })
                .EnableSwaggerUi(c =>
                    {
                        c.DocumentTitle("Demo Web API");

                    });
        }

    }
 
It is done J 

now build the web api project and run it.

Browser http://localhost:65096/swagger

Swagger UI documentation















Swagger allows you to submit HTTP request GET, HTTP POST, HTTP PUT against Web API

here is an an example 

Submit HTTP GET Request For /api/Projects/{projectId}



Swagger UI GET


Other swagger related posts:



·         Disable Swagger UI



Thanks for visiting!!

Unity Container Lifetime Scope of Instance of Registered Type

In this blog, we will discuss about lifetime scope of instance of register type created by Unity Container and how to manage it

Container.RegisterInstance(typeof(ProjectEngine), new ProjectEngine ())

By default, the RegisterInstance method registers existing object instances with a container-controlled lifetime [ContainerControlledLifetimeManager] it means that unity registers an object as a singleton instance.

By default lifetime for RegisterType method is TransientLifetimeManager, which creates new object of request type for each call.

Container.RegisterType<IProjectEngine, ProjectEngine>(); 

OR

Container.RegisterType<IProjectEngine, ProjectEngine>(new TransientLifetimeManager()); 
Unity Container have other lifetime managers so you can able to manage lifetime of created object differently.

Lifetime Manager
Description
TransientLifetimeManager
Unity Creates a new instance of request type for each call, when you call Resolve method
ContainerControlledLifetimeManager 
Unity creates a singleton object of request type on first call and it returns same object for subsequent Resolve call
HierarchicalLifetimeManager
Its same as ContainerControllerLifeTimeManager, only difference is that child and parent container creates its own singleton object and but both don’t share its singleton object
PerResolveLifetimeManager
It is same as TransientLifeTimeManager but it reuse same instanced object in the recursive object graph
PerThreadLifetimeManager
Unity Creates a singleton object per thread basis
ExternallyControlledLifetimeManager
It delegate to external source to manage the object and but for first resolve call, it creates object of requested type.

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...