Mobile DevOps using VSTS, Xamarin Test Cloud and HockeyApp for Xamarin

In this post I will show how effectively we can use VSTS to provide build automation, release management for Xamarin Apps and use Xamarin Test Cloud and HockeyApp for application testing.

VSTS stands for Visual Studio Team Services, is a cloud service to leverage certain features like defining new projects, team structure, build management, release automation and others that can be used in each phase of the software development lifecycle. Every software lifecycle undergo several phases that includes planning, designing, development, testing and then deployment on production and VSTS provides several components that can be used to accelerate and automate tasks with the complete team management and reporting.

VSTS is an online version of TFS that can be accessed from http://visualstudio.com/vso and it provides a free account supported up to 5 members. With VSTS we can define team projects, select version control (Git or TFS), define team members, manage work items, define build definitions, release management and so on.

So let’s take a simple example of hosting our basic Xamarin app on VSTS and define build definitions and policies to build application on code check in and then pass through some steps to test on cloud and publish on HockeyApp

Creating Team Project

Login with your account on {accountname}.visualstudio.com and create a new Team Project by hitting New Project button as shown below. You can name it any, in my case I named it as MobileDevOps and selected Git as the version control. Once this project is created you can clone this repository in Visual Studio or use Git commands to push your Xamarin solution on cloud.

Once your project is checked in on VSTS you can view the files from Code tab as shown below

image

So now we can play with defining a build and enable continuous integration.

Setting up a Build Definition

We can create a new build definition by going to the Build & Release tab and click on Builds. This opens up a page from where we can create a new definition. When you hit new definition, it will ask to select any template from the existing ones or select empty to create your own.

We will select the Xamarin.Android as in this post we will cover the configuration related to Android. Go through the wizard and it generates the basic template containing few steps

image

I have modified and few steps and they were deprecated and below snapshot shows the final set of steps executed when the new build is queued.

image

First step talk about restoring NuGet packages and restore all the packages defined in the package.json file in your solution. The configuration for NuGet restore would be as follows

image

Second step is about building the Xamarin project. Project reference the path of our Xamarin Android project, Output directory is where the .apk file will be created. Create App Package needs to be checked. From MSBuild we can select the version and architecture for which we need to create a package for. From JDK Options we can select the specific JDK version

image

Third step is to copy the .keystore file to the binaries directory. This is required if we want to distribute our app to the HockeyApp store. We can create .keystore file by running a keytool command from the path where Java is installed.

Here is the command

C:\Program Files (x86)\Java\jdk1.8.0_112\bin>keytool -genkey -v -keystore “D:\Projects\Xamarin\myappkey.keystore” -sigalg SHA1withDSA -keyalg DSA -keysize 1024

Below snapshot shows the steps when command is executed and ask for the Password.

image

We also need to note down the KeyAlias to specify in HockeyApp site and that can be obtained by running following command.

C:\Program Files (x86)\Java\jdk1.8.0_112\bin>keytool -keystore “D:\Projects\Xamarin\myappkey.keystore” -list -v

image

So, once the keystore is generated you have to check-in that file on your source code repository on the root folder where .sln file resides and then configure the Copy files to build directory as shown below.

image

On fourth step, we have to sign our package with the keystore we have generated and specify the same Key Password provided during the generation of keystore and for Alias use the command above to obtain the alias name.

image

Next, for Build solution step which is the fifth step in our definition. We will specify the Test project path and MSBuild arguments to place the test binaries under test-assembly folder. To learn how to create unit testing project for Xamarin apps, check following link https://developer.xamarin.com/guides/ios/deployment,_testing,_and_metrics/touch.unit/

image

Sixth step is running the tests on cloud and here is the configuration for Xamarin Test Cloud

image

Team API key can be obtained from https://testcloud.xamarin.com/. Login to this website and go to Account Settings. Click on Teams & Apps and click on Show API Key

image

Then, you have to define a new test run for that particular Team and select Android

image

Select devices for which you want the tests to run

image

Complete the wizard and note the device ID as shown on the last page wizard, and then specify the device Id, API key and your email to which your account is registered in Xamarin Test Cloud on configuration page in VSTS.

image

Seventh step is about publishing results in an xml file executed by Xamarin Test Cloud.

image

And then finally on eighth step we will copy the tested package in our drop artifact

image

Please note: Xamarin Test Cloud needs a permission to access and execute test cases. So make sure that your Xamarin AndroidManifest.xml file must have following entry

<uses-permission android:name=”android.permission.INTERNET” />

To verify, lets run a build by hitting Queue new build and see if the build is succeeded.

