Using access tokens in Swagger with Swashbuckle

Today I was trying to use Swagger for testing the Web API calls, but then faced issue with how to pass access tokens for authorization purpose.

Swagger has nice UI to pass the API parameters. But there is no direct way to pass custom header parameters.

I found the solution (See below). It is written so nicely, I am sharing it as it is 🙂

The Developer Play Book

Securing access to your API using access tokens is common practice. In this post, we’ll learn how to call secure API endpoints using the swagger specification specifically using Swashbuckle (An implementation of Swagger for .NET)

swag1

Understanding Swagger Schema:
This outline shows the basic structure of a swagger specification document. This file is represented in Json which is in turn used by Swagger-UI to display the interactive API documentation.
{
"swagger": "2.0",
 "info": {
"version": "v1",
"title": ".NET Latest API",
"description": ".NET Latest API",
"termsOfService": "Some terms",
"contact": {
"name": "donetlatest Team",
"email": "team@dotnetlatest.com"
}
},"host": "local.api.donetlatest.com:80",
"schemes": [

"http"

],"paths": {

"/V1/api/Authentication": {},
"/V1/api/Countries": {},
"/V1/api/Clients": {
},"definitions": {

"CountryDTO": {},
"StateDTO": {},
"ClientDTO": {}
}
}
Parameters
The Paths item object describes the operations on a single path. Each path has a parameters object which are a list of inputs for a given endpoint.
"/V1/api/LitmusClients": { "post": { "tags": [ "LitmusClients" ], "summary": "GET /api/clientsrn Gets an array of all clients", "operationId": "Clients_Index", "consumes": [ ], "produces": […

View original post 299 more words

Prefix – .NET Profiler

Came across Prefix by Stackify recently. Its a very good .NET profiler, works not only with MVC/Web API but also supports Webforms/ASP.NET Core etc.

No configuration required if you are using VS 2013.  Just download the setup and start using it..

For VS 2015- you just need to install a nuget package or enable the Stackify HTTP Module as mentioned here.

Worth to try it.

ApexSQL Refactor – SQL Server free Add-in.

I usually hate to even to look at the SQL code which is not well formatted and structured. It is completely developers responsibility to make sure his/her code is at-least readable and understandable to others. Even today I see lot of developers paying very little attention to SQL code formatting.

There are number of options available (including websites like poorsql.com) for formatting, but ApexSQL Refactor add-in for SQL server is more than just formatting !!

Came across this article by Pinal Dave and I found this tool useful !!

Using Build/Service configurations and web.config transformations to make deployments easier

Some background –

Recently in one of the windows azure project – I noticed that every time we want to deploy application, we have to do lot of changes in Service Configurations/Definition files. For ex. changing the connection strings to make sure they point to right database, changing the instance count and VM Size, changes in SSL setttings etc.

Changing these settings before deployment was repetitive task and had to be done very carefully each time since any mistake could be very harmful.

In addition to these azure related configuration files, there was web.config changes as well.  It had to be modified to staging vs production configurations e.g. for appkeys, session settings etc.

I knew there is a way so that once configured ,we dont have to change these values during deployment.  And then I came across Build configurations and web.config transformations.

Below is very good article on how service and build configuations can be setup to help you with deployment process. Now we just have to specify correct environment during deloyment and it would take the config files according to enviornment.

http://slalomdotcom.wordpress.com/2011/08/19/building-and-deploying-windows-azure-projects-using-msbuild-and-tfs-2010/

Below link talks about Web.Config transformations. You will find many other links but I found below link useful and it was enough to get my work done.

http://vishaljoshi.blogspot.in/2009/03/web-deployment-webconfig-transformation_23.html

Whatever transformations you try – below link is very good way to test those transformations.

https://webconfigtransformationtester.apphb.com/

I really wanted to write it in my own language and include the problems I faced, but will do that some other day 🙂

Find largest/heavy objects in a SQL Server database..

I needed a SQL query to identify heavy objects (in terms of Size) in my database and I came across below query here. I found it very useful.

SELECT 
    t.NAME AS TableName,
    i.name as indexName,
    sum(p.rows)as RowCounts,
    sum(a.total_pages)as TotalPages, 
    sum(a.used_pages)as UsedPages, 
    sum(a.data_pages)as DataPages,(sum(a.total_pages)*8)/1024as TotalSpaceMB,(sum(a.used_pages)*8)/1024as UsedSpaceMB,(sum(a.data_pages)*8)/1024as DataSpaceMB
FROM 
    sys.tables t
INNER JOIN      
    sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN 
    sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN 
    sys.allocation_units a ON p.partition_id = a.container_id
WHERE 
    t.NAME NOTLIKE'dt%'AND
    i.OBJECT_ID >255 AND   
    i.index_id <=1
GROUP BY t.NAME, i.object_id, i.index_id, i.name 
ORDER BY object_name(i.object_id)

Stored Procedures are pre-compiled…and what’s the fact ?

If the question is – what is the advantage of using Stored Procedure ?

One of the common answer for sure is – they are always pre-compiled.  But is it so ?

No, Stored Procedures are not compiled until they are run first time.   When you CREATE/ALTER the procedures – its just checking the syntax but there is no compilation process.  So they are just plain T-SQL code blocks until they are executed.  They are compiled only during first time execution and then after they are always pre-compiled for subsequent executions.

And when stored procedures are recompiled ? Below are few points when they are re-compiled.

  • dropping and recreating the stored procedure
  • using the WITH RECOMPILE clause in the CREATE PROCEDURE or the EXECUTE statement
  • changing the schema of any referenced objects
  • running the sp_recompile system stored procedure against a table referenced by the stored procedure
  • restoring the database containing the stored procedure or any object referenced by the stored procedure
  • the stored procedures plan dropping from the cache
  • Stored procedure will recompile if there is a sufficient number of rows in a table referenced by the stored procedure has changed. SQL Server will recompile the stored procedure to be sure that the execution plan has the up-to-date statistics for the table.

Bulk insert the data between tables with Parent/Child relationship

This is a very common scenario where you have two or more tables with Parent/Child relationships and you need to bulk insert the data between these tables. For ex. you may want to insert 100 rows in parent table and insert 1000 rows in the child table using parent Ids genereated during parent table insertions. 

There are numerous ways to do this with powerful T-SQL but I wanted to document the one I have been using.

Lets consider you have below tables in your database :

Image

 

 

And here is your input XML from your application which contains the data to be inserted between tables :

Image

 

Here is the query:

Image

 

Output will look like :

Image