Adding Swagger to my MyReverie ASP.Net Core API

Continuing on the MyReverie solution building upon my last post on this My Reverie.

What I would now like to do is add some API documentation around the my API

The reason I am choosing swagger is simply that it is the largest framework of API developer tools for the OpenAPI specific which allows rapid documentation and testing my APIs with great ease.

For reference check out their website @ https://swagger.io/

Let’s get started

First we want to add the Swashbuckle package from Nuget

So right click the Goals.API project

Click Manage Nuget Packages

Nuget

Under Browse tab search for Swashbuckle.AspNetCore

Package

Once found click install

Once installed we want to add the Swashbuckle namespace ‘Swashbuckle.AspNetCore.Swagger’ to our Startup class

In the ConfigureServices method add:

services.AddSwaggerGen(c =>
{
c.SwaggerDoc(“v1”, new Info { Title = “My Reverie”, Version = “v1” });
});

ConfigureServices

Next, In the Configure method

In the cope of the env.IsDevelopment() check, add

app.UseSwagger();

app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint(“/swagger/v1/swagger.json”, “My Reverie V1”);
});

Configure

Lets run it to see what it looks like, you should see something like below

image

To try it out simple select the Get button

Click the Try it out button on top right

Then hit execute

image

Summary:
We’ve just added swagger to the API to add a basic documentation and testing base in which to build upon.

My Reverie

MyReverie is a new simple side project I am working on. The idea is to build a goal tracking solution set target dates against those goals as well as set milestone markers to track progress. They could be long or short term goals – entirely up to the user.

I will routinely push the code at certain points to GitHub under MyReverie Repo.

I have a rough idea of some of the features to add which I will list under the Projects Tab of GitHub.

This is also a way to experiment and learn new technologies as I go, some technologies I will be looking to incorporate are Asp.Net Core, Swagger, Docker,  xUnit.Net and EF Core so far.

I would like to have this solution created in a Micro Services style architecture – so functionality abstracted to services.

I will break this solution out to different blog posts as the solution is going to continue to grow.

The solution is not intended to be a complete sample or starter app more of a reference or exploration on learning.

In this post I will begin with the Goals API

Okay, let’s get started.

I want to create this entirely in Code so lets go to Visual Studio and create a new ASP.Net Core project.

I am using Visual Studio 2017 Community version 15.7.1

After clicking File->New Project, I want to select ASP.NET Core Web Application

ASPNetCoreWebApp

Lets call the project name ‘Goal.API’ and Solution Name ‘MyReverie’ as below, I want to also also select ‘Create new Git repository’ to commit using Git

NameprojSol

Lets choose the API template and select ASP.Net Core 2.1 under the dropdown as well as uncheck ‘Enable Docker support’ for now and clock OK

Template

After restore of packages has completed, lets run the API to ensure it build and runs successfully,After launching add /api/values to the end of the URL and you should see something similar as below.

RunApp

Great, we have created our Solution as well as our Goal API.

Let’s go ahead and create 2 new folders in our solutions, src and Services, Services being a child of src and lets move the API project under Services as below.

SolStructure

That’s it for this post, next post will continue to build upon this solution

Getting Started with Azure Service Fabric

I wanted to show a simple example of using Azure Service Fabric to create a reliable & scalable micro service that would perform addition functionality on an input string for example “1+1” using a console application as the client.

The string is parsed out to two integers and calculates the result. Obviously this is extremely simplistic type of example. I wanted to really just do a quick test on passing input and expecting output.

The code is listed up on my GitHub account under the ServiceFabric Repo.

ASP.NET Core Web API with Docker

This post simply demonstrates creating a simple service called ‘AdditionMicroservice’ that adds two numbers provided as query string parameters.

I will add this code to my GitHub account under Getting started with Microservices repo

Pre-requisites:
Docker for Windows

1) Create a new ASP.NET Core Web Application project

image

Select the Web API Template

image

Check the Enable Docker Support checkbox

Select Windows as the OS

Click

Open Values Controller file under Controllers

image 

Remove all other methods except first Get method and change to below as below

image

The change  launchUrl’s from api/value to api/addition in launchsettngs.json file:

image

Run the app and append to the URL both the x and y parameters along with their set values to view the result:

image

Resources that helped me pass the 70-483 exam

Well a few weeks back I passed the 70-483 Programming in C# exam, it had been several years since I took an exam so I did not know how I would do.

I had worked in c# for quite some time now but all of my knowledge was pretty much self taught on the job so I knew there were some gaps in my knowledge so I thought going for the exam would at least highlight the areas in which I need to focus on. Indeed there were some areas I had touched on very little throughout my career so it was definitely a worthwhile cause from that perspective alone.

In my previous employment I had the opportunity to attend the Microsoft official course for this exam which lasted a week which was really beneficial and was a great resource, however it was expensive and not sure I personally would have attended I have had to foot the bill. Other resources that I took advantage of where pluralsight courses, virtual academy jumpstart videos as well as the MCSD Certification Toolkit.

To mention the pluralsight course there is blog post covering this exam course : http://blog.pluralsight.com/learning-path-c-microsoft-exam-70-483 which indicates the videos to check out that would be specific to the exam areas, I cannot rate pluralsight enough to be honest I truly believe it to be one of my best subscriptions I cannot tell you the amount of information I have learnt over the years using this resource as well as solving many technical issues that I had faced – if you do not have a subscription I would encourage you to take advantage of their free trial.

The virtual academy jump start videos were awesome too : http://www.microsoftvirtualacademy.com/training-courses/developer-training-with-programming-in-c, the videos were very entertaining and the presenters knew their stuff best of all it is free so definitely check them out. It is a great resource for some free training.

Other than this plenty of hands on coding there is really no substitute for actual coding I believe it is not only the best way to learn as it will quickly let you know when you hit an area you do not truly fully grasp but also the best way to retain what you learn for longer.