Create Release Definition to distribute our App to HockeyApp users

To create a release definition, we will go to Build & releases > Releases and create a new definition.  Provide any name and then create a new Environment and add task HockeyApp as shown below

image

If HockeyApp task is not showing in your task list, then add the extension for VSTS from the VS Marketplace https://marketplace.visualstudio.com/items?itemName=ms.hockeyapp

And here is the HockeyApp configuraiton

image

So this create our release definition but it will not work until we associate our VSTS with HockeyApp. So we have to go to the website http://rink.hockeyapp.net  and then create an API token with full access. This API token can be created by going to the Account Settings> API Token as shown below.

image

Note the API Token and then create a new Service Endpoint from VSTS by going to the Services tab and choose HockeyApp as follows

image

Specify the connection name and the API token retrieved from HockeyApp website

image

once this is done we can install the hockey app on our android device from https://www.hockeyapp.net/apps/ as this is not available on store. We have to enable the “unknown sources” in our android device settings so HockeyApp app can be installed.

Once installed, you can sign in with your hockey account and download the app which will be pushed when the release definition will run.

Please note that the Continuous Integration (CI) can be enabled for particular build definition from Triggers tab and this runs everytime when the user check in any code. Moreover we can also enable the Continuous Deployment (CD) from release definition triggers tab which deploy the app on HockeyApp once the build is succeeded.

 

Hope this helps!

 

 

Targeting PCL to .NET Standard

Enterprise application architecture comprises of multiple layers and usually, each layer is representing a separate project. Each project could be a class library project, UWP project, web forms project or ASP.NET MVC project and so on. We usually have a core class library project which is shared across multiple layers and contains some backend and core functionality that every layer can use.

Each platform has a different app model and referencing .NET assembly is not possible until it is a PCL (Portable Class Library). Portable class libraries were used to address these type of scenarios where you can select the app models or platforms while creating a class library and then it can be added to the project to which that library supports.

With the release of .NET Core, Microsoft introduces the next generation of PCL known as .NET Standard. .NET Standard is a set of interfaces that is implemented by different platforms and each .NET Standard has a version number to which that platform supports. There are various versions of .NET Standards to which each platform implements.

Below table shows the .NET standard version and the platform supported.

image

These arrows specifies the supportability of the platform to a version of a .NET Standard. For example, .NET Core 1.0 supports version 1.6 and it can add any of the platform assemblies lower to that version. However, if we need to reference the .NET Core 1.0 assembly in any of the lower versions of .NET Standard, we need to degrade our .NET core assembly’s .NET standard to that version.

Microsoft is now shifting the .NET Core project extension from .xproj to .csproj. As it is still in the preview it is not recommended to use in production. However, today if you are using .NET Core 1.0.* in production and wanted to reference your .NET Core assembly in any of the other platforms you can use .NET Standard and use the version that supports other projects. However, due to the different project extension .xproj we cannot reference any .NET Core assembly in other platforms for example UWP project that has a .csproj project extension.

However, there is a way as shown in this post through which you can create a .NET Core project with a .csproj extension and then easily reference in other .NET project.

1. Create a Class Library (Portable) project

image

2, Select the Target platforms.

image

3. Once this project is created, open up the project properties and click on the “Target .NET Platform Standard” as shown below.

image

4. This will change the “Target” to .NET Standard version and selected the minimal version from the platforms selected.

image

5. Finally, you have an assembly which is a .NET Core 1.0 assembly and it can be added to any platform which supports particular .NET standard or lower version.

Hope this helps!

Publishing ASP.NET Core in IIS

With ASP.NET Core, we can develop applications that runs cross-platform. We have choices whether to use the full .NET framework (4.5.2 or greater) or .NET Core, where .NET Core works cross platform. Comparatively with ASP.NET 4, ASP.NET Core framework is more powerful, modular and fast and provide different options for hosting ASP.NET Core application.

ASP.NET Core comes with two servers that hooks up the port to listen for requests.

WebListener: Only for Windows

Kestrel (recommended) : For Windows, MAC and Linux

Both Kestrel and WebListener servers are a very light weight servers with a very basic feature set. With production, we usually want other capabilities like load balancing, HTTP modules, request filtering, etc. Therefore, we have to pair Kestrel with any mature or powerful server like IIS or Apache that can act as a reverse proxy, listen for incoming request and pass it to kestrel to process. image

Configure IIS to Publish ASP.NET Core Apps

We will start first by setting up IIS to run ASP.NET Core app. First of all ensure that the IIS is installed on your OS. You can install IIS from windows features under program and features option.

