Best practices on Rest API client in Dotnet ecosystem

Howdy Readers,

Today I’m going to write something about HttpClient inspired by ASP.NET community standup. The popularities of microservices and REST API’s are on it’s highest peak. Which makes us using HttpClient the most. While you are calling rest API using HttpClient, you need to be aware of few most important things.

  1. Do not create a new instance of HttpClient class for each request. Creating new object will create two problems.
    • Each time the new connection will create a new connection pool for each request and it is a very costly operation as it includes 3-way handshake(HTTP) or even costlier TLS handshake(HTTPS)
    • The second issue with this is it may lead to socket exhaustion because in whichever operating system environment you are in, you have limited sockets. You may think that you will dispose the object of HTTP client and you might not have parallel users equivalent to no of sockets(64K) but there is a catch here. Once you dispose of the object, socket does not get freed up immediately. The different operating system takes the different amount of time to free up the sockets(say 3-5 seconds). Hence this may lead you to reach handle limit.
  2.  Do not create a single instance(static or singleton) of HttpClient. If you read the point one, you may conclude on creating the single instance of HttpClient because of it is thread-safe. This is said to be a more efficient way of creating HttpClient but it also has a serious issue. It will keep the connection open for a very long time. This has issue like
    • It may lead to not respecting load balancing. What I mean by this is if your connection is opened with server X then all requests from your application will go to the same server and other servers might be in idle state. This may be a big problem when you are one of the highest load generator client of that API.
    • The bigger problem comes when API uses things like Online-Offline (Blue-Green) deployment. On the toggle of API, Open connection with server keeps sending a request to it although it might have become an offline server.
    • You might have some default header which you might not want to share between different clients.

This Leads to a conclusion that keeping either single or transient connection can cause many issues. It is always recommended to create connections smartly, some of the examples might be to create client per request kind and also keep refreshing the connection over a period of time. There can be many such solutions but use it smartly according to your need.

Image detection bot using Microsoft Vision API : Step by step guide

Neel Bhatt

bot9Note – You can find the source code of my sample application here.

In my previous post about Bots, I have explained how to use FormFlow to create attractive selection box in bots which you can find here.

In this post, I will explain how to create Image detection bot using Microsoft Vision API.

Let us see how to create Image detection Bot application using Visual Studio 2017.

prerequisite:

Also if you want to have Bot Application as a template then as a workaround just download this  (download would start once you click on the link) project and put the extracted folder into below location:

C:UsersYourNameDocumentsVisual Studio 2015TemplatesProjectTemplatesVisual C#

Once this is done, you can see Bot Application template as shown below:

bot1Click on Bot Application and then it will create a sample project…

View original post 1,130 more words

Terraform, VPC, and why you want a tfstate file per env

charity.wtf

Hey kids!  If you’ve been following along at home, you may have seen my earlier posts on getting started with terraform and figuring out what AWS network topology to use.  You can think of this one as like if those two posts got drunk and hooked up and had a bastard hell child.

Some context: our terraform config had been pretty stable for a few weeks.  After I got it set up, I hardly ever needed to touch it.  This was an explicit goal of mine.  (I have strong feelings about delegation of authority and not using your orchestration layer for configuration, but that’s for another day.)

And then one day I decided to test drive Aurora in staging, and everything exploded.

Trigger warning: rants and scary stories about computers ahead.  The first half of this is basically a post mortem, plus some tips I learned about debugging terraform outages.  The second half is about why you should…

View original post 2,162 more words

Optimizing Identity Tokens for size

leastprivilege.com

Generally speaking, you want to keep your (identity) tokens small. They often need to be transferred via length constrained transport mechanisms – especially the browser URL which might have limitations (e.g. 2 KB in IE). You also need to somehow store the identity token for the length of a session if you want to use the post logout redirect feature at logout time.

Therefore the OpenID Connect specification suggests the following (in section 5.4):

The Claims requested by the profile, email, address, and phone scope values are returned from the UserInfo Endpoint, as described in Section 5.3.2, when a response_type value is used that results in an Access Token being issued. However, when no Access Token is issued (which is the case for the response_type value id_token), the resulting Claims are returned in the ID Token.

IOW – if only an identity token is requested, put all claims into the token. If however…

View original post 210 more words

Introducing Jasper — Asynchronous Messaging for .Net

The Shade Tree Developer

IMG_1017

For my take on when you would use a tool like Jasper, see How Should Microservice’s Communicate?

“Jasper” is the codename for a new messaging and command execution framework that my shop has been building out to both integrate with and eventually replace our existing messaging infrastructure as we migrate applications to Netstandard 2.0, ASP.Net Core, and yes, adopt a microservices architecture. While we’ve been working very hard on it over the past 6 months, I’ve been hesitant to talk about it too much online. That ends today with the release of the first usable alpha (0.5.0) to Nuget today.

We’ve already done a great deal of work and it’s fairly feature rich, but I’m really just hoping to start drumming up some community interest and getting whatever feedback I can. Production-worthy versions of Jasper should hopefully be ready this spring.

Okay, so what problems does it solve over just…

View original post 1,242 more words

How Should Microservice’s Communicate?

The Shade Tree Developer

