Skip to main content

Posts

Asynchronous WebApi2 HTTP client example

Today I want to cover a very common scenario about how to create a HTTP client for a WebAPI 2 service. To present my implementation I will be using one of a Task<T> extension method that I described recently on my blog. Let`s start from defining an API service. In this example it udes a REST based WebApi service with the following implementation.   [ AllowAnonymous ]     public   class   CityController    {         ///   <summary>         ///  The city repository.         ///   </summary>         private   readonly   ICityRepository  cityRepository;         ///   <summary>         ///  Initializes a new instance of the  <see cref= "CityController"  />  class.         ///   </summary>         ///   <param name= "cityRepository" > The city repository. </param>         public  CityController( ICityRepository  cityRepository)        {             this .cityRepository = cityRepository;        }        

A Task chaining refactoring with an example

This was a very long break since I posted my last web note however it`s time to share some fresh experience with the developer community. Today I want to show one of a handy solution which allowed me to simplify a Task chaining in .NET 4.5.1. Let`s start from defining a problem which in this case is a Task.ContinueWith<TResult>  method and the way how it`s designed for a tasks chaining. So if you want to use a chaining by using this function, all the time you need to access a parent task result property which in many cases causes a lot of redundancy in the code. To demonstrate this I use a simple async WebApi2 GET cities  auto-complete function which will query a database and later map result set of entities to collection of data transfer objects (DTO). // !!BADLY DESIGNED CODE!! [ HttpGet ] public   async   Task < HttpResponseMessage > Get( string  query)     {           return   await   this .cityRepository .AutocompleteCityAsync(query) . C

Managing Android emulator in .NET

Automated coded UI testing become very popular in the past few years. Its allow developers to create tests which are executed directly on UI level and simulate user actions. At the same time HTML5 become a standard for creating universal modern applications which can be hosts in a native browser controls. New model of creating applications brings a new challenges in the testing fields that's why in this post I want to present my solution (it takes me almost two days to get this working!) which is first step in the process of creation an end-to-end test automation for mobile applications. My solution is prototype of a .NET console application which can be use to control Android emulator and simulate user standard operation like installing app, typing and rotating. This prototype can be use as ' emulator manager ' which controls device emulator on which tests are performed - for example by using   Selenium . As initial requirements to run the project: 1) Install the

Playing with a .NET types definition

In the last few days I spent some time trying to unify structure of one of the project I`m currently working on. Most of the changes were about changing variable types because it`s were not used right way. That is why in this post I want to share my observations and practices with you. First of all we need to understand what ' variable definition ' is and how it`s different from ' variable initialization '. This part should be pretty straightforward:   variable definition  consist of data type and variable name only <data_type> <variable_name> ; for example int i ; . It`s important to understand how variable definition affects your code because it behaves differently depends weather you work with value or reference types. In the case of value types after defining variable it always has default value and it`s never null value. However after defined reference type variable without initializing it has null value by default. variable initialization  is

Creating API with MVC ApiController part 3 - moving to asynchronous code

In my two previous posts ( part 1 and part 2 ) I described both simple and more advance approach of creating Rest-full API in MVC 4. Today I want to take another step forward and go a little deeper  in the creation of API. In this post I`m going to describe how we can create asynchronous API functions which bring better performance to our applications. The first thing that need to be understand is an asynchronous operation in .NET Framework. In .NET Framework 4.0 one of the most important changed was introducing Task  class.The Tasks in System.Threading.Tasks namespace are a method of fine grained parallelism, similar to creating and using threads, but they have a few key differences and the main difference is that Tasks in .NET 4.0 don’t actually correlate to a new thread, they are executed on the new thread pool that is being shipped in .NET 4.0. More about task you can read here  but to understand this article all you need to understand about the Task<T> class is that th