Once the IIS is installed, we have to install the .NET Core hosting bundle that installs the .NET Core runtime, .NET Core library and the ASP.NET Core Module (ANCM). The job of ANCM is to create a reverse proxy between IIS and Kestrel. Restart is recommended after installing these components.

Create Website and Configure App Pool in IIS

To create an application, open IIS and create a new website as shown below.

image

 

Once the website is created, go to Application Pool and select No Managed Code for HelloWorld application as shown below.

image

This is important, because for ASP.NET Core apps we don’t want IIS to process the request.

Adding Data Protection

ASP.NET Core uses Data Protection API to encrypt and store keys that are used for authentication. When hosting your application with IIS, we need to run power shell script that creates a registry hive where all the application level keys are stored.

to create our Application registry hive, we will first go to https://raw.githubusercontent.com/aspnet/DataProtection/dev/Provision-AutoGenKeys.ps1 and download it to some local repository. Then open Power shell in Administrative mode and run following command to get the remotesigned policy to run the .ps1 file downloaded above.

powershell –ExecutionPolicy RemoteSigned

Next, we point it to .ps1 file and specify our Application Name (HelloWorld) as shown below.

image

Adding IIS Integration Package in ASP.NET Core and Publish

To run ASP.NET Core application on IIS we have to add a Nuget Package known as Microsoft.AspNetCore.Server.IISIntegration.Tools and make an entry in the program.cs to use IIS integration by calling UseIISIntegration method.

Here is the screen shot of the NuGet package needed to be installed.

image

And here is the updated program.cs of our HelloWorld application

image

UseIISIntegration method actually tells your ASP.NET Core that IIS will be the reverse proxy for the incoming requests. It doesn’t replace Kestrel but allow IIS and ASP.NET Core to setup ports and read other environment variables.

Once this is done, publish your application. Publishing can be done either through Visual Studio or through a Dotnet CLI (dotnet publish). When publishing, choose a destination to the path which is configured in IIS for your application so you don’t have to copy the package files again.

Running your application

Navigate to localhost (hostname as configured above while creating a website) and you will see the default ASP.NET Core home page as shown below.

image

Hope this helps!

My book on JavaScript for .NET Developers

I am pleased to inform you that my book on “JavaScript for .NET developers has been published by Packt Publishing. It was a very good experience but of course it took lot of time from me.

image

This book is a mastering series and targeted primarily to .NET Developers but other non .NET developers can also leverage with the knowledge I shared in this book and understand the concepts and use them with any web application platform. I have tried to accommodate all the information that will be required to master JavaScript.

This book has 10 chapters, starting from the basics to advanced concepts, learning superset of JavaScript i.e. TypeScript and discusses what superset is and how to use TypeScript with Angular 2 and ASP.NET Core. One chapter on WinJS, a JavaScript library that is widely used by UWP developers to bring rich look and feel and other windows runtime features to web applications. Chapter on Node.js that shows how JavaScript can be used on server side and how to develop Node.js applications using Visual Studio 2015. Then covered all about design patterns and shown the usage of around dozen of design patterns to handle particular problem or scenario. Finally, there are the chapters of using JavaScript for large scale projects and how to test and debug JavaScript.

To order either an e-book or a printed copy, It is available on Packt Publishing website i.e. https://www.packtpub.com/application-development/javascript-net-developers and Amazon at https://www.amazon.com/JavaScript-Developers-Ovais-Mehboob-Ahmed-ebook/dp/B01E7GP8I6

Enjoy reading!!

Using WCF SOAP Services in ASP.NET Core

When working with an ASP.NET Core application in Visual Studio 2015, there is no out of the box support to add WCF service references. To add services references of WCF services, Microsoft introduced the WCF Connected services preview to generate the SOAP service references for applications that are using .NET Core.

To install WCF Connected Services tool extension we need to have Visual Studio 2015 installed. And then it can be installed from the Visual Studio Extensions and Updates dialog as shown below. This dialog can be opened from Tools > Extensions and Updates option. Search for WCF Connected Services and download. This tool requires to have Visual Studio Update 3 plus the ASP.NET and Web Tools for Visual Studio 2015 extension installed as well.

  image

After installing WCF Connected Services, It asks you to restart Visual Studio and then you can add service reference by right clicking the references node and click on Add Connected Service option

image

Add Connected Service option will open up the dialog where you can choose  WCF Service – Preview as shown below

image

And finally, you can specify the service URL and add WCF Soap Service reference in your project that creates a proxy and you can use that proxy object to invoke WCF service methods.