We do quite a bit of distributed development and inter-service messaging at work. Some of this is done through exposing HTTP services. For asynchronous messaging between systems, my shop uses FubuMVC and its .Net Core replacement “Jasper” as a service bus (translate “Jasper” to “MassTransit” or “NServiceBus” when you read this). This blog post is a draft of our architectural team’s advice to our teams on choosing which option to use for their projects as part of our nascent microservice architecture approach. If any of my colleagues see this and disagree with me, don’t worry because one way or another this is going to be a living document and you’ll get to have input to this.

Microservices will generally need to send or process messages from other microservices or clients. To that end, it’s worth considering your options for inter-service communication.

We commonly use either HTTP services or the Jasper/FubuMVC…

View original post 722 more words

Authentication in .Net Core 2.0 : .Net Core 2.0 Identity step by step

Neel Bhatt

idn20Note – You can find the source code of my sample application here.

Whenever you start creating an application, one of the most important modules is the authentication.

In this article, I will explain how to add Login functionality to your .Net Core 2.0 application using .Net Core Identity,

Let us first see what is ASP .Net Core Identity?

ASP.NET Core Identity is a membership system which allows you to add login functionality to your application. Users can create an account and login with a user name and password or they can use an external login provider such as Facebook, Google, Microsoft Account, Twitter or others.

Let us create .Net Core 2.0 application with Core Identity step by step

First open Visual Studio 2017 -> Create New Project -> Select Core Web application

idn1

In next window, click on Change authentication link and select Individual User Accounts. By this, we…

View original post 821 more words

Introduction to Asp.net Core 1.0

The Diligent Geek

I had a privilege to be invited by Agile Testing Alliance , Vadodara chapter to talk about Asp.net Core 1.0 . It was very interactive sessions and the participants asked some really good questions.

Here is the synopsis of my talk

What is Asp.net Core ?

It is a lean stack from Microsoft to develop the modern web apps built from ground up and is most revolutionary version since asp.net first was released in 2002
Today it is available RC1 update 1 which comes with golive license so this means that this framework is production ready.

Cross platform

This is a game changer and USP about this framework.  It works seamlessly on Windows, OSX and Linux. The same code base runs directly on the metal, and remember this is different from the Mono Framework. In fact the CoreClr is around 11 meg and is shipped along with your application as…

View original post 688 more words

ASP.NET Core MVC API documentation using Swashbuckle Swagger

Software Engineering

This article shows how to document your ASP.NET Core 1.0 MVC API using Swagger with Swashbuckle. Per default, it does not use your xml comments in the code and this needs to be configured if required.

Code: https://github.com/damienbod/AspNet5GeoElasticsearch

2017.02.12 Updated to VS2017, msbuild and Swashbuckle.AspNetCore
2016.07.03 Updated to ASP.NET Core RTM
2016.06.04 Updated to ASP.NET Core RC2 dotnet

Step 1: Add the required NuGet packages to the dependencies in the project csproj file.

Step 2: Produce the .xml file which contains the xml comments when building. Click the produce outputs on build checkbox in your project file.

SwaggerProjectConfig

Or set the ProduceOutputsOnBuild property in the project csproj file.

Step 3: Configure Swashbuckle.SwaggerGen in the Startup class ConfigureServices method.

You need to define your path to the comments xml file, which can be found in the artifacts folder. This should be saved in a config file.

The ConfigureSwaggerDocument with OperationFilter method is…

View original post 51 more words

Connecting to the Azure IoT Hub using an AMQP stack

DEVEXPERIENCE

Accessing to the Azure IoT Hub service is so simple using all the available SDKs that Microsoft provides as open source on GitHub. We can laverage on more different languages for different platforms : from C# for .Net and UWP (Universal Windows Platform) applications to Java, from NodeJS (using JavaScript) to C. In this way, we are able to coverage different hardware from higher level devices like BeagleBoard, Raspberry Pi 2, Minnowboard Max to very low constrained devices like Kinetis K64F (Freescale) and CC3200 (Texas Instruments).

Of course, using directly HTTP and AMQP protocols, we can access to the IoT Hub from other platforms (not certified yet) and it could be the scenario of the .Net Micro Framework because up to now there isn’t an official SDK for it. The current C# client implementation uses Task and async/await programming model very heavily that isn’t supported (yet) by the “little” child in the .Net frameworks family. One choice to connect…

View original post 1,373 more words

RabbitMQ on Raspberry Pi

Tobias Abarbanell

Playing around with RabbitMQ lately, I was interested how it would perform on a very small box, a
Raspberry Pi.

Here is a short description how to get RabbitMQ running on a Raspberry Pi.

  1. get a Raspberry Pi installed with Raspbian – just follow the standard installation guide from http://www.raspberrypi.org/downloads/
  2. get the rabbit MQ package from here: http://www.rabbitmq.com/install-debian.html
  3. I wanted to run a client in Node.js, so I got the latest node with these commands:

    wget http://node-arm.herokuapp.com/node_latest_armhf.deb
    sudo dpkg -i node_latest_armhf.deb
    node -v
  4. pick the node version of the RabbitMQ tutorials from here: https://github.com/squaremo/amqp.node/tree/master/examples/tutorials

Here is how this looks in pictures:

Raspberry Pi running RabbitMQ Raspberry Pi running RabbitMQ node.js client running with rabbitMQ on a Raspberry Pi node.js client running with rabbitMQ on a Raspberry Pi

And the results: If you let a publisher with a simple “hello world” message run in a loop for 1000 times and start a subscriber for this queue at the same time, you will see…

View original post 103 more words