Hope this helps!

Transaction Per Request using AutoFac in WebAPI

When working with business applications, transactions plays an important role and providing atomic transactions is a good practice to commit or rollback transaction on either condition. In this post we will implement the Transaction per request in Web API controllers using AutoFac and action filters. Transaction per request is a recommended approach when designing your Web API as it provides the Database object per request and no matter how many transactions you are doing in particular request but provides commit and rollback feature using action filters. We will see how easy it is to handle that using AutoFac dependency injector

Following are the steps you can implement in your Web API project to implement Transaction Per request.

1. FIrst of all add the Autofac for Web API package through NuGet

image

2. Add Entity framework file and connect to some data source. In my case I have ProductEntities and below is the Web API code of Products

public class ProductsController : ApiController
{

ProductsEntities context;

public ProductsController(ProductsEntities entities)
{
context = entities;
}

// GET: api/Products
public IEnumerable<Product> Get()
{
return context.Products.AsEnumerable();
}

// GET: api/Products/5
public Product Get(int id)
{
return context.Products.Where(i => i.ProductID == id).FirstOrDefault();
}

// POST: api/Products
public void Post(Product product)
{
context.Products.Add(product);
context.SaveChanges();

}

// DELETE: api/Products/5
public void Delete(int id)
{
var product = context.Products.Where(i => i.ProductID == id).FirstOrDefault();
if (product != null)
{
context.Products.Remove(product);
context.SaveChanges();
}
}
}

 

I have defined a pamaterized constructor that takes ProductEntities as a parameter to perform CRUD operation on database

4. Now go to your Startup class and write following code snippet.

using Autofac;
using Autofac.Integration.WebApi;
using System.Reflection;

public void Configuration(IAppBuilder app)
{

        var builder = new ContainerBuilder();
var config = GlobalConfiguration.Configuration;

//Register all Controllers with AutoFac so it will inject ProductEntities by calling
//parameterized constructors
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterWebApiFilterProvider(config);

//Initialize ProductEntities per request
builder.RegisterType<ProductsEntities>().InstancePerRequest();

var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

ConfigureAuth(app);

}

First initialize the dependency container builder object and register API controllers that resides in your project by calling builder.RegisterApiControllers method.  After that I have register the ProductEntities type as InstancePerRequest. This InstancePerRequest will create the new context object per request to preform CRUD operation. Next, build your builder and specify the dependency resolver for your Web API.

5. That’s all set for injecting ProductEntities in Web API Controllers

6. Now in order to handle transactions, we will create a custom ActionFilter class that imlements IAutofacActionFilter interface

It provides two methods OnActionExecuted and OnActionExecuting that you can use to perform begin transactions and commit/rollback transactions.

public class TransactionActionFilter : IAutofacActionFilter
{
public void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
throw new NotImplementedException();
}

public void OnActionExecuting(HttpActionContext actionContext)
{
throw new NotImplementedException();
}
}

But before writing code to begin or commit transactions we have to inject the ProductEntities so create parameterized constructor and pass ProductEntities. So lets add a TransactionActionFilter constructer with having ProductEnttities as a parameter

ProductsEntities context;
public TransactionActionFilter(ProductsEntities entities)
{
context = entities;
}

7. Following is the code to handle transactions on action executed and executing methods

DbContextTransaction currentTransaction;

public void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Exception != null)
currentTransaction.Rollback();
else
currentTransaction.Commit();
}

public void OnActionExecuting(HttpActionContext actionContext)
{
currentTransaction= context.Database.BeginTransaction();
}

 

8. Finally, register the action filter for Web API controller method to which you want to have transaction applied on. Once the request comes the OnExecuting will be invoked before the actual method call and after it is executed OnExecuted method will be invoked which check if there is any exception and commit or rollback the transaction accordingly.

Register Web API Filter provider

builder.RegisterWebApiFilterProvider(config);

Then add following code to register TransactionActionFilter with your Web API Controller’s method.

builder.Register(c => new TransactionActionFilter(c.Resolve<ProductsEntities>()))
.AsWebApiActionFilterFor<ProductsController>(c => c.Post(default(Product)))
.InstancePerRequest();

 

builder.Build() should be called after all declaration of container injections.

Hope this helps!

2015 in review

The WordPress.com stats helper prepared a 2015 annual report for this blog.

Here’s an excerpt:

The concert hall at the Sydney Opera House holds 2,700 people. This blog was viewed about 34,000 times in 2015. If it were a concert at Sydney Opera House, it would take about 13 sold-out performances for that many people to see it.

Click here to see the complete report.